简体   繁体   English

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

[英]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
}

it always falls back to else state cause the the cmd command always start with the same: 它总是回退到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.

tried to hide the first 3 rows with select-object -skip 3 but this doenst work like i wanted, powershell still sees this: i think the select-object only hides it for the user, not for the script. 试图用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.

Any idea how to get this working correctly? 知道如何正常工作吗? Thanks alot 非常感谢

You've done good work to get so far, you're almost there. 到目前为止,你已经做了很好的工作,你几乎就在那里。

The -like operator is PowerShell's wild-card comparison operator, and note what you're missing on lines 2 & 8 ? -like操作是PowerShell的通配符比较操作,并注意自己缺少什么lines 2 & 8 Some wildcards! 一些通配符! The -Like operator won't work without `em. 如果没有它们,-Like运算符将无法工作。

A PowerShell wildcard is the asterisk character * , let's add one and see what happens. PowerShell通配符是星号* ,让我们添加一个,看看会发生什么。

I'm going to intentionally throw an error by specifying a drive letter which doesn't exist on my pc (The forgotten A: drive, I still love you 💾). 我打算通过指定我的电脑上不存在的驱动器号来故意抛出错误(被遗忘的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

All I changed was adding * characters around the search string on line 2 of my example above. 我改变的是在我上面例子的第2行的搜索字符串周围添加*字符。

With similar tweaking of your own code, you're basically done already, so you should feel good about this. 通过类似的自己的代码调整,你基本上已经完成了,所以你应该对此感觉良好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM