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
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745005730a4605767.html
评论列表(0条)