获取vm PowerCLI的快照信息

时间:2018-07-12 07:17:56

标签: powershell csv virtual-machine vmware snapshot

我写了一个小脚本,向我展示了有关虚拟机的一些信息。

    $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

我的问题是我如何扩展此脚本以获取有关还原点的信息(创建时间等)。以及如何将结果导出为csv,以便每个虚拟机的所有创建时间和其他属性都在一个单元格/行中?结果应该像我已经做的那样导出为csv。 它应该看起来像这样:

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

不是:

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

1 个答案:

答案 0 :(得分:0)

您将在您的snapshotdate属性中使用-Join

示例:

(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 "|"

您得到的输出如下:

"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"

希望有帮助!