admin管理员组文章数量:1406308
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 badges2 Answers
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
本文标签: powershellHow to pass array of strings as param from function to getprocesswhereobjectStack Overflow
版权声明:本文标题:powershell - How to pass array of strings as param from function to get-processwhere-object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745005729a2637249.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论