I have a task that is running with NETWORK SERVICE account that is calling a ps1, the ps1 is calling System.Diagnostics.ProcessStartInfo and System.Diagnostics.Process with credentials and always have the same exit Code -1073741502 and never calls the StartProcessDummy.ps1 , the arguments for the actions in the task are: -Version "5.0" -Noninteractive -Noprofile -WindowStyle "Hidden" -Command "& 'C:\Dev\StartProcessInfo.ps1'", the settings of the task:
And the code:
try {
Start-Transcript -Path "$($PSScriptRoot)\StartProcess.log"
$user = "DOMAIN\userName"
$pswd = "Password" | ConvertTo-SecureString -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $user,$pswd
$workingDirectory = $PSScriptRoot
$commandlet = "$($PSScriptRoot)\StartProcessDummy.ps1"
$argList = ' -Command ""& '+ $commandlet + '""'
ProcessStartInfo
$psi = New-Object System.Diagnostics.ProcessStartInfo -Prop @{
RedirectStandardError = $True
RedirectStandardOutput = $True
UseShellExecute = $False
UserName = $credentials.GetNetworkCredential().UserName
Domain = $credentials.GetNetworkCredential().Domain
Password = $credentials.Password
WorkingDirectory = $PSScriptRoot
FileName = "powershell.exe"
Arguments = $argList
WindowStyle = "Hidden"
}
$whosthis=whoami
Write-Host "WhoAmi: $whosthis"
Write-Host "ProcessStartInfo:"
Write-Host "*******************************************************************"
#$psi | Format-List -Property * | Out-String
Write-Host
Write-Host
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $psi
$p.Start() | Out-Null
$p.WaitForExit()
Write-Host "Process:"
Write-Host "*******************************************************************"
#$p | Format-List -Property * | Out-String
$stdout = $p.StandardOutput.ReadToEnd()
$stderr = $p.StandardError.ReadToEnd()
Write-Host "stdout: $stdout"
Write-Host "stderr: $stderr"
Write-Host "exit code: " + $p.ExitCode
}
catch {
$_ | format-list -force | Out-String
throw
}
finally{
Stop-Transcript
}