Start-Process System.InvalidOperationException: This command cannot be run due to the error: The handle is invalid.

Horacio 1 Reputation point
2020-10-02T19:17:34.75+00:00

I have a task that is running with NETWORK SERVICE account that is calling a ps1, the ps1 is calling Start-Process with credentials, the arguments for the actions in the task are: -Version "5.0" -Noninteractive -Noprofile -WindowStyle "Hidden" -Command "& 'C:\Dev\Connectv3\connect-reports\PSScripts\StartProcess.ps1'", the settings of the task:
29916-image.png

And the code

ry {  
  
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"  
  
#a) StartProcess  
  
 $commandlet = "'" + $commandlet + "'"  
$argList = "$commandlet"  
$argList = ' -Version "5.0" -NoProfile -WindowStyle "Hidden" -Command "& '+ $argList + """"  
Write-Host $argList  
Start-Process -FilePath powershell.exe -ArgumentList $argList -Credential $credentials -Verbose -Wait -RedirectStandardError "StartProcessErrors.log" -WorkingDirectory $PSHOME  
  
  
}  
catch {  
    $_ | format-list -force | Out-String   
    throw  
}  
finally{  
Stop-Transcript  
}  
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.