PowerCLI

 View Only
  • 1.  Revert to Current Snapshot VIA PowerCLI

    Posted Jan 28, 2014 06:43 PM

    I have beat myself up long enough trying to find the answer. What is the syntax  in PowerCli to revert to the current (last) snapshot. I have a pool of 50 machines I need to do this on regularly. I may not know the name of the snapshot or the name may not be the same for all machines.

    Thanks in advance!

    Jason



  • 2.  RE: Revert to Current Snapshot VIA PowerCLI

    Posted Jan 28, 2014 07:20 PM

    Something like this

    foreach($vm in Get-VM){
       
    $snap = Get-Snapshot -VM $vm | Sort-Object -Property Created -Descending | Select -First 1
       
    Set-VM -VM $vm -SnapShot $snap -Confirm:$false
    }


  • 3.  RE: Revert to Current Snapshot VIA PowerCLI

    Posted Jan 28, 2014 07:59 PM

    I was thinking of something similar lately where I needed to find the current snapshot in a snapshot tree, i.e. not just a single chain. For this special requirement the creation date didn't work and I ended up using the "isCurrent" property. So for the script provided above you may optionally use:

    $snap = Get-Snapshot -VM $vm | where {$_.IsCurrent -eq $true}

    André



  • 4.  RE: Revert to Current Snapshot VIA PowerCLI

    Posted Jan 28, 2014 09:07 PM

    I've tried it both ways with a lot of different variants. Neither work so far.  The second worked unless there was more then one VM in the folder. I am by no means a scripting wiz kid. Probably something simple.

    connect-viserver servername

    $vm = Get-Folder -Name "TEST" |Get-vm

    foreach($vm in Get-VM)

    {$snap = Get-Snapshot -VM $vm | Sort-Object -Property Created -Descending | Select -First 1

    Set-VM -VM $vm -SnapShot $snap -Confirm:$false

    connect-viserver servername

    $vm = Get-Folder -Name "TEST" |Get-vm

    foreach($vm in Get-VM)

    $snap = Get-Snapshot -VM $vm | where {$_.IsCurrent -eq $true}

    Set-VM -VM $vm -SnapShot $snap -Confirm:$false



  • 5.  RE: Revert to Current Snapshot VIA PowerCLI

    Posted Jan 28, 2014 09:13 PM

    Your code should look like this

    connect-viserver servername
    foreach($vm in (Get-Folder -Name "TEST" |Get-vm)){
     
    $snap = Get-Snapshot -VM $vm | Sort-Object -Property Created -Descending | Select -First 1
     
    Set-VM -VM $vm -SnapShot $snap -Confirm:$false
    }

    Aren't you getting any error messages when you run the code you included ?



  • 6.  RE: Revert to Current Snapshot VIA PowerCLI

    Posted Jan 28, 2014 09:26 PM

    LucD - I ran the code you provided above and this is the error message received.

    Select-Object : A parameter cannot be found that matches parameter name 'VM'. At line:1 char:152 + foreach($vm in (Get-Folder -Name "TEST" |Get-vm)){$snap = Get-Snapshot -VM $v m | Sort-Object -Property Created -Descending | Select -First 1 Set-VM -VM <<<<   $vm -SnapShot $snap -Confirm:$false}     + CategoryInfo          : InvalidArgument: (:) [Select-Object], ParameterB   indingException     + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Comm   ands.SelectObjectCommand Select-Object : A parameter cannot be found that matches parameter name 'VM'. At line:1 char:152 + foreach($vm in (Get-Folder -Name "TEST" |Get-vm)){$snap = Get-Snapshot -VM $v m | Sort-Object -Property Created -Descending | Select -First 1 Set-VM -VM <<<<   $vm -SnapShot $snap -Confirm:$false}     + CategoryInfo          : InvalidArgument: (:) [Select-Object], ParameterB   indingException     + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Comm   ands.SelectObjectCommand



  • 7.  RE: Revert to Current Snapshot VIA PowerCLI
    Best Answer

    Posted Jan 28, 2014 09:33 PM

    It looks like your copy/paste of the code went wrong.

    Are you by any chance using an older IE browser ?

    In any case I attached the code in a file.



  • 8.  RE: Revert to Current Snapshot VIA PowerCLI

    Posted Jan 28, 2014 10:05 PM

    Must have been a C&P issue. It's working and I am learning!

    I appreciate all your help on this. I do have one more thought. Is there anyway to restore more then one machine at a time? My thoughts are it will take a really long time to do 50 restores if each restore waits on the previous restore to complete.

    Possibly with a variable ? Say 5 at a time?

    $concurrentrestore = 5

    What would something like that look like?



  • 9.  RE: Revert to Current Snapshot VIA PowerCLI

    Posted Jan 28, 2014 10:14 PM

    The easiest solution, where you let vSphere handle the parallelism, is to add the -RunAsync switch on the Set-VM cmdlet.

    Then the script will not wait till the action is finished, but will continue with the next line in the script.

    The vCenter control how many of these tasks can run in parallel.

    You can code a limit in your script, but that requires some more coding.

    See for example my About Async tasks, the Get-Task cmdlet and a hash table post.

    Depending on the vSphere version you are running, that script will require some changes though.



  • 10.  RE: Revert to Current Snapshot VIA PowerCLI

    Posted Jan 28, 2014 10:23 PM

    Thanks so much for the info.  I have bookmarked your blog. :smileyhappy: I am sure I will be a frequent visitor!