Automation

 View Only
  • 1.  Snapshots with PowerCLI

    Posted Sep 06, 2013 07:09 PM

    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?



  • 2.  RE: Snapshots with PowerCLI

    Posted Sep 06, 2013 10:30 PM

    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.



  • 3.  RE: Snapshots with PowerCLI

    Posted Sep 07, 2013 03:18 AM

    That is correct.



  • 4.  RE: Snapshots with PowerCLI

    Posted Sep 07, 2013 01:29 PM

    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 :smileysad:

    The same same snapshot will be taken multiple times.