Powershell:获取VM属性

时间:2016-06-28 12:24:57

标签: powershell virtual-machine hyper-v

我创建了一个虚拟机,我想在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地址?

如果有人可以帮助我,那将会很棒。

1 个答案:

答案 0 :(得分:0)

试试这个:

$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