We help IT Professionals succeed at work.

Vmware revert to spesific snapshot from the cli

3XLcom
3XLcom asked
on
how should i revert the 112 id vm to spesific client for ex. to yedek6 ?
~ # vim-cmd vmsvc/snapshot.get 112
Get Snapshot:
|-ROOT
--Snapshot Name        : yedek4
--Snapshot Desciption  :
--Snapshot Created On  : 12/4/2011 4:36:33
--Snapshot State       : powered off
--|-CHILD
----Snapshot Name        : yedek3
----Snapshot Desciption  :
----Snapshot Created On  : 12/4/2011 4:36:37
----Snapshot State       : powered off
----|-CHILD
------Snapshot Name        : yedek6
------Snapshot Desciption  :
------Snapshot Created On  : 12/4/2011 4:36:55
------Snapshot State       : powered off

Open in new window

Comment
Watch Question

Network Administrator, Network Consultant
Top Expert 2011
Commented:
###################################################################
#
# ReverseToSnapshot.ps1
#
# -Get stored credentials
# -Connect to vCenter
# -Shutdown Virtual machine
# -Set to required snapshot
# -Remove other snapshots
# -Start Virtual machine again
#
# Example:
# .\ReverseToSnapshot.ps1 -vmname TIIS01.domain.com -snapshotsname Do-Not-Remove-Default 
# Version 1.0 juni 2010 JW Nieuwenhuizen ict.myjewe.nl
#
####################################################################

param(
    [parameter(Mandatory = $true)]
    [string]$vmname,
    [string]$snapshotsname
)

function PowerOff-VM{
    param([string] $vm)
        if((Get-VM $vm).powerstate -eq “PoweredOff”){
        Write-Host “$vm is already powered off”}
        else{
    Shutdown-VMGuest -VM (Get-VM $vm) -Confirm:$false | Out-Null
    Write-Host “Shutdown $vm”
    do {
        $status = (get-VM $vm).PowerState
    }until($status -eq “PoweredOff”)
    }
}

function PowerOn-VM{
    param( [string] $vm)
    if((Get-VM $vm).powerstate -eq “PoweredOn”){
        Write-Host “$vm is already powered on”}
    else{
        Start-VM -VM (Get-VM $vm) -Confirm:$false | Out-Null
        Write-Host “Starting $vm”
        do {
            $status = (Get-vm $vm | Get-View).Guest.ToolsRunningStatus
        }until($status -eq “guestToolsRunning”)
    }
}

function Remove-Snapshot-VM{
    param( [string] $vm, [string] $vm1)
    $snaps = Get-Snapshot -VM (Get-VM -Name $vm)
    foreach ($snappie in $snaps)
    {
        if ($snappie.Name -eq $vm1){
        Write-Host “This one we need again $vm1¿}
        else{
        Get-Snapshot -VM (Get-VM -Name $vm) -Name $snappie.Name | Remove-Snapshot -Confirm:$false | Out-Null
        }
    }
}

#######################################################################################
# Main script
#######################################################################################
# Import VimAutomation.Core to run from a “normal” powershell prompt
add-pssnapin VMware.VimAutomation.Core

# Get and Decript credentials and Connect vcenter Using AD credentials
$creds = Get-VICredentialStoreItem -file c:\vc_cred
    foreach ($item in $creds){
    Connect-VIServer -Server $item.Host-User $item.User -Password $item.Password
}

$poweroff = PowerOff-VM $vmname

# Set Snapshot name
$snapname = Get-Snapshot -VM (Get-VM -Name $vmname) -Name $snapshotsname
# Set VM to snapshot
Set-VM -VM $vmname -Snapshot $snapname -confirm:$false

$removesnap = Remove-Snapshot-VM $vmname $snapshotsname
$poweron = PowerOn-VM $vmname

Disconnect-VIServer -Confirm:$False

Open in new window

Author

Commented:
what is this :D
Maen Abu-TabanjehNetwork Administrator, Network Consultant
Top Expert 2011

Commented:
its a computer game :P - read the comments inside the code

Author

Commented:
Is there any way to work with vmid instead of -vmname TIIS01.domain.com
Maen Abu-TabanjehNetwork Administrator, Network Consultant
Top Expert 2011

Commented:

Author

Commented:
thnx