powershell - How to pass array of strings as param from function to get-processwhere-object - Stack Overflow

I'm trying to write a function so I can pass an array of strings as a parameter that is being used

I'm trying to write a function so I can pass an array of strings as a parameter that is being used by get-process | where-object but It's not matching how I want. The code below kinda shows what I'm having trouble with:

[string[]]$Procs = "notepad","mspaint" 
$Procs -replace '","','|'
$Processes = Get-Process | where-object {$_.name -match $Procs} | select-object name, ID
$Processes2 = Get-Process | where-object {$_.name -match "notepad|mspaint"} | select-object name, ID
write-host "-------- Processes --------"
$Processes
write-host "-------- Processes2 --------"
$Processes2

notepad
mspaint
-------- Processes --------
-------- Processes2 --------

Name         Id
----         --
mspaint   28772
mspaint   32336
notepad    2172
notepad   21168
notepad++ 21056

How do I pass $Procs as as a variable and get output like $Processes2 gives? I tried splitting it and doing a foreach loop but that didn't work either.

I'm trying to write a function so I can pass an array of strings as a parameter that is being used by get-process | where-object but It's not matching how I want. The code below kinda shows what I'm having trouble with:

[string[]]$Procs = "notepad","mspaint" 
$Procs -replace '","','|'
$Processes = Get-Process | where-object {$_.name -match $Procs} | select-object name, ID
$Processes2 = Get-Process | where-object {$_.name -match "notepad|mspaint"} | select-object name, ID
write-host "-------- Processes --------"
$Processes
write-host "-------- Processes2 --------"
$Processes2

notepad
mspaint
-------- Processes --------
-------- Processes2 --------

Name         Id
----         --
mspaint   28772
mspaint   32336
notepad    2172
notepad   21168
notepad++ 21056

How do I pass $Procs as as a variable and get output like $Processes2 gives? I tried splitting it and doing a foreach loop but that didn't work either.

Share Improve this question edited Mar 6 at 18:31 Olumuyiwa 3572 silver badges13 bronze badges asked Mar 5 at 21:53 Matt WilliamsonMatt Williamson 7,1191 gold badge26 silver badges38 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

$Procs = "notepad","mspaint" is an array already, there is no replacement needed, in fact -replace '","','|' is doing nothing other than enumerating the array, there are no "," in it.

If you want to use -match because you're looking for a partial match then you should be joining the elements of the array with |:

[string[]] $Procs = 'notepad', 'mspaint'
$pattern = $Procs -join '|'
$Processes = Get-Process | Where-Object Name -Match $pattern | Select-Object name, ID

In case you're looking for an exact match, then -match could be replaced by -in and no joining would be needed:

[string[]] $Procs = 'notepad', 'mspaint'
$Processes = Get-Process | Where-Object Name -In $Procs | Select-Object name, ID

Select-string can match an array of patterns.

$procs = 'notepad','mspaint'
get-process | where-object { $_.name | select-string $procs }

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
    313      57    12392      32916       0.30   1780   1 mspaint
    249      14     3172      15288       0.11  16412   1 notepad

This also works (although it would also match 'System.Diagnostics.Process'):

get-process | where { $_ | select-string $procs }

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
    313      57    12392      32916       0.30   1780   1 mspaint
    249      14     3172      15288       0.11  16412   1 notepad

Or this, but the process objects get turned into strings:

get-process | select-string $procs

System.Diagnostics.Process (mspaint)
System.Diagnostics.Process (notepad)

get-process itself can have arrays and wildcards:

get-process note*,msp*

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
    313      57    12356      32872       0.38  15540   1 mspaint
    244      13     2640      14612       0.11  16412   1 notepad

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745005730a4605767.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信