Marc Magnin said:
Toolsmith said:
*snip*

Hello,

I'm replying to this old post because it helped me to find a way to dynamically call / run program requiring arguments like :

C:\Program files\Program\program.exe /verysilent /norestart /LoadInf=".\conf.ini"

This work if I write directly the full command in powershell like :

[code]
& C:\Program files\Program\program.exe /verysilent /norestart /LoadInf=".\conf.ini"
[/code]

Great!
But I have no idea of the command in advance... Heck!
So I tried several things like :

[code]
& $fullCommandString
& $commandString $argsString
. $commandString $argsString
. $quotedCommandString $quotedArgsString
powershell -command "& {" $quotedCommandString $quotedArgsString "}"
invoke-item $commandString $argsString
[/code]
etc........

but I still had errors caused by space in "Program files" or slash of arguments etc...

The way I found is to pass a string for the command and an array of string for arguments :
(here I directly wrote the command in the code by in reality, $command is feeded from and XML file)

[code]
$command = "C:\Program files\Program\program.exe /verysilent /norestart /LoadInf='.\conf.ini'"
if($command -match "(?<appPath>.*\.[A-Za-z]+\s)(?<appArgs>.*)"){
   $args = [regex]::Split($Matches.appArgs.trim(), "\s" )
   & $Matches.appPath.trim() $args
}
[/code]

Hope thats helps.

Marc Magnin,
Pourquoi faire simple quand on peut faire compliqué ?!

I'have just found a better way by using the [diagnostics.process] class cause I need to wait the end of execution :

[code]
 if($command -match "(?<appPath>.*\.[A-Za-z]+\s)(?<appArgs>.*)"){
     $com =$Matches.appPath.trim()
     $args =  $Matches.appArgs.trim()
     [diagnostics.process]::start($com, $args).WaitForExit()
}
[/code]


Marc Magnin,
Pourquoi faire simple quand on peut faire compliqué ?!