Powershell – Hyper-V get VM IP config

Posted: February 28, 2020 in Scripts

Prerequisite:

Install Integration services on VM

This script will get IPV4 only IP address, VM name and MAC address.

: will be added to MAC address. In case VM has more than one IP/MAC, those values will be in same column separated by space

# Get all running VMs

$vms = Get-VM | Where { $_.State –eq ‘Running’ } | Select-Object -ExpandProperty Name 

# Declare "global" array variable (make it available outside loop)

$results = @()
 foreach($vm in $vms) {

    # Get network interface details
    $out = Get-VMNetworkAdapter -vmname $vm | select VMName, MacAddress, IPAddresses

    # Remove duplicate VM names
    $vm_name = $out.VMName | Get-Unique

    # In case more than 1 IP, put it in same row separated by space (192.168.1.1, 192.168.1.2)
    
    $ip = ($out.IPAddresses | ForEach-Object {
    $_ | ? {$_ -notmatch ':'}   
    }) -join " "

    # If more than 1 MAC , put it in same row separated by space (00:15:5D:58:12:5E 00:15:5D:58:12:5F )
    
    $mac = ($out.MacAddress | ForEach-Object {
    $_.Insert(2,":").Insert(5,":").Insert(8,":").Insert(11,":").Insert(14,":")
}) -join ' '
     
  # Add headers
   
$comp = Get-WmiObject Win32_ComputerSystem | Select-Object -ExpandProperty name

$obj = New-Object -TypeName psobject
$obj | Add-Member -MemberType NoteProperty -Name "VM NAME" -Value $vm_name
$obj | Add-Member -MemberType NoteProperty -Name "IP ADDRESS" -Value $ip
$obj | Add-Member -MemberType NoteProperty -Name "MAC ADDRESS" -Value $mac
$obj | Add-Member -MemberType NoteProperty -Name "HYPER-V HOST" -Value $comp

# Append object to outside "global" variable

$results += $obj

#$obj| Export-Csv -Path "c:\1.csv" -NoTypeInformation -append 
}

# write results to CSV file
                                
$results| Export-Csv -Path "c:\1.csv" -NoTypeInformation
 

Option 2: multi-line (every IP/MAC) have new line

$results = Get-VM | Where State –eq Running | Get-VMNetworkAdapter | ForEach-Object {
    [pscustomobject]@{
        'VM NAME'      = $_.VMName
        'IP ADDRESS'   = ($_.IPAddresses -notmatch ':') -join ' '
        'MAC ADDRESS'  = ($_.MacAddress -replace '(..)(..)(..)(..)(..)','$1:$2:$3:$4:$5:') -join ' '
        'HYPER-V HOST' = $env:COMPUTERNAME
    }
}
$results | Export-Csv -Path "c:\1.csv" -NoTypeInformation

# Put in single row multiple IP/MAC addresses for one VM:

$csv = $results | Group-Object 'VM NAME'  | ForEach-Object {
 [PsCustomObject]@{
 'VM NAME' = $_.Name
 'IP ADDRESS' = $_.Group.'IP ADDRESS' -join ' '
 'MAC ADDRESS' = $_.Group.'MAC ADDRESS' -join ' '
 }
}
$csv | Export-Csv -Path "c:\1.csv" -NoTypeInformation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s