Running cmd.exe through start-process but unable to pass the command to cmd.exe
14,269
Based on CMD documentation, you can specify the parameter /c
or /k
to carry out the command.
Start-Process 'cmd' -Credential $cred -ArgumentList "/c $command"

Author by
Rahul Gupta
Updated on June 27, 2022Comments
-
Rahul Gupta 11 months
I want to run a groovy script with
cmd.exe
under a different user.I have used
Start-Process
, when the script gets executed it just opens the prompt on the screen with different user but doesn't process the$command
.So my question is "How to pass the command after running
cmd.exe
with PowerShell?This is what I have so far:
$username = "abc" $pwd = ConvertTo -SecureString "xyz" -AsPlainText -Force $cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username $pw $command = "filepath/.groovy" try { Start-Process 'cmd' -Credential $cred -ArgumentList $command Write-Host $LASTEXITCODE if($LASTEXITCODE -ne 0) { throw "Error occured" } else { return 0 } } catch { Write-Error "Error Desc:$_Error.InnerException.Message"; }