pipes
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
pipes
I seem to be able to pipe conditional statements like:
gwmi -class "win32_service" -namespace "rootCIMV2" | Where-Object {$_.StartMode -eq "Auto" -and $_.Status -eq "OK"}
or:
gwmi -class "win32_service" -namespace "rootCIMV2" | Where-Object {$_.StartMode -eq "Auto" -or $_.Status -eq "OK"}
But how do I use the "-not" condition?
I can use something like:
$colitems = gwmi -class "win32_service" -namespace "rootCIMV2" `
foreach ($objItem in $colItems) {
$a = $objItem.Name
# write-host "Name: " $a
$b = $objItem.StartMode
# write-host "StartMode: " $b
$c = $objItem.Status
# write-host "State: " $c
$d = $objItem.State
# write-host "Status " $d
If ($b -ne "Manual")
{ If ($d -ne "Running")
{
Write-Host $a "`t" $b "`t" $c "`t" $d
}
}
}
Is there a better way?
Roy C
pipes
I seem to be able to pipe conditional statements like:
gwmi -class "win32_service" -namespace "rootCIMV2" | Where-Object {$_.StartMode -eq "Auto" -and $_.Status -eq "OK"}
or:
gwmi -class "win32_service" -namespace "rootCIMV2" | Where-Object {$_.StartMode -eq "Auto" -or $_.Status -eq "OK"}
But how do I use the "-not" condition?
I can use something like:
$colitems = gwmi -class "win32_service" -namespace "rootCIMV2" `
foreach ($objItem in $colItems) {
$a = $objItem.Name
# write-host "Name: " $a
$b = $objItem.StartMode
# write-host "StartMode: " $b
$c = $objItem.Status
# write-host "State: " $c
$d = $objItem.State
# write-host "Status " $d
If ($b -ne "Manual")
{ If ($d -ne "Running")
{
Write-Host $a "`t" $b "`t" $c "`t" $d
}
}
}
Is there a better way?
Roy C
pipes
Let me give you some examples to try. These aren't necessarily perfect production expressions but should serve to demonstrate how to use the operators.
get-process | where {$_.company -match "microsoft" -AND $_.name -ne "svchost"}
get-process | where {$_.company -notmatch "microsoft" -or $_.company -match "google"} | Sort Company | Select name,Company
get-service | where {$_.status -eq "Running" -AND -not $_.CanStop}
I also encourage you to look at help about_logical_operator and help about_comparison_operators
get-process | where {$_.company -match "microsoft" -AND $_.name -ne "svchost"}
get-process | where {$_.company -notmatch "microsoft" -or $_.company -match "google"} | Sort Company | Select name,Company
get-service | where {$_.status -eq "Running" -AND -not $_.CanStop}
I also encourage you to look at help about_logical_operator and help about_comparison_operators