VMware Cloud Community
crispyire
Enthusiast
Enthusiast

Display and remove snapshots from vCloud Director 5.1

Hi All,

I've been trying without success to create a PowerCLI script that will report back and delete all snapshot over x days.

If I run the vSphere 5.5 PowerCLI commands:

Get-VM | Get-Snapshot | Where { $_.Created -lt (Get-Date).AddDays(-15)}

I get a list of snapshots that are over 15 days (for example).

My understanding is that if use this script as the basis for the 'Remove-Snapshot', it will 'break' vCloud Director so that is off the cards.

As there doesn't seem to be any native PowerCLI commands to deal with snapshots in vCloud Director 5.1, I altered the command above with help from the module from GeekAfterFive - Infrastructure as Code ‌‌(I'm told that the API hasn't changed from 1.5 to 5.1)

If I rerun the command as such:


Get-CIVM | Get-CISnapshot | Where { $_.Created -lt (Get-Date).AddDays(-15)}

I get the error message:

+ $streamReader = new-object System.IO.StreamReader($response.getResponseStream <<<< ())

+ CategoryInfo          : InvalidOperation: (getResponseStream:String) [],    RuntimeException

+ FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.

Any help would be greatly appreciated!

Chris

1 Reply
pezh0re
Enthusiast
Enthusiast

I hate to bump an old thread - but I'm working on this exact issue. I tried leveraging Get-CIVM - but it's super slow for the 2k VMs in our org.

[CmdletBinding()]

param(

        [PSCredential] $systemAdmin,

        [String] $CIServer= "vCloud.corp.local",

        $maxAge = 60,

        $targetOrg = "devcloud"

     )

BEGIN

{

    Write-Verbose "Importing Modules"

    try

    {

       Import-Module vmadmin -Verbose:$False -Debug:$False

    }

    catch

    {

        Write-Host "$($_ |select -ExpandProperty Exception)"

    }

    if (!$systemAdmin) { $systemAdmin = get-credential -Message "zId" }

   

    try

    {

        Write-Verbose "Connecting to vCloud $CIServeras $($systemAdmin.username)"

        $connected = Connect-CIServer $CIServer-Credential $systemAdmin -ErrorAction Stop -Verbose:$False -Debug:$False

    }

    catch

    {

        Write-Host "Cannot connect to vCloud server."

        exit

    }

}

PROCESS

{

    Write-Verbose "Getting all CIVMs... this could take a while"

   

    # Get all the CIVMs in the target org

    $myCIVMs = Get-CIVM -Org $TargetOrg -Verbose:$False -Debug:$False # -Owner $eId.UserName

    # Determine our max age based on today

    $now = Get-Date

    $maxSnapshotAge = $now.AddDays(-$maxAge)

   

    $removalCandidates = @()

    Write-Verbose "Finding VMs in all vCloud vApps with Snapshots older than $maxAge"

    foreach ($myVM in $myCIVMs)

    {

        Write-Progress -Activity "Analyzing vApps" -Status "vApp $($myVM.Name): ($($myCIVMs.IndexOf($myVM)) of $($myCIVMs.length)"

        $snaps = $myVM | Get-CISnapshot -Verbose:$False -Debug:$False

        if ($snaps.Created -ge $maxSnapshotAge)

        {

            $candidate = [PSObject]@{

                            Snap = $snaps

                            myVM = $myVM

                         }

            $removalCandidates += $candidate

        }

    }

}

END

{

    Write-Debug "Anything Else?"

}

I'm currently working on a much faster way - leveraging Get-View limiting to the vCenter resource pool for the target org and working backwards to civm objects - but this is presenting its own challenges. (Note that vmadmin is a module with the Get-CISnapshot function).