OM_J
Contributor
Contributor

VM Snapshot

Jump to solution

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

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

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

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"

0 Kudos
LucD
Leadership
Leadership

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

Thanks i will try and let you know

0 Kudos
OM_J
Contributor
Contributor

It works Perfectly thanks a lot

0 Kudos

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 

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
OM_J
Contributor
Contributor

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.

0 Kudos
LucD
Leadership
Leadership

You mean by using different text files?


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

0 Kudos
OM_J
Contributor
Contributor

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

0 Kudos
LucD
Leadership
Leadership

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

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

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.

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos