简体   繁体   English

获取vm PowerCLI的快照信息

[英]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? 以及如何将结果导出为csv,以便每个虚拟机的所有创建时间和其他属性都在一个单元格/行中? the result should be exported as csv like i already do. 结果应该像我已经做的那样导出为csv。 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. 您将在您的snapshotdate属性中需要-Join

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! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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