Start-Process issue with -ArgumentList

In a Powershell script I needed to call another Powershell script “as administrateur”, thus triggering the UAC on purpose. (assuming the user account is a local admin) A Start-Process -verb ‘Runas’ would do the job, but I had some problems with the arguments passed to the called .ps1 script.

So I wrote this ‘test.ps1’ script that displays the arguments it receives:

for ( $i = 0; $i -lt $args.count; $i++ ) { write-host ( '$args[' + $i + ']="' + $args[$i] + '"') } write-host 'Press any key to continue...' $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | out-null 

This below code should work properly but in fact it does not (you can test without -Verb ‘RunAs’):

start-process -Verb 'RunAs' -FilePath 'powershell.exe' -ArgumentList @( 'C:Powershell_testtest.ps1', 'one', 'two', 'and three' ) 

This will display 4 arguments instead of 3, the last argument is splitted:

$args[0]="one" $args[1]="two" $args[2]="and" $args[3]="three" Press any key to continue... 

The only way I found to obtain only the 3 arguments is to pass “‘and three'” as last argument! ( ‘”and three”‘ won’t work ):

start-process -Verb 'RunAs' -FilePath 'powershell.exe' -ArgumentList @( 'C:Powershell_testtest.ps1', 'one', 'two', "'and three'" ) 

This will display:

$args[0]="one" $args[1]="two" $args[2]="and three" Press any key to continue... 

Maybe the Powershell team had to compromise with the “DOS” way of command line parsing but THIS is really dirty! (and undocumented as far as I know…)

Do you think there is another solution without ‘”and three”‘?

submitted by /u/denisrennes
[link] [comments]

Leave a Reply