Automation

 View Only
Expand all | Collapse all

Create VM snapshots only on hosts listed in csv?

  • 1.  Create VM snapshots only on hosts listed in csv?

    Posted May 16, 2012 02:59 PM

    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 " "


  • 2.  RE: Create VM snapshots only on hosts listed in csv?

    Posted May 16, 2012 03:09 PM

    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




  • 3.  RE: Create VM snapshots only on hosts listed in csv?

    Posted May 16, 2012 04:53 PM

    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 " "


  • 4.  RE: Create VM snapshots only on hosts listed in csv?
    Best Answer

    Posted May 16, 2012 05:01 PM

    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
    " "


  • 5.  RE: Create VM snapshots only on hosts listed in csv?

    Posted May 16, 2012 06:37 PM

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



  • 6.  RE: Create VM snapshots only on hosts listed in csv?

    Posted Jan 08, 2013 02:33 PM

    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?



  • 7.  RE: Create VM snapshots only on hosts listed in csv?

    Posted Jan 08, 2013 02:42 PM

    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.



  • 8.  RE: Create VM snapshots only on hosts listed in csv?

    Posted Jan 08, 2013 02:58 PM

    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 " "


  • 9.  RE: Create VM snapshots only on hosts listed in csv?

    Posted Jan 08, 2013 03:04 PM

    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


  • 10.  RE: Create VM snapshots only on hosts listed in csv?

    Posted Jan 08, 2013 03:08 PM

    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?



  • 11.  RE: Create VM snapshots only on hosts listed in csv?

    Posted Jan 08, 2013 03:14 PM

    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



  • 12.  RE: Create VM snapshots only on hosts listed in csv?

    Posted Jan 08, 2013 03:38 PM

    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 " "