Tinus EngOps Wiki

Logo

OpsaC - Operating as PowerShell code

Links

Home

PowerShell Blog

PowerShell Index

PowerShell Search

Additional Websites

View my GitHub Profile

View my GitHub Gists

View Tinus IT Wiki

View my Photo Website

Query VirtualMachines

published: September 24, 2023 author: Tinu tags: PowerShell categories: VMware


Table of Contents

Table of Contents PowerCLI

PowerCLI

Get-View vs. Get-VM

The performance with Get-View is much better than Get-VM:

Connect-ViServer -Server "vcenter.example.com"

$Stopwatch = [System.Diagnostics.Stopwatch]::new()
$Stopwatch.Start()

$OverheadVMs = Get-VM
$OverheadVMs | Get-Snapshot | Select-Object -Property VM, Name, Description

$Stopwatch.Stop()
$Stopwatch.Elapsed.TotalSeconds

Output

0.8237531 seconds
$Stopwatch = [System.Diagnostics.Stopwatch]::new()
$Stopwatch.Start()

$FastVMs = Get-View -ViewType VirtualMachine
$FastVMs | Select-Object -Property @{N='VM';E={$PSItem.Name}},
 @{N='Description';E={$PSItem.Snapshot.RootSnapshotList[0].Description}}, 
 @{N='Snapshot';E={$PSItem.Snapshot.RootSnapshotList[0].Name}} | Where-Object Snapshot

$Stopwatch.Stop()
$Stopwatch.Elapsed.TotalSeconds

Output

0.1823357 seconds

Fast listing of old Snapshots

$Stopwatch = [System.Diagnostics.Stopwatch]::new()
$Stopwatch.Start()

$FastVMs  = Get-View -ViewType VirtualMachine -Filter @{'Config.Template' = 'false'; 'Snapshot' = '.*'}
$Snapshot = $FastVMs | ForEach-Object {
    $CreateTime = Get-Date ($_.Snapshot.RootSnapshotList[0].CreateTime)
    [PSCustomObject][Ordered] @{
        CreateTime       = Get-Date ($CreateTime) -Format 'yyyy-MM-dd HH:mm:ss'
        VMName           = $_.Name
        Snapshot         = $_.Snapshot.RootSnapshotList[0].Name -replace '%2f', '/'
        Description      = $_.Snapshot.RootSnapshotList[0].Description
        TotalDays        = [Math]::Round((New-TimeSpan -Start $CreateTime -End (Get-Date)).TotalDays,0)
        ChildSnapshot    = $_.Snapshot.RootSnapshotList[0].ChildSnapshotList
        ChildDescription = $_.Snapshot.RootSnapshotList[0].ChildSnapshotList.Description
        ChildCreateTime  = $_.Snapshot.RootSnapshotList[0].ChildSnapshotList.CreateTime
    }
} | Sort-Object CreateTime 
$Snapshot.Where( {$_.Description -notmatch 'do not delete' -and $_.TotalDays -gt $day} ) | Format-Table -AutoSize

$Stopwatch.Stop()
$Stopwatch.Elapsed.TotalSeconds

See also

API Reference on VMware Developper Documentation.


← Previous Post [ Top ] Copyright © 2024 by tinuwalther [ Blog ] Next Post →