Após o ForEach-Object (or aliases: ForEach, %)
, coloque o código que segue nas chaves {}
.
Portanto, altere esta linha do que você tem:
Get-Process $args[0] -ErrorAction SilentlyContinue | ForEach Kill-ChildProcess -id {$_.ID};
para isso, que coloca corretamente as chaves ao redor do código após ForEach-Object
:
Get-Process $args[0] -ErrorAction SilentlyContinue | ForEach { Kill-ChildProcess -id $_.ID }
Deixe-nos saber se isso resolve aquela mensagem de erro específica que você postou.
Além disso, aqui estão algumas informações sobre como usar ForEach-Object
para sua referência:
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.