You get this error when you try to execute an independent block of code from the pipeline chain.
As another example, imagine this code using jQuery:
$("div").not(".main").console.log(this)
Each dot ( . ) Will bind an array to the next function. In the above function, this is aborted using console because it is not intended to enter any values. If we want to get out of our chain to execute some code (possibly on objects in the chain), we can do this with each like this:
$("div").not(".main").each(function() {console.log(this)})
Powershell's solution is identical. If you want to run a script for each element in your chain separately, you can use ForEach-Object or its alias ( % ).
Imagine that Powershell has the following function:
$settings | ?{$_.Key -eq 'Environment' } | $_.Value = "Prod"
The last line cannot be executed because it is a script, but we can fix it with ForEach as follows:
$settings | ?{$_.Key -eq 'Environment' } | %{ $_.Value = "Prod" }
Kylemit
source share