VMware Cloud Community
shawnmininger
Contributor
Contributor
Jump to solution

Create VM snapshots only on hosts listed in csv?

I am trying to write a script that will create snapshots for all VMs on specific hosts.  I was thinking I could put a list of hosts in a .csv file so that I could change which host VMs I want this to run against just by editing the .csv file.  I am having a brain fart on how to do it.  Help?  This is what I have so far:

connect-viserver xx -user xx -password xx
Write-Host " "
Write-Host "The creation of snapshots for pod VMs is beginning." -foregroundcolor blue -backgroundcolor yellow
Write-Host " "
$snapname = "base_05.16.2012"
$vmcsv = import-csv C:\lab_scripts\snapshot\list_vm_snapshot.csv
$vms = get-vm -location $line.hostname
foreach ($vm in $vms)
{
Get-VM $vm -location $line.hostname | new-snapshot -name $snapname
}
disconnect-viserver vcenter01.vmware.lab -confirm:$FALSE
Write-Host " "
Write-Host "The creation of snapshots for pod VMs is complete." -foregroundcolor blue -backgroundcolor yellow
Write-Host " "
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Give it a try like this

connect-viserver xx -user xx -password xx
Write-Host " " 
Write-Host "The creation of snapshots for pod VMs is beginning." -foregroundcolor blue -backgroundcolor yellow
Write-Host
" "
$snapname
= "base_05.16.2012"
$vmcsv
= import-csv C:\lab_scripts\snapshot\list_vm_snapshot.csv
foreach
($line in $vmcsv) {   Get-VMHost -Name $line.hostname | Get-VM | new-snapshot -name $snapname -Confirm:$false
}
disconnect-viserver vcenter01.vmware.lab -confirm:$FALSE
Write-Host
" "
Write-Host
"The creation of snapshots for pod VMs is complete." -foregroundcolor blue -backgroundcolor yellow
Write-Host
" "


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

View solution in original post

Reply
0 Kudos
11 Replies
CRad14
Hot Shot
Hot Shot
Jump to solution

You don't need a csv... you could just as easily use just a plain txt file with the host names on seperate lines, and then using a get-content instead of a import-csv

Also you don't need to do the get-vm twice

$vms = get-vm -location $line.hostname
foreach ($vm in $vms)
{
$vm | new-snapshot -name $snapname
}

Would work as well for those lines, since you have already pulled the VMs.... Also I don't think you need the loop in there either

From directly above and condensing it into just

Get-VM -location $line.hostname | new-snapshot -name $snapname

I believe that should work as well and it eliminates the foreach vm loop.

Also the way you currently have it setup  you would need to do a host foreach loop

foreach($line in $vmcsv)

{

Get-VM -location $line.hostname | new-snapshot -name $snapname

}

Hope this helps


Conrad www.vnoob.com | @vNoob | If I or anyone else is helpful to you make sure you mark their posts as such! 🙂
shawnmininger
Contributor
Contributor
Jump to solution

Hmm, for some reason I'm not following, lol, might need more coffee.  I have 20 VMs per host.  I already have a script that clones those VMs from a set of templates.  That part works great.  Now I just want to have the script create a snapshot for any an all VMs on that host....but I also want the ability to have it do this form multiple hosts based on a list in the .csv file.  How does this look?

connect-viserver xx -user xx -password xx
Write-Host " "
Write-Host "The creation of snapshots for pod VMs is beginning." -foregroundcolor blue -backgroundcolor yellow
Write-Host " "
$snapname = "base_05.16.2012"
$vmcsv = import-csv C:\lab_scripts\snapshot\list_vm_snapshot.csv
$vms = get-vm -location $line.hostname
foreach ($vm in $vms)
{
$vm | new-snapshot -name $snapname
}
disconnect-viserver vcenter01.vmware.lab -confirm:$FALSE
Write-Host " "
Write-Host "The creation of snapshots for pod VMs is complete." -foregroundcolor blue -backgroundcolor yellow
Write-Host " "
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Give it a try like this

connect-viserver xx -user xx -password xx
Write-Host " " 
Write-Host "The creation of snapshots for pod VMs is beginning." -foregroundcolor blue -backgroundcolor yellow
Write-Host
" "
$snapname
= "base_05.16.2012"
$vmcsv
= import-csv C:\lab_scripts\snapshot\list_vm_snapshot.csv
foreach
($line in $vmcsv) {   Get-VMHost -Name $line.hostname | Get-VM | new-snapshot -name $snapname -Confirm:$false
}
disconnect-viserver vcenter01.vmware.lab -confirm:$FALSE
Write-Host
" "
Write-Host
"The creation of snapshots for pod VMs is complete." -foregroundcolor blue -backgroundcolor yellow
Write-Host
" "


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

Reply
0 Kudos
shawnmininger
Contributor
Contributor
Jump to solution

Wow, thanks!  That actually worked faster than the other method I was using!

Reply
0 Kudos
bseymour
Contributor
Contributor
Jump to solution

Can you edit this so that it looks at a csv file or txt file that has a list of server names that I want to snapshot instead of saying an entire host?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sure, the central loop should be something like this

$vmcsv = import-csv C:\vmnames.csv 
foreach($line in $vmcsv)
{
  Get-VM -Name $line.VMname | new-snapshot -name $snapname -Confirm:$false
}

Note that you will have to have a column named VMname in the CSV file.


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

Reply
0 Kudos
bseymour
Contributor
Contributor
Jump to solution

Connect-VIServer XXX

Write-Host " "
Write-Host "The creation of snapshots for pod VMs is beginning." -foregroundcolor blue -backgroundcolor yellow
Write-Host " "
$snapname = "test_01.08.2013"
$vmcsv = import-csv C:\lab_scripts\snapshot\list_vm_snapshot.csv
foreach($line in $vmcsv)
{
  Get-VM -Name $line.vmname | Get-VM | new-snapshot -name $snapname -Confirm:$false
}
disconnect-viserver XXX -confirm:$FALSE
Write-Host " "
Write-Host "The creation of snapshots for pod VMs is complete." -foregroundcolor blue -backgroundcolor yellow
Write-Host " "
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

This

Get-VM -Name $line.vmname | Get-VM | new-snapshot -name $snapname -Confirm:$false

should be

Get-VM -Name $line.vmname | new-snapshot -name $snapname -Confirm:$false


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

Reply
0 Kudos
bseymour
Contributor
Contributor
Jump to solution

Awesome that worked great. Can there be a line added to check to see if there is snap already and if so to delete it before creating a new one?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

This is the central loop for this

foreach($line in $vmcsv)
{
  $vm = Get-VM -Name $line.VMname
 
$snap = Get-Snapshot -VM $vm -Name $snapname -ErrorAction SilentlyContinue
  if($snap){     Remove-Snapshot -Snapshot $snap -Confirm:$false
  }   New-Snapshot -VM $vm -Name $snapname -Confirm:$false
}

The script tries to get the snapshot for the VM. If there is such a snapshot then $snap will not be $null, and the code removes the snapshot.

The ErrorAction parameter is used to avoid an error message when there is no such snapshot


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

Reply
0 Kudos
bseymour
Contributor
Contributor
Jump to solution

That worked great! I was also able to take that and create a new one that uses the same list and deletes the snapshots. I can use this for maintenance now.

Connect-VIServer vcenter -User XXX -Password XXX
Write-Host " "
Write-Host "The creation of snapshots for pod VMs is beginning." -foregroundcolor blue -backgroundcolor yellow
Write-Host " "
$vmcsv = import-csv C:\scripts\list_vm_snapshot.csv
foreach($line in $vmcsv)
{
  $vm = Get-VM -Name $line.VMname
  $snap = Get-Snapshot -VM $vm -Name $snapname -ErrorAction SilentlyContinue
  if($snap){
    Remove-Snapshot -Snapshot $snap -Confirm:$false
  }
}
disconnect-viserver vcenter -confirm:$FALSE
Write-Host " "
Write-Host "The creation of snapshots for pod VMs is complete." -foregroundcolor blue -backgroundcolor yellow
Write-Host " "
Reply
0 Kudos