VMware Cloud Community
jfierberg
Contributor
Contributor
Jump to solution

Reverting to a specific snapshot

get-vm $servername | get-snapshot

When I execute this cmdlet I get the listing of the snapshots for the specific vm. I want to know how to select one of the snapshots and revert back to it. The example in the hand's on lab PDF does not show how this is done.

Reply
0 Kudos
1 Solution

Accepted Solutions
admin
Immortal
Immortal
Jump to solution

I was able to select a specific snapshot using the following code:Get-VM $servername | Get-Snapshot | where { $_.name -like $snapshotname}

If I add at the end of the statement | set-vm -snapshot $_, I get "the input object cannot be bound to any parameters for the command either because the command does not take pipeline input..."

Since you're piping the snapshot in to set-vm, you don't need the -snapshot argument, but you do need the -vm argument. You can either tweak that in this way:

get-vm $servername | get-snapshot | where { $_.name -like $snapshotname } | set-vm -vm (get-vm $servername)

Or, you could do it Antonio's way, which is more efficient:

get-vm $servername | get-snapshot | where { $_.name -like $snapshotname } | % { set-vm -vm $_.vm -snapshot $_ }

View solution in original post

Reply
0 Kudos
9 Replies
admin
Immortal
Immortal
Jump to solution

Try get-vm $servername | get-snapshot $snapshotname | set-vm -vm (get-vm $servername)

Reply
0 Kudos
jfierberg
Contributor
Contributor
Jump to solution

Thanks but that did not work. I tried the code below also and it failed.

get-vm $servername | get-snapshot | foreach { `

$_.VM | set-vm -snapshot $snapshotname `

-confirm:$false `

}

Reply
0 Kudos
admin
Immortal
Immortal
Jump to solution

Well, selecting the snapshot is your algorithm. As Carter suggested, it could be something like a known name. If that is the case, then I'll offer a minor tweak on Carter's code:


get-vm $servername | get-snapshot -name LastKnownGood | % { set-vm -vm $_.vm -snapshot $_ }

If your algorithm for selecting the snapshot is more involved, then it becomes something like:


$snapshotList = get-vm $servername | get-snapshot
# whatever logic you want to apply here, whether it's selecting by date created or by description or by ordinal number or...
# and of course checking for an empty list...
$snapshotToUse = {logic here}
set-vm -vm $snapshotToUse.vm -snapshot $snapshotToUse

Reply
0 Kudos
jfierberg
Contributor
Contributor
Jump to solution

get-snapshot | where ......

Is there a way to determine what the property names are for the snapshots?

Reply
0 Kudos
admin
Immortal
Immortal
Jump to solution

get-snapshot | where ......

Is there a way to determine what the property names are for the snapshots?

Try get-vm | get-snapshot | get-member

Reply
0 Kudos
halr9000
Commander
Commander
Jump to solution

get-vm $servername | get-snapshot | foreach { `

$_.VM | set-vm -snapshot $snapshotname `

-confirm:$false `

}

PowerShell tip: (and I need to find or make good documentation on this) line termination and continuation characters are not required that often.

  • An open brace { , parenthesis ( , or square bracket [ will allow for continuation across multiple lines until the block is closed by the corresponding } ) ]

  • A trailing comma (the array operator) will allow for a line break until the next array member

  • Single and double-quotes will work as well, but I really don't recommend doing so just because its unexpected and confusing to read the code.

So you can do this without a single line-continuation character (a trailing back-tick ` )

function test {
  param (
    $firstname,
    $lastname
  )
  (get-process) [
    1
  ]
  write-host "$firstname
$lastname"
}

Hal Rottenberg

Co-Host, PowerScripting Podcast (http://powerscripting.net)

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
Reply
0 Kudos
jfierberg
Contributor
Contributor
Jump to solution

I was able to select a specific snapshot using the following code:Get-VM $servername | Get-Snapshot | where { $_.name -like $snapshotname}

If I add at the end of the statement | set-vm -snapshot $_, I get "the input object cannot be bound to any parameters for the command either because the command does not take pipeline input..."

Reply
0 Kudos
admin
Immortal
Immortal
Jump to solution

I was able to select a specific snapshot using the following code:Get-VM $servername | Get-Snapshot | where { $_.name -like $snapshotname}

If I add at the end of the statement | set-vm -snapshot $_, I get "the input object cannot be bound to any parameters for the command either because the command does not take pipeline input..."

Since you're piping the snapshot in to set-vm, you don't need the -snapshot argument, but you do need the -vm argument. You can either tweak that in this way:

get-vm $servername | get-snapshot | where { $_.name -like $snapshotname } | set-vm -vm (get-vm $servername)

Or, you could do it Antonio's way, which is more efficient:

get-vm $servername | get-snapshot | where { $_.name -like $snapshotname } | % { set-vm -vm $_.vm -snapshot $_ }

Reply
0 Kudos
jfierberg
Contributor
Contributor
Jump to solution

Thank you very much. That worked like a champ.

Reply
0 Kudos