使用Powershell分析RSTCLI的卷状态
我正在尝试使用powershell解析rstcli64(英特尔快速存储技术命令行界面)的输出,以用于Hyper-v 2012裸机服务器。目标是通过返回$true表示“正常”,或$false表示除正常或$null以外的任何内容,来查找任何状态为“非正常”的卷或磁盘。的最终目标是为Icinga创建警报。完成后,我将发布工作脚本。这就是我所处的位置,我可能会以完全错误的方式进行: 我从rstcli64开始:使用Powershell分析RSTCLI的卷状态,powershell,intel,hyper-v,Powershell,Intel,Hyper V,我正在尝试使用powershell解析rstcli64(英特尔快速存储技术命令行界面)的输出,以用于Hyper-v 2012裸机服务器。目标是通过返回$true表示“正常”,或$false表示除正常或$null以外的任何内容,来查找任何状态为“非正常”的卷或磁盘。的最终目标是为Icinga创建警报。完成后,我将发布工作脚本。这就是我所处的位置,我可能会以完全错误的方式进行: 我从rstcli64开始: rstcli64 --information --volume 哪些产出: --VOLUME
rstcli64 --information --volume
哪些产出:
--VOLUME INFORMATION--
Name: Volume0
Raid Level: 10
Size: 466 GB
StripeSize: 64 KB
Num Disks: 4
State: Normal
System: True
Initialized: False
Cache Policy: Off
--DISKS IN VOLUME: Volume0 --
ID: 0-0-0-0
Type: Disk
Disk Type: SATA
State: Normal
Size: 233 GB
Free Size: 0 GB
System Disk: False
Usage: Array member
Serial Number: WD-WCAT1F483065
Model: WDC WD2502ABYS-18B7A0
ID: 0-1-0-0
Type: Disk
Disk Type: SATA
State: Normal
Size: 233 GB
Free Size: 0 GB
System Disk: False
Usage: Array member
Serial Number: WD-WCAT1F468139
Model: WDC WD2502ABYS-18B7A0
ID: 0-2-0-0
Type: Disk
Disk Type: SATA
State: Normal
Size: 233 GB
Free Size: 0 GB
System Disk: False
Usage: Array member
Serial Number: WD-WCAT1H077856
Model: WDC WD2502ABYS-18B7A0
ID: 0-3-0-0
Type: Disk
Disk Type: SATA
State: Normal
Size: 233 GB
Free Size: 0 GB
System Disk: False
Usage: Array member
Serial Number: WD-WCAT1F522503
Model: WDC WD2502ABYS-18B7A0
rstcli64 :
+ CategoryInfo : NotSpecified: (:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
0
我对“State:”条目存在的任何地方都感兴趣,所以我用Select字符串过滤掉它,我使用如下输出:
rstcli64 --information --volume 2> Out-Null | select-string -Pattern "State:"
State: Normal
State: Normal
State: Normal
State: Normal
State: Normal
。。。这就是我所能做到的。我需要找出如果所有的“状态:”字段都是-eq“Normal”,那么如何返回$true,如果没有输出($null),或者如果有任何“状态:”-ne“Normal”,那么如何返回$false
非常感谢您的帮助。多谢各位
编辑:谢谢你的帮助!这就是我最后在中使用了MadTechnician的逻辑:好吧,从您现在的位置开始,这很容易做到。运行正则表达式匹配或运行-like,查看是否存在任何-match或-like,并查找“Normal”。获取一个计数,如果状态的总数-gt 0和该计数-eq与匹配的计数相等,则您都已设置好
$Status = rstcli64 --information --volume 2> Out-Null | select-string -Pattern "State:"
If(($status.count -gt 0) -and ($status.count -eq ($status|Where{$_ -match "Normal"}).count)){
"All is well"
}else{
"Stuff be broke!"
}
另一种方法将捕获状态计数
$states = rstcli64 --information --volume 2> Out-Null | select-string -Pattern "State:"
$notNormalStates = $states | Select-String -Pattern "Normal" -NotMatch
if ($state.Count -gt 0 -and $notNormalStates.Count -eq 0){
Write-Host "Everything Ok"
} Else {
Write-Host "Something Wrong"
}
您可以将Select String
的结果再次导入Select String
,并吐出没有“Normal”的结果。如果您只是对非正常状态计数感兴趣,您可以使用以下任一方法
$notNormalCount = (rstcli64 --information --volume 2> Out-Null | select-string -Pattern "State:" | Select-String -Pattern "Normal" -NotMatch).Count
您还可以使用regex只需一个selectstring
cmdlet
$notNormalCount = (rstcli64 --information --volume 2> Out-Null | Select-String -Pattern "State:\s+(?!.*Normal).*").Count
正则表达式将匹配“State:”后跟任何空格,后跟任何内容,只要它不是“正常”的,就使用负前瞻。值得注意的是,Regex的设计更适合于“匹配”事物,而不是“不匹配”事物 我没有这样做,因为他说他都想检查是否没有输出,或者是否有任何非正常状态。“除此之外,这是一个很好的单线解决方案,即使它有点长。”我试图保存我的答案的维修技师说,“是的,现在回答了他的问题。”。恢复得很好。嗯,看起来很像我现在的答案,你只需在If/Then语句之前预先计算整个非正常状态计数。你做的和我做的不同,使用选择字符串而不是Where-match,所以这绝对是一个不同的答案!这太棒了,谢谢!我发现select字符串的输出需要在回车时拆分,所以我想出了这个方法,发现它工作得很好$state=rstcli64--信息--第2卷>Out Null | select string-Pattern“state:”If((($state-split”
r”).count-gt 0)和($state-split”
r”).count-eq($state-split”`r)|其中{$\匹配“正常”}).count)){{#确定$status=0}其他{#关键$status=2}$status