I have the following tiny PowerShell script that’s meant to kill some certain processes on a remote machine:
$destPS = "mywebserver1"
$brokerPIDs = Get-Process -ComputerName $destPS | ?{$_.processname -eq "uniRQBroker" -or $_.processname -eq "uniRTE"}
foreach ($process in $brokerPIDs){
$thisId = $process.ID
Write-Host "Killing PID $thisId"
Invoke-Command $destPS {Stop-Process $thisId}
}
However, I’m getting the following error:
Cannot bind argument to parameter ‘Id’ because it is null.
As far as I can see, the pipeline shouldn’t be interrupted by anything, so I’m not quite sure where I’m going wrong.
The script block doesn’t get the
$thisIdand that is set to null. Sostop-processgives the error. You can pass the arguments to the script block like @Rynant mentions.Since all you are doing is to get the processes and kill processes that match your requirement, move the commands into a script block and execute that scriptblock as whole using
Invoke-Commandon the remote box: