1

I'm testing a function to stop some processes with a specified pattern in their name:

        ("aaa", "bbb", "ccc") |
        ForEach-Object -ThrottleLimit 5 -Parallel {
          [string]$pattern = -Join ( "`"", "*", $_, "*", "`"")
          [string]$arguments = ("-NoProfile -NoLogo -NoExit -Command Get-Process | Where-Object { `$_.name -like " + $pattern+ " } | ForEach-Object -ThrottleLimit 5 -Parallel { Stop-Process `$_ }")
          Write-Output $arguments
          Start-Process pwsh -ArgumentList $arguments -Verb RunAs -WindowStyle Normal -Wait
        }

Write-Output shows $arguments string as I expect:

-NoProfile -NoLogo -NoExit -Command Get-Process | Where-Object { $_.name -like "*aaa*" } | ForEach-Object -ThrottleLimit 5 -Parallel { Stop-Process `$_ }

But process windows doesn't:

ParserError:
Line |
   1 |  Get-Process | Where-Object { $_.name -like *aaa* } | ForEach-Object - …
     |                                            ~
     | You must provide a value expression following the '-like' operator.

What am I missing?

Many thanks for you help.

Alby11
  • 607
  • 1
  • 7
  • 15

0 Answers0