Powershell:获取虚拟机属性

Powershell: Get VM Properties

我创建了一个 VM,我想将其属性导出到 CSV 文件中。
我试过的没有给我 IPAddress、SwitchName、Macaddress。

$Data = @();
$VMs = Get-VM $VMName;
foreach($VM in $VMs){
$VMCustom = New-Object System.Object;
$VMCustom | Add-Member -Type NoteProperty -Name VMName -Value $VM.VMName;
# Get-VMNetworkAdapter -VMName $VMName | Select -expand IPAddresses
$VMCustom | Add-Member -Type NoteProperty -Name IPAddress -Value $VM.guest.IPAddresses;
$VMCustom | Add-Member -Type NoteProperty -Name SwitchName -Value $VM.MacAddress;
$VMCustom | Add-Member -Type NoteProperty -Name Status -Value $VM.State;
$VMCustom | Add-Member -Type NoteProperty -Name Generation -Value $VM.Generation;
$VMCustom | Add-Member -Type NoteProperty -Name SwitchName -Value $VM.SwitchName;

$Data += $VMCustom;
} 

$Data | Export-CSV "C:\VM.csv" -Delimiter ";"; 

问:Ipaddress是VM的IP地址还是Hyper-V的IP地址?

如果有人能帮助我,那就太好了。

试试这个:

$Data = @()

$VMs = "Server-001","Server-002","Server-003"

foreach($VM in $VMs)
{
    $VMInfo = Get-VM -Name $VM
    $VMNetwork = $VMInfo | Get-VMNetworkAdapter

    $VMCustom = New-Object System.Object
    $VMCustom | Add-Member -Type NoteProperty -Name VMName -Value $VMInfo.VMName
    $VMCustom | Add-Member -Type NoteProperty -Name Status -Value $VMInfo.Status
    $VMCustom | Add-Member -Type NoteProperty -Name Generation -Value $VMInfo.Generation

    $VMCustom | Add-Member -Type NoteProperty -Name IPAddress -Value $VMNetwork.IPAddresses[0]
    $VMCustom | Add-Member -Type NoteProperty -Name MacAddress -Value $VMNetwork.MacAddress
    $VMCustom | Add-Member -Type NoteProperty -Name SwitchName -Value $VMNetwork.SwitchName

    $Data += $VMCustom
} 

$Data | Export-CSV "C:\VM.csv" -Delimiter ";" -NoTypeInformation