VMware Cloud Community
OM_J
Contributor
Contributor
Jump to solution

VM Snapshot

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

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

15 Replies
LucD
Leadership
Leadership
Jump to solution

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

OM_J
Contributor
Contributor
Jump to solution

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"

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

OM_J
Contributor
Contributor
Jump to solution

Thanks i will try and let you know

Reply
0 Kudos
OM_J
Contributor
Contributor
Jump to solution

It works Perfectly thanks a lot

Reply
0 Kudos
antoniogemelli
Hot Shot
Hot Shot
Jump to solution

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 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

antoniogemelli
Hot Shot
Hot Shot
Jump to solution

It works, Thanks a lot LucD

Reply
0 Kudos
OM_J
Contributor
Contributor
Jump to solution

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.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You mean by using different text files?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
OM_J
Contributor
Contributor
Jump to solution

What will be best solution if 5 people want to excute  The script at same time on different server ?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
JuXas
Contributor
Contributor
Jump to solution

Hi, What about removing just a specific snapshot that matches description (used previously)
Reply
0 Kudos
JuXas
Contributor
Contributor
Jump to solution

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.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos