$vssstatus = vssadmin list shadowstorage /for="c":\ | select-object -skip 3 | Out-String
if ($vssstatus -like "No items found that satisfy the query.")
{
Write-Host "VSS Shadow Copy: Disabled"
Exit 1010
}
if ($vssstatus -like "Error: Invalid option value.")
{
Write-Host "Partition name incorrect or missing"
Exit 1010
}
else {
Write-Host "VSS Shadow Copy: Enabled |
$vssstatus"
Exit 0
}
它总是退回到else状态,因为cmd命令总是以相同的开头:
vssadmin 1.1 - Volume Shadow Copy Service administrative command-line tool
(C) Copyright 2001-2013 Microsoft Corp.
No items found that satisfy the query.
试图用选择对象-skip 3隐藏前3行,但是像我想要的那样,doesst工作,powershell仍然看到了这一点:我认为选择对象只为用户而不是脚本隐藏了它。
vssadmin 1.1 - Volume Shadow Copy Service administrative command-line tool
(C) Copyright 2001-2013 Microsoft Corp.
任何想法如何使它正常工作?非常感谢
最佳答案
到目前为止,您已经做好了出色的工作,您快要完成了。-like
运算符是PowerShell的通配符比较运算符,请注意您在lines 2 & 8
上缺少什么?一些通配符!如果没有em,-Like运算符将无法工作。
PowerShell通配符是星号字符*
,让我们添加一个并查看会发生什么。
我将通过指定我的PC上不存在的驱动器号来故意抛出错误(被遗忘的A:驱动器,我仍然爱你you)。
$vssstatus = vssadmin list shadowstorage /for="A":\ | select-object -skip 3 | Out-String
if ($vssstatus -like "*Error: Invalid option value.*") {
Write-Warning "Partition name incorrect or missing"
#Exit1010 <--I don't want to exit for this example
}
WARNING: Partition name incorrect or missing
我所做的只是在上面示例的第2行的搜索字符串周围添加了
*
字符。通过对自己的代码进行类似的调整,您基本上已经完成了,因此您应该对此感到满意。
关于powershell - 从cmd命令到Powershell获取正确的变量状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52877300/