简体   繁体   中英

Get snapshot information of vm PowerCLI

I have written a little script which shows me some informations about virtual machines.

    $vsms = Get-VM 
    $erg = $vsms | Select-Object -Property @{N="VmName"; E={$_.Name}}, 
                                   @{N="Power"; E={$_.PowerState}}, 
                                   @{N="CustomTag1"; E={$_.CustomFields.Item("CustomTag1")}}, 
                                   @{N="Customtag2"; E={$_.CustomFields.Item("Customtag2")}}, 
                                   @{N="Customtag3"; E={$_.CustomFields.Item("Customtag3")}}, 
                                   @{N="ProvisionedSpaceGB"; E={[math]::Round($_.ProvisionedSpaceGB)}}, `
                                   @{N="UsedSpaceGB"; E={[math]::Round($_.UsedSpaceGB)}}, 
                                   @{N="IP Address";E={@($_.guest.IPAddress[0])}}

$erg | Sort-Object VmName | Export-Csv $outputPath -NoType

My Question is how can i expand this script to get informations about the restore points (creation time, etc...). And how can i export the result as csv so all creation times and other properties are in one cell/line for each vm? the result should be exported as csv like i already do. It should look like this:

VmName | SnapshotCreationTime| ... other properties
testvm | 19:17 01.02.18, 19:17 02.02.18,... | other properties
testvm2| 19:17 08.02.18, 19:17 02.03.18,... | other properties

not:

VmName | SnapshotCreationTime| other properties (already in script)
testvm | 19:17 01.02.18,     | other properties
testvm | 19:17 02.02.18,     | other properties
testvm | 19:17 03.02.18,     | other properties
testvm2| 19:17 08.02.18,     | other properties
testvm2| 19:17 09.02.18,     | other properties
testvm2| 19:17 10.02.18,     | other properties

You will need the -Join in your snapshotdate attribute.

Example:

(Get-VM "VMName" | Get-View) | %{
$Summary = "" | Select Name, HostName, State, NumCPU, MemoryMB, HDSizeKB, HDFreeSpaceKB, GuestOS, Datacenter, Folder, IP, SnapshotName, SnapshotDate
    $Summary.Name = $_.Summary.Config.Name
    $summary.HostName = $_.Summary.Guest.HostName
    $Summary.State = $_.Summary.Runtime.PowerState
    $Summary.NumCPU = $_.Summary.Config.NumCPU
    $Summary.MemoryMB = $_.Summary.Config.MemorySizeMB
    $Summary.HDSizeKB = $_.Guest.Disk.Capacity/1KB
    $Summary.HDFreeSpaceKB = $_.Guest.Disk.FreeSpace/1KB
    $Summary.GuestOS = $_.Summary.Config.GuestFullName
    $Summary.Folder = $_.Folder.Name
    $Summary.IP = $_.Summary.Guest.IPAddress
    $Summary.SnapshotName = &{$script:snaps = Get-Snapshot -VM $Summary.Name; $script:snaps.Name -join ','}
    $Summary.snapshotdate = $script:snaps.Created -join ','
    $Summary

}| Export-Csv C:\setup\Tezt.csv -NoTypeInformation -Encoding UTF8 -Delimiter "|"

You are getting an output like:

"Name"|"HostName"|"State"|"NumCPU"|"MemoryMB"|"HDSizeKB"|"HDFreeSpaceKB"|"GuestOS"|"Datacenter"|"Folder"|"IP"|"SnapshotName"|"SnapshotDate"
"VMName"||"poweredOff"|"2"|"4096"|"0"|"0"|"Microsoft Windows Server 2008 (64-bit)"||||"Test Snap 00,Test snap 01"|"12/07/18 11:15:42 AM,12/07/18 11:16:04 AM"

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM