powershell - 从cmd命令到Powershell获取正确的变量状态

标签 powershell variables cmd output

$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/

相关文章:

batch-file - 合并不带标题的 CSV 文件

powershell - 如何存储 Format-Table 的输出以供以后使用

powershell - 如何在 Powershell 中为 conda activate 创建别名并重命名 shelltab

javascript - 如何更新以编程方式设置的文本区域的输入

python - 根据行值对数据框进行子集化,其中行值和列名存储在 VARIABLE 中

c++ - 如何从Lua运行进程中读取变量值(获取进程变量值)?

powershell - 在Office 365中通过Powershell使用CSV创建新邮箱

c# - 使用 PowerShell 中的 WinRM 连接到远程服务器失败

windows - 将参数传递给 powershell 脚本

c# - 如何以编程方式解析命令(例如 gacutil)的路径?