Powershell 匹配system.string值
我正在使用Powershell调用命令并将结果作为system.string获取,我需要匹配结果的一部分作为检查点以继续使用脚本 例如,我将结果添加到一个名为$upgradecomm的变量中,如果我尝试打印它,我得到的结果如下:Powershell 匹配system.string值,powershell,Powershell,我正在使用Powershell调用命令并将结果作为system.string获取,我需要匹配结果的一部分作为检查点以继续使用脚本 例如,我将结果添加到一个名为$upgradecomm的变量中,如果我尝试打印它,我得到的结果如下: C:\> $upgradecomm Host : HQ-ESXi-Edge-01a.nsx.gss Output : {Update Result, Message: The update completed successfully
C:\> $upgradecomm
Host : HQ-ESXi-Edge-01a.nsx.gss
Output : {Update Result, Message: The update completed successfully, but the system needs to be rebooted for the changes to be effective., Reboot Required: true, VIBs
Installed: VMware_bootbank.........
“上面的输出仍然有更多,但我不会将它们添加到此帖子中。”
我需要的是匹配消息部分中显示的部分(更新成功完成,但需要重新启动系统才能使更改生效)
如果我只打印输出,我将得到以下结果:
C:\> $upgradecomm.Output
Update Result
Message: The update completed successfully, but the system needs to be rebooted for the changes to be effective.
Reboot Required: true
VIBs Installed: VMware_bootbank.....................
“上面的输出仍然有更多,但我不会将它们添加到此帖子中。”
我无法在这里获得我需要的内容,因为我说过我需要检查是否从结果中获得以下内容(更新成功完成,但需要重新启动系统才能使更改生效)。这样我就可以继续我的脚本,如果没有获得,我将打印错误
任何想法都将受到欢迎。如果你不在乎正文的其余部分说了什么,像这样简单的
,就足够了
$success = "*The update completed successfully, but the system needs to be rebooted for the changes to be effective.*"
if($upgradecomm.Output -like $success){
#success
}
输出
-属性是字符串数组。您可以使用ex.-match
在数组中搜索与关键字匹配的字符串。如果它返回任何值,那就是匹配
#Creating sampleobject
$o = "Update Result",
" Message: The update completed successfully, but the system needs to be rebooted for the changes to be effective.",
" Reboot Required: true",
" VIBs Installed: VMware_bootbank....................."
$upgradecomm = New-Object psobject -Property @{
Host = "Server1"
Output = $o
}
#Verify that sampleobject is correct
$upgradecomm | Format-List *
#Output
Host : Server1
Output : {Update Result, Message: The update completed successfully, but the system needs to be rebooted for the changes to be effe
ctive., Reboot Required: true, VIBs Installed: VMware_bootbank.....................}
#Check if successfull and rebooted are in the message-string in output
if($upgradecomm.Output -match 'Message: .*?successfull.*?rebooted') { "Do something" }
#Output
Do something
.Output-如“*成功完成,但系统需要重新启动*”
?