Hi
I did get the below mentioned script created by LucD in one of the answers to a community
$VM = (Get-Content c:\temp\servers.txt)
foreach($vm in Get-VM -Name $VM){
Shutdown-VMguest -VM $vm -confirm:$false
while ($vm.ExtensionData.Runtime.PowerState -eq "poweredOn"){
Start-Sleep -Seconds 2
$vm.ExtensionData.UpdateViewData("Runtime.PowerState")
}
New-Snapshot -VM $vm -name (get-date)
Start-VM -VM $vm
}
That helps me to start, though what my requirement is :-
Shutdown a VM
Check for ping status
Once down, take snapshot with the name as of that date & time, send a confirmation e-mail, say on Monday
Power on the machine. send a confirmation e-mail.
Same script will be scheduled to run after 2 day to have another snapshot on the same machine, say on Wed.
Now on Fri, I need to take another snapshot of the name with that days date & time, once done
Delete the snapshot that was taken on Monday.
Power on the machine.
And it goes on ...as again on Mon, new snapshot after powering down & deleting the oldest snapshot i.e. taken on Wed. & then power it on send a confirmation e-mail.
LucD any help ??
I modified Luc's script according your requirements. I placed the removing of the snapshot part after starting the vm because removing a snapshot can take a long time. You want the vm to be powered off for the shortest time possible don't you? You have to modify the lines starting with Send-MailMessage and put your email addresses and smtp server at the right place.
$VM = (Get-Content c:\temp\servers.txt)
foreach ($vm in (Get-VM -Name $VM)){
Shutdown-VMGuest -VM $vm -Confirm:$false
while ($vm.ExtensionData.Runtime.PowerState -eq "poweredOn")
{
Start-Sleep -Seconds 2
$vm.ExtensionData.UpdateViewData("Runtime.PowerState")
}
$DateTime = Get-Date
New-Snapshot -VM $vm -Name $DateTime
Send-MailMessage -From user@domain.com -To user@domaincom -Subject "Snapshot $DateTime taken from VM $($VM.Name)" -SmtpServer smtpserver@domain.com
Start-VM -VM $vm
Send-MailMessage -From user@domain.com -To user@domaincom -Subject "Started VM $($VM.Name)" -SmtpServer smtpserver.domain.com
# Remove snapshots older then 3.5 days
Get-Snapshot -VM $vm |
Where-Object {$_.Created -lt $DateTime.AddDays(-3).AddHours(-12)} |
Remove-Snapshot -Confirm:$false
}
I modified the script posted by RvdNieuwendijk to add error handling
Function New-OffLineSnapShot {
param (
$sendUser = me@domain.com,
$Receiver = Someone@user.net
$SMTPServer = stmp.domain.com
)
Get-VM -Name (Get-Content c:\temp\servers.txt) | % {
Try {
Shutdown-VMGuest -VM $_ -Confirm:$false
}
Catch {
throw (New-Object System.Exception(“VM Guest shutdown failed.”,$_.Exception))
}
While ($$_.ExtensionData.Runtime.PowerState -eq "poweredOn") {
Start-Sleep -Seconds 2
$vm.ExtensionData.UpdateViewData("Runtime.PowerState")}
Try {$DateTime = Get-Date
New-Snapshot -VM $_ -Name $DateTime
Send-MailMessage -From $sendUser -To $Receiver -Subject "Snapshot $DateTime taken from VM $($_.Name)" -SmtpServer $SMTPServer
}
Catch {
Send-MailMessage -From $sendUser -To $Receiver -Subject "Snapshot $DateTime taken failed from VM $($_.Name)" -SmtpServer $SMTPServer
throw (New-Object System.Exception(“Take VM Snapshot failed.”,$_.Exception))
}
Try {
Start-VM -VM $vmSend-MailMessage -From $sendUser -To $Receiver -Subject "Started VM $($_.Name)" -SmtpServer $SMTPServer
}
Catch {
Send-MailMessage -From $sendUser -To $Receiver -Subject "Started VM $($_.Name) failed" -SmtpServer $SMTPServer
throw (New-Object System.Exception(“Start VM failed.”,$_.Exception))
}
Try {
# Remove snapshots older then 3.5 days
Get-Snapshot -VM $vm |
Where-Object {$_.Created -lt $DateTime.AddDays(-3).AddHours(-12)} |
Remove-Snapshot -Confirm:$false}
Catch {
throw (New-Object System.Exception(“Error removing old snapshots.”,$_.Exception))
}
}
RvdNieuwendijk fabiendibot
I was trying to get the snapshot list for a particular machine to check it's picking the correct snapshot by just using a part of the script running directly on powercli but it's not providing any output? Any reason why?
PowerCLI D:\> $datetime = Get-Date
PowerCLI D:\> Get-Snapshot -VM machinename | Where-Object {$_.Created -lt $datetime.AddDays(-3).AddHours(-12)