Automation

 View Only
  • 1.  Basic powershell command to revert to snapshot

    Posted Feb 20, 2011 06:27 PM

    I am starting to learn powershell and have pretty much gone through the entire guide.  The only command I am strugling with is reverting a snapshot.

    The VM is currently off

    When I try to do what the guide says

    $VMs = Get-VM -Location ( Get-ResourcePool DevelopmentResources )

    foreach( $vm in $VMs ) { Set-VM -VM $vm -Snapshot ( Get-Snapshot -VM $vm -Name

    InitialDevConfig ) }

    It replies Unexpected token 'in' in expression or statement

    I have been following the guide so the names should work.  I am unsure where it is pulling $vm as it does not have me specify it in the vsphere Admin guide I have be following

    When I try

    Found something on VMTN that says try:

    Set-VM -Snapshot nameofsnap -VM name of vm -Confirm:$falseVM

    But when I try I get:

    I Get cannot bind parameter 'Snapshot'.  Cannot convert the "snapshot1" value of the type .............

    I know there are some complex scripts out there that I could copy and paste but at this point I am trying to learn

    I have checked snapshot manager to make sure the snapshot is listed.

    Thanks in advance, very frustrated.



  • 2.  RE: Basic powershell command to revert to snapshot

    Posted Feb 20, 2011 07:38 PM

    I have tried your first script and it works for me. So I don't think there is something wrong with that.

    In the second script you can't use a name of a snapshot behind the -Snapshot parameter. You have to supply a SnapshotImpl object. One that you get from the Get-Snapshot cmdlet. Like you do in the first script. Behind the -VM parameter you can give the name of a VM or a VirtualMachineImpl object that you get from the Get-VM cmdlet. So the -VM parameter and the -Snapshot parameter of the Set-VM cmdlet behave different.

    You can rewrite your first script using the pipeline like this:

    Get-ResourcePool DevelopmentResources | `
    Get-VM | `
    ForEach-Object {
      Set-VM -VM $_ -Snapshot (Get-Snapshot -VM $_ -Name InitialDevConfig)
    }
    
    

     

    Regards, Robert



  • 3.  RE: Basic powershell command to revert to snapshot

    Posted Feb 22, 2011 01:24 AM

    That  worked great.

    Is there something I can add to the end of the script so I do not have to add Y to confirm?



  • 4.  RE: Basic powershell command to revert to snapshot
    Best Answer

    Posted Feb 22, 2011 02:48 AM

    Here you go:

    Get-ResourcePool DevelopmentResources | `
    Get-VM | `
    ForEach-Object {
      Set-VM -VM $_ -Snapshot  (Get-Snapshot -VM $_ -Name InitialDevConfig ) -Confirm:$false
    }