Groups > Microsoft > Windows PowerShell > Re: Executing external program




Executing external program

Executing external program
Tue, 8 Apr 2008 07:13:00 -0700
I am trying to execute an external program from my powershell script. This 
program takes several parameters. I have built a string ($strCmd) that 
includes my exe and all parameters. I then tried the following:

Invoke-item ($strCmd) 

but that didn't work so I tried using:

(new-object -com shell.application).shellexecute($strCmd)

but that doesn't work either.

If I run the command without the parameters it works fine. 

What should I do to execute this program with all of it's associated 
parameters?

Thanks.
Post Reply
Re: Executing external program
Tue, 8 Apr 2008 07:56:01 -0700
Here's the command I'm trying to execute:

C:\Vmware\vcbMounter.exe -h <host> -u <user> -p <password> -s
name:<name of 
machine> -r <location> -t <backup type>

All of the parameters are dynamic. Will this still work?

Thanks.

"Steven Murawski" wrote:

> Snowmizer wrote:
> > I am trying to execute an external program from my powershell script.
This 
> > program takes several parameters. I have built a string ($strCmd) that

> > includes my exe and all parameters. I then tried the following:
> > 
> > Invoke-item ($strCmd) 
> > 
> > but that didn't work so I tried using:
> > 
> > (new-object -com shell.application).shellexecute($strCmd)
> > 
> > but that doesn't work either.
> > 
> > If I run the command without the parameters it works fine. 
> > 
> > What should I do to execute this program with all of it's associated 
> > parameters?
> > 
> > Thanks.
> > 
> Snowmizer,
> 
> You could use
> 
> [Diagnostics.Process]::Start('yourapplication','arguments')
> 
> 
> Steven Murawski
> Co-Host - Mind of Root
> http://www.mindofroot.com
> Host - PowerShell Basics
> http://powershell-basics.com
Post Reply
Re: Executing external program
Tue, 8 Apr 2008 09:14:02 -0700
Steven,

That worked great...you're a genius. Now for my next question. This command 
is running a backup command on a virtual machine. When this backup command 
finishes executing (could be 15 minutes or longer) This script will loop 
through several times for each virtual machine we need to backup on my server.

I want to check the execution result for each backup and end time so I can 
log this for tracking purposes.  I also want to make sure that I don't start 
too many backups at once. 

Currently when I run the script it starts my backup job, loops through for 
the next virutal machine and starts its backup, etc.... The end result will 
be that my powershell script will be done before my backups.

How can I achieve what I'm looking for? Is there a way to do this?

Thanks.

"Steven Murawski" wrote:

> Snowmizer wrote:
> > Here's the command I'm trying to execute:
> > 
> > C:\Vmware\vcbMounter.exe -h <host> -u <user> -p
<password> -s name:<name of 
> > machine> -r <location> -t <backup type>
> > 
> > All of the parameters are dynamic. Will this still work?
> > 
> > Thanks.
> > 
> > "Steven Murawski" wrote:
> > 
> >> Snowmizer wrote:
> >>> I am trying to execute an external program from my powershell
script. This 
> >>> program takes several parameters. I have built a string
($strCmd) that 
> >>> includes my exe and all parameters. I then tried the
following:
> >>>
> >>> Invoke-item ($strCmd) 
> >>>
> >>> but that didn't work so I tried using:
> >>>
> >>> (new-object -com shell.application).shellexecute($strCmd)
> >>>
> >>> but that doesn't work either.
> >>>
> >>> If I run the command without the parameters it works fine. 
> >>>
> >>> What should I do to execute this program with all of it's
associated 
> >>> parameters?
> >>>
> >>> Thanks.
> >>>
> >> Snowmizer,
> >>
> >> You could use
> >>
> >> [Diagnostics.Process]::Start('yourapplication','arguments')
> >>
> >>
> >> Steven Murawski
> >> Co-Host - Mind of Root
> >> http://www.mindofroot.com
> >> Host - PowerShell Basics
> >> http://powershell-basics.com
> >>
> 
> Sure,
> 
> [Diagnostics.Process]::Start('C:\Vmware\vcbMounter.exe',"-h $host -u 
> $user -p $password -s name:$machine -r $location -t $backuptype")
> 
> This should work for you.. There are a number of other ways you could 
> build this too, like..
> 
> $application = 'C:\Vmware\vcbMounter.exe'
> $arguments = "-h  -u  -p  -s name: -r  -t " -f
($host, 
> $user, $password, $machine, $location, $backuptype)
> 
> [Diagnostics.Process]::Start('$application','$arguments')
> 
> 
> Steven Murawski
> Co-Host - Mind of Root
> http://www.mindofroot.com
> Host - PowerShell Basics
> http://powershell-basics.com
> 
Post Reply
Re: Executing external program
Tue, 08 Apr 2008 09:37:03 -050
Snowmizer wrote:
> I am trying to execute an external program from my powershell script. This

> program takes several parameters. I have built a string ($strCmd) that 
> includes my exe and all parameters. I then tried the following:
> 
> Invoke-item ($strCmd) 
> 
> but that didn't work so I tried using:
> 
> (new-object -com shell.application).shellexecute($strCmd)
> 
> but that doesn't work either.
> 
> If I run the command without the parameters it works fine. 
> 
> What should I do to execute this program with all of it's associated 
> parameters?
> 
> Thanks.
> 
Snowmizer,

You could use

[Diagnostics.Process]::Start('yourapplication','arguments')


Steven Murawski
Co-Host - Mind of Root
http://www.mindofroot.com
Host - PowerShell Basics
Post Reply
Re: Executing external program
Tue, 8 Apr 2008 10:06:31 -0600
"Snowmizer" <Snowmizer@discussions.microsoft.com> wrote in
message 
news:A1159573-9C4D-44C2-9283-419E8C9E42FD@microsoft.com...
> Here's the command I'm trying to execute:
>
> C:\Vmware\vcbMounter.exe -h <host> -u <user> -p
<password> -s name:<name 
> of
> machine> -r <location> -t <backup type>
>
> All of the parameters are dynamic. Will this still work?

You shouldn’t have to resort to [Diagnostics.Process]::Start for this.  Try 
this:

$exe = "C:\Vmware\vcbMounter.exe"
$host = "server"
$user = "joe"
$password = "cleartextpasswordsareanono:-)"
$machine = "somepc"
$location = "somelocation"
$backupType = "incremental"

& $exe -h $host -u $user -p $password -s "name:$machine" -r
$location -t 
$backupType

If your exe has problems with these parameters grab a copy of PSCX and 
substitute echoargs.exe for $exe above.  This will show you how your EXE is 
receiving the arguments from PowerShell.

--
Keith
PSCX - http://www.codeplex.com/powershellcx

 
Post Reply
<< Previous 1 2 3 Next >>
( Page 1 of 3 )
about | contact