Powershell Get-Process string as name -
when using get-process how can use string -name?
$program = "myprogram" get-process $program instead of
get-process -myprogram
powershell accept name, no matter what.
get-process firefox will return running firefox. likewise, arrays work too:
get-process firefox, iexplore or
$proc = @("firefox","iexplore") get-process $proc declares array explicitly, checks each running process.
of course, stated, can , should use -name clarity. nothing changes. script should still read like:
$proc = @("firefox","iexplore") get-process -name $proc there where-object approach:
get-process | where-object {$_.name -eq "firefox"} to "stuff" it, pipe objects script block.
where-object:
ps c:\> get-process | where-object {$_.name -eq "firefox"} | fl $_.id returns:
id : 7516 handles : 628 cpu : 1154.1421983 name : firefox get-process $proc | ft id, name
would return:
id name -- ---- 7516 firefox 12244 iexplore 12640 iexplore or, use foreach-object mentioned, , loop through them:
get-process $proc | foreach-object {if($_.path -contains "c:\program files*"){write-host "bad directory"} else{ write-host "safe"}} if running .exe above running c:\program files* writes out "safe", otherwise writes "bad directory".
Comments
Post a Comment