VMware Cloud Community
bseymour
Contributor
Contributor

Snapshots with PowerCLI

I use this script to create snapshot of servers from an excel list. Can someone help with changing this so that it looks at a list of IP Addresses instead?

ScreenHunter_02 Sep. 06 15.06.gif

Reply
0 Kudos
3 Replies
LucD
Leadership
Leadership

Checking to see if I understood the question.

You want to find the VMs based on their IP addresses, which are in the CSV ?

And once you have the VM, do the same snapshot removal/creation steps.


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

Reply
0 Kudos
bseymour
Contributor
Contributor

That is correct.

Reply
0 Kudos
LucD
Leadership
Leadership

Try something like this

The link between the VMs and the IP addresses is stored in a hash table for easy reference.

$ipTab = @{}
foreach($vm in (Get-VM | Where {$_.Guest.IPAddress})){
   
$vm.Guest.IPAddress | %{
       
$ipTab.Add($_,$vm)
    }
}
$snapName = "maintenance_mm_dd_yy"

Import-Csv iplist.csv -UseCulture | %{
   
if($ipTab.ContainsKey($_.IPAddress)){
       
$snap = Get-Snapshot -VM $ipTab[$_.IPAddress] -ErrorAction SilentlyContinue
       
if($snap){
           
Remove-Snapshot -Snapshot $snap -Confirm:$false
        }
       
New-Snapshot -Name $snapName -VM $ipTab[$_.IPAddress] -Confirm:$false
    }
}

Note that the script doesn't contain any extra logic to handle VMs that have more than 1 IP address Smiley Sad

The same same snapshot will be taken multiple times.


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

Reply
0 Kudos