PowerCLI

 View Only
  • 1.  Snapshot properties

    Posted Feb 01, 2018 04:37 PM

    We have several processes that manage snapshots in our environment, but we wanted to identify those snapshots that might require a little more attention when removing. Specifically, we are looking for a way to identify which snapshots had the option to capture memory set when the snapshot was taken. The get-snapshot applet shows if the Quiesced option was selected, but not the memory option.. Does anyone know of a way to identify this for a given snapshot?



  • 2.  RE: Snapshot properties

    Posted Feb 01, 2018 08:06 PM

    Hello SJVAPCD​,

    Welcome to Communities.

    There is a way to differentiate snapshots with memory vs non-memory snapshots but it may not be so straight-forward for the purpose you intend here, though you should be able to make a simple script to identify the size of the files involved, which VMs they belong to and which snapshots were taken with memory.

    Basically if you create a non-memory snapshot the .vmsn file associated with that snapshot will be relatively tiny - 19.1KB in size in 6.6  (do check default size in whatever version you are running or just assume any in KB range do not have memory) while those with memory have a .vmsn file approximately the same size as the memory attached to the VM (regardless of memory reservation).

    Below is an example of a VM with 256MB memory with 2 snapshots taken, Snapshot1 with memory, Snapshot2 without memory:

    https://docs.vmware.com/en/VMware-vSphere/6.5/com.vmware.vsphere.vm_admin.doc/GUID-38F4D574-ADE7-4B80-AEAB-7EC502A379F4.html

    Another likely easier option is to examine the .vmsd files of all VMs for any that have an entry for:

    snapshotX.type = "1"

    Only memory snapshots will have these entries.

    e.g. you could use this to identify the working directory of all VMs with memory snapshots:

    # grep type /vmfs/volumes/*/*/*.vmsd

    And then use AWK to sanitize the output to only print the VM namespace and uniq.

    Bob



  • 3.  RE: Snapshot properties

    Posted Feb 03, 2018 08:16 PM

    Hi SJVAPCD, with the info provided by Bobkin I wrote a script that provides what you need probably. I would advice the admins or yourself to move the thread to the PowerCLI forum section however.. specially if there will be questions about the script. Basically it checks if the VM's vmsd file has the entry that only VM memory snapshots have. To get to the .vmsd file we need plink.exe to get in each ESXi host involved. I tested it and it works for me. It went through few thousands of VMs in less than 4 minutes for me. If you don't have plink.exe download it to a path and adjust the script with that path. Please mark the answer as Correct if ok.. The script below is for scenarios where VMs have max one snapshot.

    # Login credentials to SSH each host. It will convert the string from secure to plain text for plink

    $user = Read-Host "ESXi Host SSH User"

    $rootpword = Read-Host "ESXi Host SSH Password" -AsSecureString

    $rootbstr = [System.Runtime.InteropServices.marshal]::SecureStringToBSTR($rootpword)

    $rootpword = [System.Runtime.InteropServices.marshal]::PtrToStringAuto($rootbstr)

    $vms = Get-View -ViewType virtualmachine | ?{$_.Snapshot -ne $null} | select Name

    $collection = @()

    foreach($vm in $vms)

        {

        $gather = "" | select VM,SnapshotName,PowerState,MemorySnapshot

        $gather.VM = Get-VM $vm.Name

        $gather.SnapshotName = Get-VM $vm.Name | Get-Snapshot | select -ExpandProperty Name

        $gather.PowerState = Get-VM $vm.Name | select -ExpandProperty PowerState

        $esx = Get-VM $vm.Name | Get-VMHost

        # Check if the SSH service is running on the host else it will start it

        Get-VMHost $esx | Get-VMHostService | Where-Object {$_.Key -eq "TSM-SSH" -and $_.Running -eq $false}|Start-VMHostService -Confirm:$false | Out-Null

        # The "echo y" command accepts the key thumbprint on behalf of plink

        echo y | C:\plink.exe -ssh $esx -l $user -pw $rootpword exit | Out-Null

        # builds the vmfs path to the vmsd file

        $path2 = Get-VM $vm.Name | %{$_.Extensiondata.LayoutEx.File | where {$_.Name -like "*.vmsd"}} |

        select @{N="Path2";E={(($_.Name).ToString()).Split('[]')[1] }} | select -ExpandProperty Path2

        $path4 = Get-VM $vm.Name | %{$_.Extensiondata.LayoutEx.File | where {$_.Name -like "*.vmsd"}} |

        select @{N="Path4";E={(((($_.Name).ToString()).Split('[]')[2]).Split('/')[0]).TrimStart(" ") }} | select -ExpandProperty Path4

        $vmPath = $path2+"/"+$path4

        # checks whether the vmsd file has the entry which identifies a VM snapshot with VM memory

        $vmsd = C:\plink.exe -ssh $esx -l $user -pw $rootpword -batch "cat /vmfs/volumes/$vmPath/*.vmsd | grep snapshot0.type"

            if ($vmsd -match "snapshot")

            {

            $gather.MemorySnapshot = "Yes"

            }

            else

            {

            $gather.MemorySnapshot = "No"

            }

       

      $collection += $gather

     

      }

      $collection | ft



  • 4.  RE: Snapshot properties

    Posted Feb 03, 2018 11:07 PM

    Hello @JStars,

    Ln 28.     $vmsd = C:\plink.exe -ssh $esx -l $user -pw $rootpword -batch "cat /vmfs/volumes/$vmPath/*.vmsd | grep snapshot0.type"

    Not a massive PowerCLI head (yet :smileygrin: ) so excuse if I am misinterpreting something of the above:

    Shouldn't there be a wildcard or just grep for 'type' as I stated?

    Wouldn't the above only find a memory-snapshot if it is the first taken snapshot (snapshot0) - e.g. 2nd (snapshot1) or later snapshot could have memory and the above would not detect it.

    Aswell, a VM having 1 snapshot with memory doesn't mean all of the snapshots included memory.

    Bob



  • 5.  RE: Snapshot properties

    Posted Feb 04, 2018 03:50 PM

    I'm not a "head" either! :smileyhappy:

    We don't go past one snapshot in our environment however you are totally right. The script logic does not change though, it just needs few more loops. The script below deals with a situation where there could be 3 snapshots on a VM. If needed to consider more the script can be easily amended. Again, please move this thread to the PowerCLI section as more experienced people could look at this and comment/improve/benefit...

    # Login credentials to SSH each host. It will convert the string from secure to plain text for plink

    $user = Read-Host "ESXi Host SSH User"

    $rootpword = Read-Host "ESXi Host SSH Password" -AsSecureString

    $rootbstr = [System.Runtime.InteropServices.marshal]::SecureStringToBSTR($rootpword)

    $rootpword = [System.Runtime.InteropServices.marshal]::PtrToStringAuto($rootbstr)

    $vms = Get-View -ViewType virtualmachine | ?{$_.Snapshot -ne $null} | select Name

    $collection = @()

    foreach($vm in $vms)

        {

        $gather = "" | select VM,FirstSnAndType,SecondSnAndType,ThirdSnAndType,PowerState

        $gather.VM = Get-VM $vm.Name

        $gather.PowerState = Get-VM $vm.Name | select -ExpandProperty PowerState

        $esx = Get-VM $vm.Name | Get-VMHost

        # Check if the SSH service is running on the host else it will start it

        Get-VMHost $esx | Get-VMHostService | Where-Object {$_.Key -eq "TSM-SSH" -and $_.Running -eq $false}|Start-VMHostService -Confirm:$false | Out-Null

        # The "echo y" command automatically accepts the key for plink

        echo y | C:\plink.exe -ssh $esx -l $user -pw $rootpword exit | Out-Null

        # builds the vmfs path to the vmsd file

        $path2 = Get-VM $vm.Name | %{$_.Extensiondata.LayoutEx.File | where {$_.Name -like "*.vmsd"}} |

        select @{N="Path2";E={(($_.Name).ToString()).Split('[]')[1] }} | select -ExpandProperty Path2

        $path4 = Get-VM $vm.Name | %{$_.Extensiondata.LayoutEx.File | where {$_.Name -like "*.vmsd"}} |

        select @{N="Path4";E={(((($_.Name).ToString()).Split('[]')[2]).Split('/')[0]).TrimStart(" ") }} | select -ExpandProperty Path4

        $vmPath = $path2+"/"+$path4

        # checks whether the vmsd file has the entries which identify a VM snapshot with or without VM memory

        if ((Get-VM $vm.Name | Get-Snapshot | select -ExpandProperty Name).count -ge "1" )

          {

          $vmsd = C:\plink.exe -ssh $esx -l $user -pw $rootpword -batch "cat /vmfs/volumes/$vmpath/*.vmsd"

          $gather.VM = Get-VM $vm.Name

          if($vmsd -match "snapshot0.type")

              {

              $gather.FirstSnAndType = "MemorySNAP - "+(Get-VM $vm.Name|Get-Snapshot|select -Index 0|select -ExpandProperty Name)

              }

              else

              {

              $gather.FirstSnAndType = "NOmemorySN - "+(Get-VM $vm.Name|Get-Snapshot|select -Index 0|select -ExpandProperty Name)

              }

          if($vmsd -match "snapshot1.type")

              {

              $gather.SecondSnAndType = "MemorySNAP - "+(Get-VM $vm.Name|Get-Snapshot|select -Index 1|select -ExpandProperty Name)

              }

              elseif($vmsd -match "snapshot1.uid")

                {

                $gather.SecondSnAndType = "NOmemorySN - "+(Get-VM $vm.Name|Get-Snapshot|select -Index 1|select -ExpandProperty Name)

                }

                elseif($vmsd -notmatch "snapshot1.uid")

                    {

                    $gather.SecondSnAndType = "NOSnapshot"

                    }

          if($vmsd -match "snapshot2.type")

              {

              $gather.ThirdSnAndType = "MemorySNAP - "+(Get-VM $vm.Name|Get-Snapshot|select -Index 2|select -ExpandProperty Name)

              }

              elseif($vmsd -match "snapshot2.uid")

                {

                $gather.ThirdSnAndType = "NOmemorySN - "+(Get-VM $vm.Name|Get-Snapshot|select -Index 2|select -ExpandProperty Name)

                }

                elseif($vmsd -notmatch "snapshot2.uid")

                    {

                    $gather.ThirdSnAndType = "NOSnapshot"

                    }

          }

      $collection += $gather

     

        }

    $collection | ft



  • 6.  RE: Snapshot properties

    Posted Feb 06, 2018 08:27 PM

    Doesn't the PowerState of the snapshot show if the memory was included? I can't find documentation to verify this, but my anecdotal evidence shows it's true. :smileyhappy:

    This will list all snapshots in the servers you are currently connected to (Connect-ViServer).

    Get-VM | Get-Snapshot | Select Created, Description, VM, Parent, Children, @{L="SizeGB";E={$_.sizegb.toString("#0.##")}}, PowerState, Name

    The above command can be run through Export-CSV and saved as a csv if needed. Just add | Export-CSV filename.csv to the end of the command.

    Here's a script that will format things a bit (also changes PowerState heading to IncludeMemory) and can also be piped through Export-Csv.

    if ($global:DefaultViServers.count) {

        $vcenter = $global:DefaultViServers.name

        write-host "`nSearching [$vcenter] for snapshots.`n" -f yellow

       

    } else {

        write-host "`n`nNot connected to a vCenter!" -f red

        Connect-ViServer

    }

    $snapshots = Get-VM | Get-Snapshot | Select Created, Description, VM, Parent, Children, @{L="SizeGB";E={$_.sizegb.toString("#0.##")}}, PowerState, Name

    $snaps_out = @()

    foreach ($snapshot in $snapshots) {

        if ($snapshot.PowerState -eq "PoweredOn") {

            $memory = $True

        } else {

            $memory = $False

        }

        $snap = New-Object PSObject -Property @{

            Name = $snapshot.name

            Description = $snapshot.Description

            Created = $snapshot.created

            VM = $snapshot.VM

            IncludeMemory = $memory

            ParentSnap = $snapshot.Parent

            ChildSnaps = $snapshot.Children

            SizeGB = $snapshot.SizeGB

        }

        $snaps_out += $snap

    }

    write-output $snaps_out



  • 7.  RE: Snapshot properties

    Posted Feb 07, 2018 09:03 PM

    true, as I haven't found info on this, I thought the PowerState referred to the VM state but it does not. In fact VM power state is also labelled PowerState. So yes, get-snapshot provides the requested info