![](/img/trans.png)
[英]How to direct output from a Powershell command into a CMD variable
[英]Get correct variable status from cmd command to powershell
$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.
试图用select-object -skip 3隐藏前3行,但是这个doenst工作就像我想要的那样,powershell仍然看到这个:我认为select-object只为用户隐藏它,而不是为脚本隐藏它。
vssadmin 1.1 - Volume Shadow Copy Service administrative command-line tool
(C) Copyright 2001-2013 Microsoft Corp.
知道如何正常工作吗? 非常感谢
到目前为止,你已经做了很好的工作,你几乎就在那里。
该-like
操作是PowerShell的通配符比较操作,并注意自己缺少什么lines 2 & 8
? 一些通配符! 如果没有它们,-Like运算符将无法工作。
PowerShell通配符是星号*
,让我们添加一个,看看会发生什么。
我打算通过指定我的电脑上不存在的驱动器号来故意抛出错误(被遗忘的A:驱动器,我仍然爱你💾)。
$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行的搜索字符串周围添加*
字符。
通过类似的自己的代码调整,你基本上已经完成了,所以你应该对此感觉良好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.