3

I'm attempting to kill a parent process and it's child process (There will only be one child) on a remote computer. When executing this script (Which is part of a larger one) i get the following error. PowerShell Newbie, so any suggestions for improvement beyond solving the error are most welcome.

Cannot bind parameter 'Process'. Cannot convert the "Kill-ChildProcess" value of type "System.String" to type "System.Management.Automation.ScriptBlock". + CategoryInfo : InvalidArgument: (:) [ForEach-Object], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ForEachObjectCommand 

Script:

$scriptBlock = { function Kill-ChildProcess(){ param($ID=$PID) $CustomColumnID = @{ Name = 'Id' Expression = { [Int[]]$_.ProcessID } } Write-Host $ID $result = Get-WmiObject -Class Win32_Process -Filter "ParentProcessID=$ID" | Select-Object -Property ProcessName, $CustomColumnID, CommandLine $result | Where-Object { $_.ID -ne $null } | Stop-Process } Get-Process $args[0] -ErrorAction SilentlyContinue | ForEach Kill-ChildProcess -id {$_.ID}; Get-Process $args[0] -ErrorAction SilentlyContinue | Stop-Process -ErrorAction SilentlyContinue; }; Invoke-Command -Session $session -ArgumentList $processToKill -ScriptBlock $scriptBlock 
1
  • Hmm, this doesn't enumerate the process tree, so processes spawned by sub-processes will be excluded. Commented Oct 9, 2020 at 21:12

2 Answers 2

4

The error your getting is because the following line is improperly expressed:

Get-Process $args[0] -ErrorAction SilentlyContinue | ForEach Kill-ChildProcess -id {$_.ID}; 

The error is trying (in microsoft fashion) to tell you that it needs a script block after a ForEach. Replace the line with the following to continue past that error:

Get-Process $args[0] -ErrorAction SilentlyContinue | ForEach {Kill-ChildProcess -id $_.ID} 

Also, just an aside, but powershell does not require line terminators unless you're processing multiple commands on the same line in the shell. In short, you don't need to end every line with ;.

1
  • Cheers! Will check when i get back to work tomorrow morning. Commented Nov 16, 2015 at 20:03
1

After your ForEach-Object (or aliases: ForEach, %) place the code which follows into curly braces {}.

So change this line from what you have:

Get-Process $args[0] -ErrorAction SilentlyContinue | ForEach Kill-ChildProcess -id {$_.ID}; 

to this, which correctly places the curly braces around the code after ForEach-Object:

 Get-Process $args[0] -ErrorAction SilentlyContinue | ForEach { Kill-ChildProcess -id $_.ID } 

Let us know if that resolves that specific error message you posted.


Also, here is some information on using ForEach-Object for your reference:

Beginning in Windows PowerShell 3.0, there are two different ways to construct a ForEach-Object command.

Script block.

You can use a script block to specify the operation. Within the script block, use the $_ variable to represent the current object. The script block is the value of the Process parameter. The script block can contain any Windows PowerShell script.

For example, the following command gets the value of the ProcessName property of each process on the computer.

Get-Process | ForEach-Object {$_.ProcessName} 

Operation statement.

You can also write a operation statement, which is much more like natural language. You can use the operation statement to specify a property value or call a method. Operation statements were introduced in Windows PowerShell 3.0.

For example, the following command also gets the value of the ProcessName property of each process on the computer.

Get-Process | ForEach-Object ProcessName 

When using the script block format, in addition to using the script block that describes the operations that are performed on each input object, you can provide two additional script blocks. The Begin script block, which is the value of the Begin parameter, runs before the first input object is processed. The End script block, which is the value of the End parameter, runs after the last input object is processed.

1
  • Thank you, that solved it (as did the other first answer) Commented Nov 17, 2015 at 9:58

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.