May
31
PowerShell Revert to snapshot
For our test environment we need to rollback to a snapshot(default) every evening. I don’t like to do that by hand so lets create a script.
The second point it has to be run as a scheduled task and I don’t want to parse my credentials within the script as plain text.
First of all
On the machine that has to run the task, create the credentials file:
From the PowerShell command run New-VICredentialStoreItem -host ‘vccenter’ -user ‘marcus’ -password ‘garvey’ -file c:\vc_cred
Getting the credentials:
$creds = Get-VICredentialStoreItem -file c:\vc_cred
so in code it will end up as:
###################################################################
#
# 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
Thanks to A Fokkema and baars.it and secure-credential-storage