文章

使用Powershell批量收集Hyper-V VM信息

Powershell 批量收集Hyper-V VM信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
############################################################################################
# 批量查询VM信息,并导出为csv文件
#    名称、计算机名、IP地址、内存、CPU、创建时间等等
#
# 注意 :适用于SCVMM 控制台的Powershell命令,如果要用于本地Hyper-V主机,需要修改以下参数名 
#    VirtualNetworkAdapters ——> NetworkAdapters
#    VirtualHardDisks       ——> HardDisks
#    IPv4Addresses          ——> IPAddresses
#    Description            ——> Notes
############################################################################################

$allVM = Get-VM | Select-Object *
$vmlist = @()

foreach ( $VM in $allVM )
{
    $IP = Get-VM $VM.Name | Select-Object -ExpandProperty VirtualNetworkAdapters | Select-Object -ExpandProperty IPv4Addresses
    $DiskSize = [int]( (Get-VM $VM.Name | Select-Object -ExpandProperty VirtualHardDisks).TotalSize /1024/1024/1024)

    $VM | Add-Member -Membertype noteproperty -Name IP -value $IP 
    $VM | Add-Member -Membertype noteproperty -Name DiskSize -value $DiskSize
    $VMList+=$VM
}

$VMlist | Select name,ComputerName,IP,Description,CreationTime,OperatingSystem,CPUCount,Memory,DiskSize | ft 

$allVM = Get-VM | Select-Object *
$VMlist = @()

foreach ( $VM in $allVM )
{
    $IP = Get-VM $VM.Name | Select-Object -ExpandProperty VirtualNetworkAdapters | Select-Object -ExpandProperty IPv4Addresses
    $DiskSize = [int]( (Get-VM $VM.Name | Select-Object * ).TotalSize /1024/1024/1024)
    $VMObject = @()
    $VMObject = New-Object psobject

    $VMObject | Add-Member -Membertype noteproperty -Name 名称          -value $VM.Name 
    $VMObject | Add-Member -Membertype noteproperty -Name 计算机名      -value $VM.ComputerName
    $VMObject | Add-Member -Membertype noteproperty -Name IP地址        -value $IP
    $VMObject | Add-Member -Membertype noteproperty -Name 状态          -value $VM.Status
    $VMObject | Add-Member -Membertype noteproperty -Name CPU           -value $VM.CPUCount
    $VMObject | Add-Member -Membertype noteproperty -Name "内存(MB)"    -value $VM.Memory
    $VMObject | Add-Member -Membertype noteproperty -Name "磁盘(GB)"    -value $DiskSize
    $VMObject | Add-Member -Membertype noteproperty -Name 创建时间      -value $VM.CreationTime
    $VMObject | Add-Member -Membertype noteproperty -Name 描述信息      -value $VM.Description
    $VMObject | Add-Member -Membertype noteproperty -Name 操作系统      -value $VM.OperatingSystem

    $VMList+=$VMObject
}
$VMlist | Select-Object * | Format-Table
$VMlist | Export-Csv D:\VM_info.csv -Encoding UTF8 -Append -Force -ErrorAction:Continue

查询Guest VM的IP、MAC、vNic ID

1
Get-VM DC01 | Select-Object -ExpandProperty VirtualNetworkAdapters | Select-Object Name,MacAddress,IPv4Addresses,ID

查询单台 VM 信息

1
Get-vm | Select Name,ComputerName,Description,CreationTime,Generation,CPUCount,Memory,TotalSize | ft
本文由作者按照 CC BY 4.0 进行授权