PowerCLI

 View Only
  • 1.  Get snapshot status from CLI

    Posted Jan 24, 2015 04:05 AM


    Hey guys, is there a way to get the current status of a snapshot? Similar to how the GUI shows a percentage.


    I'm using the -runasync command to kick off a bunch of snapshots but would like to be able to query them and see what they're up to.


     


    Thanks.



  • 2.  RE: Get snapshot status from CLI

    Posted Jan 24, 2015 06:35 AM

    When you use the Get-Task cmdlet, the returned Task object has a property called PercentComplete.

    Something like this

    $task = Get-vm MyVM | Get-Snapshot | Remove-Snapshot -Confirm:$false -RunAsync

    while ($Task.State -eq 'Running'){

        $task = Get-Task -Id $task.Id

        $task | Select State,PercentComplete

    }



  • 3.  RE: Get snapshot status from CLI

    Posted Jan 24, 2015 12:34 PM

    Hi LucD, thanks for that. My PS skills are pretty weak and although I managed to use your code to create and monitor a snapshot, I'm not sure how I could do that for multiples?

    At the moment, I'm importing a CSV which contains a list of VMs to snapshot.

    Fairly simple stuff:

    $vms2snap=import-csv c:\temp\test_snapshot1.csv

    foreach ($machine in $vms2snap) {

    $vm_to_snap = $($machine.'computer')

    get-vm -name $vm_to_snap | new-snapshot -name snaptest -Memory -RunAsync

    }

    Do you know how I could then go through and check the status of the snapshots? Just not sure how to incorporate your code into it.

    Thanks.



  • 4.  RE: Get snapshot status from CLI

    Posted Jan 25, 2015 09:52 AM

    Try something like this.

    It first starts all the snapshots in background mode, and then in a loop, checks every 5 seconds for the status of the snapshots.

    $taskTab = @{}

    $vms2snap = Import-Csv c:\temp\test_snapshot1.csv -UseCulture | Select -ExpandProperty Computer

    foreach ($machine in $vms2snap) {

        $task = Get-VM -Name $machine | New-Snapshot -Name snaptest -Memory -RunAsync

        $taskTab.Add($task.Id,$machine)

    }

    while($taskTab.Count -ne 0){

        $keys = @()

        $taskTab.GetEnumerator() | %{

            $newState = Get-Task -Id $_.Key

            if($newState.State -eq 'Running'){

                Write-Output "$($_.Value) - At $($newState.PercentComplete)%"

            }

            else{

                Write-Output "$($_.Value) Finished - $($newState.State)"

                $keys += $_.Key

            }

        }

        $keys | %{

            $taskTab.Remove($_)

        }

        sleep 5

    }



  • 5.  RE: Get snapshot status from CLI

    Posted Jan 25, 2015 10:36 AM

    Mate that's awesome! Thank you so much.