PowerCLI

 View Only
  • 1.  Search for Snapshots using PowerCli

    Posted Oct 11, 2012 01:16 PM

    Hi There

    I'm trying to write a script which can find snapshots taken from 3 ESX servers hosting Windows, which should display all the snapshots and give me an option to select and delete snapshots then restart the servers.

    Thank you.

    Sache.



  • 2.  RE: Search for Snapshots using PowerCli

    Posted Oct 12, 2012 04:17 AM

    Hello, Sache35-

    Welcome to the communities.

    As for getting the snapshots for VMs running on three particular VMHosts, you could use something like:

    Get-VMHost myHost0,myHost1,myHost2 | Get-VM | Get-Snapshot | select VM, Name, Description, Created

    That would return some info like:

    VM       Name            Description   Created
    --       ----            -----------   -------
    myVM03   test_toRemove   test snap     7/3/2012 11:28:35 PM

    You could export that info to CSV or something for later review as desired (via Export-Csv).  As for the selecting and deleting snapshots, if you did export the info to a CSV, you could go edit the CSV, deleting rows for snapshots that you want to keep, leaving rows of snapshots to delete.

    You could then import that CSV (Import-Csv) and use it as the data for a loop involving the Remove-Snapshot cmdlet.  If you need/want to reboot the given VMs after deleting their snapshots, you could use the Restart-VMGuest cmdlet in the same loop.

    How does that sound?



  • 3.  RE: Search for Snapshots using PowerCli

    Posted Oct 15, 2012 02:17 PM

    Hi Matt

    Greatly appreciate for your answer. Is there a solution where i can automate export VM's to CSV then import CSV? Also i want to take snapshot of the all servers after reboot.

    Thanks,

    Sache



  • 4.  RE: Search for Snapshots using PowerCli

    Posted Oct 19, 2012 04:04 PM

    Hello, Sache-

    Yes, you can export VM info (any info, really) to CSV, and then later import it.  Example:

    Get-VMHost myHost0,myHost1,myHost2 | Get-VM | Select Name,PowerState,NumCpu,MemoryGB | Export-Csv c:\temp\myVMInfo.csv -UseCulture -NoTypeInformation

    To later, import the VM info from the CSV, you would use Import-Csv:

    $arrMyVMInfo = Import-Csv c:\temp\myVMInfo.csv

    This results in an array ("$arrMyVMInfo") that contains Name, PowerState, Num CPUs, and MemoryGB info for each VM that was gotten previously.  (As you know, you should be able to just save the VM info directly into a variable for use in the same PowerShell session instead of the export-to-CSV and import-from-CSV exercise).

    To use that imported info to reboot guests quickly and then take snapshots, you could do something like:

    ## restart the guest in each of the given VMs (requires VMware Tools to be running in each guest)
    Restart-VMGuest ($arrMyVMInfo | %{$_.Name}) -Confirm:$false -WhatIf

    ## take a snapshot of each VM after Tools are running (waits up to two minutes for tools to be started)
    $arrMyVMInfo | %{
       
    if (Wait-Tools -VM $_.Name -TimeoutSeconds 120) {
           
    New-Snapshot -VM $_.Name -Name "snapshot0" -Description "snapshot after reboot" -Memory -Quiesce -Confirm:$false -WhatIf
        }
    ## end if
        else {"Tools did not start up on VM '$($_.Name)' in given amount of time.  Snapshot not taken."}
    }
    ## end foreach-object

    Note, that restarts all of the given VMs at the same time, so be mindful of the resources in use by other machines at the time.  And, the code here contains two (2) -WhatIf parameters, so will only do a dry-run as written.  To actually restart all of the given guests, and to take snapshots after they are back up, take off the two -WhatIf params (on Restart-VMGuest and on New-Snapshot).  Also, since boot times may vary, you might wait a bit before running the second part that does the snapshots, or adjust the -TimeoutSeconds param on Wait-Tools.

    That do it for you?



  • 5.  RE: Search for Snapshots using PowerCli

    Posted Oct 19, 2012 04:25 PM

    Hi Guys,

      also have in mind that some snapshots does not have to be included by : get-snapshot . Most of the time it will work but sometimes ... well ... it happens. So you will have to add another option to look for snapshots by vmdk names.

    You can use this line to be 100% that vm is or is not running from snapshot

    foreach($vmview in get-view -viewtype virtualmachine){ $vmview.Config.Hardware.Device | ? {$_ -is [VMware.Vim.VirtualDisk]} | %{$_.Backing | select @{N="VMname";E={$vmview.name}},Filename |?{$_.FileName.Split('/')[-1] -match ".*\-[0-9]{6}\.vmdk"} }  }