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.
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 $_ }
Try get-vm $servername | get-snapshot $snapshotname | set-vm -vm (get-vm $servername)
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 `
}
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
get-snapshot | where ......
Is there a way to determine what the property names are for the snapshots?
get-snapshot | where ......
Is there a way to determine what the property names are for the snapshots?
Try get-vm | get-snapshot | get-member
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)
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..."
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 $_ }
Thank you very much. That worked like a champ.