什么是语法包括在当量命令几个值:
这工作,但我认为有一种方法来节省一些打字:
Get-Service | where {($_.Status -eq "Stopped") -OR ($_.Status -eq "Running")}
思码应该看起来像,但我不记得确切的语法:
Get-Service | where {($_.Status -eq "Stopped"|"Running"|"...")}
什么是语法包括在当量命令几个值:
这工作,但我认为有一种方法来节省一些打字:
Get-Service | where {($_.Status -eq "Stopped") -OR ($_.Status -eq "Running")}
思码应该看起来像,但我不记得确切的语法:
Get-Service | where {($_.Status -eq "Stopped"|"Running"|"...")}
您可以使用-contains
和gsv
别名:
gsv | where-object {@("Stopped","Running") -contains $_.Status}
编辑:您还可以使用-match
操作:
gsv | where-object {$_.Status -match "Stopped|Running"}
2.EDIT:较短的版本,W /特别感谢@Joey:
gsv | ? {$_.Status -match "Stopped|Running"}
基于正则表达式组的匹配总是最短和最安全的方式。你也可以使用互补:
gsv | ? {$_.status -notmatch "Paused|Running_Pending|Pause_Pending|Stop_Pending|Continue_Pending"}
在这种情况下,它不是最短的,显然;但有时它是!
请注意,这里的相应逻辑是'where {($ _。Status -ne“Paused”)-AND($ _。Status -ne“Running_Pending”)} -AND($ _。Status -ne“Pause_Pending”)} -and($ _。状态-ne “Stop_Pending”)} - 和($ _。状态-ne “Continue_Pending”)}'。希望这个帮助。 – 2011-04-01 21:18:59
正如@OcasoProtal所述,您可以使用-contains
或-notcontains
运算符比较一组有效状态与目标状态。
鉴于现状是基于枚举类型还可以使用的是(即相对于使用的字符串比较)。这增加了额外的验证(即,没有手动指定ValidateSet),并允许拉值的列表从枚举(例如,如示于我的示例代码,其中被指定Not
下面
clear-host
[string]$ComputerName = $env:COMPUTERNAME
[string]$ServiceName = "MSSQLSERVER"
function Wait-ServiceStatus {
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$ServiceName
,
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$ComputerName = $env:COMPUTERNAME
,
[switch]$Not
,
[Parameter(Mandatory = $true)]
[System.ServiceProcess.ServiceControllerStatus]$TartgetStatus
)
begin {
[System.ServiceProcess.ServiceControllerStatus[]]$TargetStatuses = @($TartgetStatus)
if ($Not.IsPresent) {
#EXAMPLE: Build your comparison array direct from the ENUM
$TargetStatuses = [Enum]::GetValues([System.ServiceProcess.ServiceControllerStatus]) | ?{$_ -ne $TartgetStatus}
}
}
process {
#EXAMPLE: Compare status against an array of statuses
while ($TargetStatuses -notcontains (Get-Service -ComputerName $ComputerName -Name $ServiceName | Select -Expand Status)) {
write-host "." -NoNewline -ForegroundColor Red
start-sleep -seconds 1
}
write-host ""
#this is a demo of array of statuses, so won't bother adding code for timeouts/etc
}
}
function Write-InfoToHost ($text) {write-host $text -ForegroundColor cyan} #quick thing to make our status updates distinct from function call output
Write-InfoToHost "Report Current Service Status"
get-service -Name $ServiceName -Computer $ComputerName | Select -ExpandProperty Status
Write-InfoToHost ("Stop Service at {0:HH:mm:ss}" -f (get-date))
(Get-WmiObject Win32_Service -Filter "name='$ServiceName'" -Computer $ComputerName).StopService() | out-null #use WMI to prevent waiting
Write-InfoToHost ("Invoked Stop Service at {0:HH:mm:ss}" -f (get-date))
Wait-ServiceStatus -ServiceName $ServiceName -TartgetStatus Stopped
Write-InfoToHost ("Stop Service Completed at {0:HH:mm:ss}" -f (get-date))
Write-InfoToHost "Report Current Service Status"
get-service -Name $ServiceName -Computer $ComputerName | Select -ExpandProperty Status
Write-InfoToHost ("Start Service at {0:HH:mm:ss}" -f (get-date))
(Get-WmiObject Win32_Service -Filter "name='$ServiceName'" -Computer $ComputerName).StartService() | out-null #use WMI to prevent waiting
Write-InfoToHost ("Invoked Start Service at {0:HH:mm:ss}" -f (get-date))
Wait-ServiceStatus -ServiceName $ServiceName -Not -TartgetStatus Stopped
Write-InfoToHost ("Service Not Stopped at {0:HH:mm:ss}" -f (get-date))
Wait-ServiceStatus -ServiceName $ServiceName -Not -TartgetStatus StartPending
Write-InfoToHost ("Service Not Start-Pending at {0:HH:mm:ss}" -f (get-date))
Write-InfoToHost "Report Current Service Status"
get-service -Name $ServiceName -Computer $ComputerName | Select -ExpandProperty Status
样本输出:
Report Current Service Status
Running
Stop Service at 12:04:49
Invoked Stop Service at 12:04:50
.
Stop Service Completed at 12:04:51
Report Current Service Status
Stopped
Start Service at 12:04:51
Invoked Start Service at 12:04:52
Service Not Stopped at 12:04:52
..
Service Not Start-Pending at 12:04:54
Report Current Service Status
Running
使用这样的事情你也可以很容易地得到挂起或“稳定状态”的状态:
function PendingDemo([bool]$Pending) {
write-host "Pending is $Pending" -ForegroundColor cyan
[Enum]::GetValues([System.ServiceProcess.ServiceControllerStatus]) | ?{($_ -notlike "*Pending") -xor $Pending}
}
PendingDemo $true
""
PendingDemo $false
样本输出:
Pending is True
StartPending
StopPending
ContinuePending
PausePending
Pending is False
Stopped
Running
Paused
关闭,其实。 '“停止”,“运行” -eq $ _。Status'会的工作,但只是因为没有枚举值是'0'。 – Joey 2011-04-01 13:47:53