IIIdefconIII
IIIdefconIII

Reputation: 363

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:

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.

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

Upvotes: 1

Views: 295

Answers (1)

FoxDeploy
FoxDeploy

Reputation: 13557

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? Some wildcards! The -Like operator won't work without `em.

A PowerShell wildcard is the asterisk character *, let's add one and see what happens.

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 💾).

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

With similar tweaking of your own code, you're basically done already, so you should feel good about this.

Upvotes: 3

Related Questions