Hi,
I am new for scripting.. I am trying to create script for Vm Snapshot for multiple VM.. But i need Snapshot should take vm name automatic and create snapshot like VM1_BEFOREPATCH , VM2_BEFOREPATCH and so on..
#VM Server List
$vmlist = Get-Content C:\Servers.txt
foreach($VM in $VMlist) {
New-Snapshot -VM $vm -Name BEFOREPATCH- -description 'Patching'
}
Disconnect-VIServer -Confirm:$false
You can use variable substitution in a double-quoted string for that.
Something like this
Connect-VIServer -Server YourVC
#VM Server List
$vmlist = Get-Content C:\Servers.txt
foreach($VM in Get-VM $VMlist) {
New-Snapshot -VM $vm -Name "$($vm.Name)_BEFOREPATCH" -description 'Patching'
}
Disconnect-VIServer -Confirm:$false
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You can use variable substitution in a double-quoted string for that.
Something like this
Connect-VIServer -Server YourVC
#VM Server List
$vmlist = Get-Content C:\Servers.txt
foreach($VM in Get-VM $VMlist) {
New-Snapshot -VM $vm -Name "$($vm.Name)_BEFOREPATCH" -description 'Patching'
}
Disconnect-VIServer -Confirm:$false
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you so much for the help..
Now i would like to send mail of the created VM-Snapshot only from Server.txt . Can you help with this as i am getting all vm data.
Connect-VIServer -Server YourVC
#VM Server List
$vmlist = Get-Content C:\Servers.txt
foreach($VM in Get-VM $VMlist) {
New-Snapshot -VM $vm -Name "$($vm.Name)_BEFOREPATCH" -description 'Patching'
}
Disconnect-VIServer -Confirm:$false
# Mail Format
$body = Get-VM | Get-Snapshot | Select Name, VM | Out-String
Send-MailMessage -From "xyz.com" -To "abc.com" -SmtpServer "vmware.com" -Body $body -Subject "Snapshot Report"
You could do
# Mail Format
$body = Get-VM -Name $vmList | Get-Snapshot | Select Name, VM | Out-String
Send-MailMessage -From "xyz.com" -To "abc.com" -SmtpServer "vmware.com" -Body $body -Subject "Snapshot Report"
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks i will try and let you know
It works Perfectly thanks a lot
Hello,
Is possible to use same script to delete snapshots?
I trying but getting error, not sure if this is correct:
$vmlist = Get-Content C:\Users\gemela\Desktop\snap.txt
foreach($VM in $VMlist) {
Remove-Snapshot -VM $vm -snapshot -confirm:$false
}
Disconnect-VIServer -Confirm:$false
You have to provide a Snapshot object to Remove-Snapshot, so you would have to do something like this
$vmlist = Get-Content C:\Users\gemela\Desktop\snap.txt
foreach($VM in $VMlist) {
Get-Snapshot -VM $vm |
Remove-Snapshot -Confirm:$false
}
Disconnect-VIServer -Confirm:$false
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
It works, Thanks a lot LucD
Thanks for the reply.
Is there any way that multiple user can execute the script at the same time with different server names ? as there is only 1 txt file fro server list.
You mean by using different text files?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
What will be best solution if 5 people want to excute The script at same time on different server ?
The Get-Snapshot will not return anything, so there will be no object reaching the Remove-Snapshot cmdlet.
Should 2 scripts reach the same snapshot at the same time, the Remove-Snapshot will probably give an error.
You can try to add an -ErrorAction SilentlyContinue on the Remove-Snapshot cmdlet.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
What about adding delays or that script would go in sequence with delay or that would go to next one only when previous one (or previous "bach" of say 5) is completed? Also as an option would be bit more "intelligent" if it would monitor if set number of snapshot tasks in progress it would not progress with next ones to not "hammer" the environment too mutch.
Help appreciated. Thank you.
To only select snapshots with a specific Description, you can use a Where-clause after the Get-Snapshot cmdlet.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference