none
Deleting a Snapshot with Powershell RRS feed

  • Question

  • I have been writing scripts to automatically take snapshots everynight.  Those work fine but I also need the ability to delete snapshots via powershell and that is where my problem is.  Here is my deletion script:

    # Prompt for the Hyper-V Server to use
    $HyperVServer = 'Hyperv Machine'
     
    # Prompt for the virtual machine to use
    $VMName = '01-vm'
     
    # Prompt for the name of the snapshot to delete
    $SnapshotName = 'DailyBackup'
     
    # Get the management service
    $VMMS = gwmi -namespace root\virtualization Msvm_VirtualSystemManagementService -computername $HyperVServer
     
    # Get the virtual machine object
    $VM = gwmi MSVM_ComputerSystem -filter "ElementName='$VMName'" -namespace "root\virtualization" -computername $HyperVServer
     
    # Find the snapshot that we want to delete
    $Snapshot = gwmi -Namespace root\virtualization -Query "Associators Of {$VM} Where AssocClass=Msvm_ElementSettingData ResultClass=Msvm_VirtualSystemSettingData" | where {$_.ElementName -eq $SnapshotName} | select -first 1
     
    # Delete the snapshot
    $result = $VMMS.RemoveVirtualSystemSnapshot($Snapshot)

    I got this script from here : http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/10/20/deleting-a-snapshot-via-powershell-hyper-v.aspx

    My problem is when I hit the line 

    $Snapshot = gwmi -Namespace root\virtualization -Query "Associators Of {$VM} Where AssocClass=Msvm_ElementSettingData ResultClass=Msvm_VirtualSystemSettingData" | where {$_.ElementName -eq $SnapshotName} | select -first 1

    I get the error gwmi: not supported

    All help is greatly appreciated!!

    Tuesday, March 26, 2013 2:25 PM

Answers

  • You have Hyper-V 2008 R2. 

    You therefore need to delete the snapshots using WMI direct to the Hyper-V Server where the VM is, or you must use SCVMM.  AND, you MUST power off your VMs after deleting the snapshots (unless you are reverting).

    In this thread you have been given the Hyper-V 2012 cmdlet commands and the SCVMM cmdlet commands.

    Personally, if you have SCVMM, why not use it?

    Your error in the WMI script is in this portion of the pipeline:

    gwmi -Namespace root\virtualization -Query "Associators Of {$VM} Where AssocClass=Msvm_ElementSettingData ResultClass=Msvm_VirtualSystemSettingData"

    The assumption of this command is where you are running the script.  It assumes that you are running the script locally on the Hyper-V Server itself, in spite of previously asking you and the previous lines targeting a remote system.

    Try this:

    $Snapshot = gwmi -Namespace root\virtualization -Query "Associators Of {$VM} Where AssocClass=Msvm_ElementSettingData ResultClass=Msvm_VirtualSystemSettingData" -computername $HyperVServer | where {$_.ElementName -eq $SnapshotName} | select -first 1
    

    Notice that I added the -computername



    Brian Ehlert
    http://ITProctology.blogspot.com
    Learn. Apply. Repeat.
    Disclaimer: Attempting change is of your own free will.

    • Marked as answer by Billyj82 Tuesday, April 2, 2013 2:03 PM
    Monday, April 1, 2013 9:05 PM
    Moderator

All replies

  • Why not use the "Get-VMCheckpoint" and "Remove-VM Checkpoint" commands?

    Your script would look something like:

    $cp = Get-VMCheckpoint -VM 01-vm
    Remove-VMCheckpoint -vmcheckpoint $cp


    Tuesday, March 26, 2013 2:57 PM
  • *-VMCheckpoint is SCVMM (prior to 2012).

    *-VMSnapshot is Hyper-V 2012.

    If you are using Hyper-v 2012, use the built in cmdlets, not the WMI - much easier and safer.

    and gwmi - get-WmiObject - should always be supported unless the class you are calling is miss-typed.  Or, your $snapshot is being returned empty.


    Brian Ehlert
    http://ITProctology.blogspot.com
    Learn. Apply. Repeat.
    Disclaimer: Attempting change is of your own free will.

    Tuesday, March 26, 2013 3:10 PM
    Moderator
  • Ok I tried your

    $cp = Get-VMCheckpoint -VM 01-vm
    Remove-VMCheckpoint -vmcheckpoint $cp

     If I open powershell inside of SCVMM that script works fine.  But if I open powershell outside the manager and  "Add-PSSnapin microsoft.systemcenter.virtualmachinemanager" and run the same script I get

    Cannot bing parameter 'VM'. Cannot convert value '01-vm' to type "Microsoft.SystemCenter.VirtualMachineManager.VM". Error: "VMM is unable to perform this operation without a
    connection to a Virtual Machine Manager server.

    So I added

    $cp = Get-VMCheckpoint -VMMSERVER "01-VMServer.domain.com" -VM 01-vm
    

    Then it freezes up and just sits there

    Thursday, March 28, 2013 4:53 PM
  • Using the Hyper-V module from outside VMM, it would be:

    $cp = Get-VMSnapShot -VMName -01-vm -ComputerName 01-vmserver.domain.com

    Remove-VMSnapshot $cp




    Friday, March 29, 2013 2:15 AM
  • When I try that I get this error:
    Get-VMSnapshot : The Hyper-V role is not installed on the destination host. Add the Hyper-V role on that host and then
    run this cmdlet again.

    Yes I did Add-PSSnapin Microsoft.systemcenter.virtualmachinemanager first.  I am trying to run this on the machine that has SCVMM installed on, not on the hyper-v machines themselves.  

    It appears my problem is with connecting to the VMMServer because whenever I add that to the script I get no errors but it just stops.  So I ran
    Get-VMMServer -ComputerName "01-VMServer.domain.com"

    I got an error stating this

    Get-VMMServer : Unable to connect to the Virtual Machine Manager server 01-vmserver.domain.com. The attempt to
    connect timed out. (Error ID: 1603)

    I can ping the vmm server so I'm not sure what the issue is.


    • Edited by Billyj82 Friday, March 29, 2013 2:53 PM
    Friday, March 29, 2013 1:35 PM
  • Get-VMSnapshot is a Hyper-V cmdlet and requires that Hyper-V be installed on the target server. (direct to a Hyper-V Server)

    (this is part of the Hyper-V Tools)

    Get-SCVMCheckpoint is an SCVMM cmdlet and would be used to target the SCVMM Server.

    (this is installed with the SCVMM Console)

    If you open the SCVMM PowerShell console, it automatically loads of the SCVMM modules for you.  So we always recommend doing it this way when possible.

    The user that you are logged in as, must also be an SCVMM Server administrator.

    Have you tried performing this at the SCVMM Server console?  Using the SCVMM PowerShell console.


    Brian Ehlert
    http://ITProctology.blogspot.com
    Learn. Apply. Repeat.
    Disclaimer: Attempting change is of your own free will.

    Friday, March 29, 2013 3:10 PM
    Moderator
  • This process will be automated daily so opening scvmm and clicking the powershell button will not work.  However I did test that way.  If I open scvmm 2012 and click the powershell button this script works.

    $cp = Get-VMCheckpoint -VM 01-vm Remove-VMCheckpoint -vmcheckpoint $cp

    But if I open powershell outside of SCVMM and use "Add-PSSnapin Microsoft.systemcenter.virtualmachinemanager" and use the same code it says no Virtual Machine Manager is identified.  So once again from outside SCVMM I opened powershell and use

    $VMM = Get-VMMServer -ComputerName "01-VMMServer.domain.com"

    I get a timed out error.  Powershell Error 1603.

    I tried Get-SCVMCheckpoint and it failed instantly saying it could not find anything for that.

    Friday, March 29, 2013 8:05 PM
  • add-pssnapin no longer works as it became a module with the 2012 release.

    Import-Module instead.

    "Get-Module -ListAvailable" shows the registered ones.


    Brian Ehlert
    http://ITProctology.blogspot.com
    Learn. Apply. Repeat.
    Disclaimer: Attempting change is of your own free will.

    Friday, March 29, 2013 8:08 PM
    Moderator
  • Yes to expand the above:

    import-module Hyper-V
    $cp = Get-VMSnapShot -VMName -01-vm -ComputerName 01-vmserver.domain.com
    Remove-VMSnapshot $cp
    
    

    Friday, March 29, 2013 8:29 PM
  • Now I get this error:

    Get-VMSnapShot : The Hyper-V role is not installed on the destination host. Add the Hyper-V role on that host and then
    run this cmdlet again.

    I am positive the servers the servers have the hyper-v role activated.  They are server 2008 R2.  Would that make a difference?  Our SCVMM is 2012 and hyper-v's are 2008 R2.

    Monday, April 1, 2013 1:13 PM
  • Only hyper-v 2012 supports the hyper-v cmdelts (which you are trying to use).


    Brian Ehlert
    http://ITProctology.blogspot.com
    Learn. Apply. Repeat.
    Disclaimer: Attempting change is of your own free will.

    Monday, April 1, 2013 1:43 PM
    Moderator
  • So I can create them and not delete them?  I have already successfully created them on the scvmm server through powershell.  There has to be a way to delete them.  It doesn't not matter to me if they are created/deleted on the 2008 r2 hyper-v server or the 2012 SCVMM Server.  If the commands only work for 2012, how to you delete snapshots from a 2008 server?
    Monday, April 1, 2013 8:18 PM
  • You have Hyper-V 2008 R2. 

    You therefore need to delete the snapshots using WMI direct to the Hyper-V Server where the VM is, or you must use SCVMM.  AND, you MUST power off your VMs after deleting the snapshots (unless you are reverting).

    In this thread you have been given the Hyper-V 2012 cmdlet commands and the SCVMM cmdlet commands.

    Personally, if you have SCVMM, why not use it?

    Your error in the WMI script is in this portion of the pipeline:

    gwmi -Namespace root\virtualization -Query "Associators Of {$VM} Where AssocClass=Msvm_ElementSettingData ResultClass=Msvm_VirtualSystemSettingData"

    The assumption of this command is where you are running the script.  It assumes that you are running the script locally on the Hyper-V Server itself, in spite of previously asking you and the previous lines targeting a remote system.

    Try this:

    $Snapshot = gwmi -Namespace root\virtualization -Query "Associators Of {$VM} Where AssocClass=Msvm_ElementSettingData ResultClass=Msvm_VirtualSystemSettingData" -computername $HyperVServer | where {$_.ElementName -eq $SnapshotName} | select -first 1
    

    Notice that I added the -computername



    Brian Ehlert
    http://ITProctology.blogspot.com
    Learn. Apply. Repeat.
    Disclaimer: Attempting change is of your own free will.

    • Marked as answer by Billyj82 Tuesday, April 2, 2013 2:03 PM
    Monday, April 1, 2013 9:05 PM
    Moderator
  • That worked thanks Brian.  I tried using the powershell for SCVMM and it failed everytime when I would call the computer name so I guess until we update our hyper-v servers to 2012 I will have to use the GWMI style.  Thank you for the help!!
    Tuesday, April 2, 2013 2:04 PM