3

Would any of you possibly know why this is not working?

Start-Process $PSHOME\powershell.exe -ArgumentList "-NoExit -Command & `"{$outvar1 = 4+4; `"out: $outvar1`"}`"" -Wait

The ultimate purpose for this is so that i can run a script block as another user with the addition of the -Credential option. But i can not get this simple script block to work yet. Many thanks. Chris.

chris
  • 233
  • 2
  • 3
  • 6
  • 1
    Any reason you're not using `Invoke-Command` which can run a script block locally or remotely with specified credentials (see example 2 in its [help](http://technet.microsoft.com/en-us/library/dd347578.aspx))? – Richard Sep 06 '11 at 11:13
  • Hi, Well truth is that im having an error when executing the AutoSPInstaller script, at line 2056 AutoSPInstallerFunctions.ps1. Im trying to replicate what it does, the original line is ; Start-Process $PSHOME\powershell.exe -Credential $FarmCredential -ArgumentList "-Command Start-Process $PSHOME\powershell.exe -ArgumentList `"'$ScriptFile'`" -Verb Runas" -Wait – chris Sep 06 '11 at 11:43
  • where script file is the path and file name of a script that was generated earlier in the process – chris Sep 06 '11 at 11:44

1 Answers1

2

Here is somthing that is working:

PS C:\> Start-Process $PSHOME\powershell.exe -ArgumentList "-NoExit","-Command  `"&{`$outvar1 = 4+4; `"write-output `$outvar1`"}`"" -Wait

-ArgumentList is an array of strings $outvar is interpreted so I use `$outvar

ExceptionLimeCat
  • 6,191
  • 6
  • 44
  • 77
JPBlanc
  • 70,406
  • 17
  • 130
  • 175
  • Great, and then to add some text before we just escape the special chars, now i understand. cheers Start-Process $PSHOME\powershell.exe -ArgumentList "-NoExit","-Command `"&{`$outvar1 = 4+4; `"write-output `"Hello:`"`$outvar1`"}`"" -Wait – chris Sep 06 '11 at 11:50
  • 5
    FYI, I prefer to use `$p = Start-Process ... -PassThru; Wait-Process $p.id -Timeout xxx`. Just in case the started process hangs, my script won't hang any longer than the specified timeout. – Keith Hill Sep 06 '11 at 13:44