Search code examples
vmwarevspherepowercli

Retrieving the list of VMWare snapshots using PowerCLI which fall under my criteria


I am trying to get the list of VMWare snapshots whose Uid does not match 'XYZ' using PowerCLI.

I have written the following code to do that.

$body +=echo ""`r`n""`r`n"-----Open VMware snapshots other than Uid 'XYZ'-----"`r`n""`r`n""
Get-VM | Get-snapshot | where {($_.Uid -notmatch 'XYZ')} |  ForEach-Object {
    $object=New-Object -TypeName PSObject -Property @{
        VM_Name = $_.VM
        Created = $_.Created
        Size = "$([math]::Round($_.SizeGB, 2)) GB"
        SnapshotName = $_.name
        Description = $_.extensiondata.description
                 }
    $body +=  $object | ft | out-string
    Write-Output $object    
    }

Running that code in PowerCLI returns all the VMWare snapshots including the one with Uid 'XYZ', which should not happen.

How do I modify the code?


Solution

  • Try using -ne or -notlike in your Where-Object comparison since it appears we're comparing strings and not using Regular Expressions.

    Get-VM | Get-snapshot | where {($_.Uid -notlike 'XYZ')} | ForEach-Object ...