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:
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
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
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
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?
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
Wow, thanks! That actually worked faster than the other method I was using!
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?
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
Connect-VIServer XXX
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
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?
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
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.