VMware Cloud Community
MehdiF
Enthusiast
Enthusiast
Jump to solution

Split Network Adapter and Mac Address in list

$vms= Get-VM 

foreach ($vm in $vms) {
    $vm |  
    foreach -Process {

        $vmNet = Get-NetworkAdapter -VM $vm
        ''| Select @{N="VM";E={$vm.Name}},
         
        @{N='Adapter';E={$vmNet.Type}},
        @{N="MAC Address:";E={$vm.Guest.Nics.MacAddress.Split("`n")[0]}},
        @{N='Guest OS' ; E={$vm.ExtensionData.Guest.GuestFullName}},
        @{N='Guest OS Version' ; E={$vm.ExtensionData.Config.GuestFullName}},
        @{N='Hardware Version' ; E={$vm.ExtensionData.Guest.HWversion}},
        @{N='VMware Tools State';E={$vm.ExtensionData.Guest.ToolsStatus}},
        @{N='VMware Tools Version';E={$vm.ExtensionData.Guest.ToolsVersion}},
        @{N='Managed By';E={$vm.CustomFields.Item('ManagedBy')}} | Format-Table -AutoSize

    }

}

 

I don't know split in columns values of Mac Address and Adapter like 

I tried several code with foreach or .Split but no success

VM | Adapter 1 | Adapter 2 | Adapter 3 | Mac Address 1 |Mac Address 2 | ......

Anyone knows this ? 

Thank you 🙂 

0 Kudos
24 Replies
MehdiF
Enthusiast
Enthusiast
Jump to solution

Yes, i have vm without vmtools. 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

This should skip the Net info for VMs that do not have VMware Tools installed.

 

$Report = New-Object System.Collections.ArrayList
Get-View -ViewType VirtualMachine -SearchRoot (Get-Cluster "MyCluster").id -PipelineVariable vm |
    ForEach-Object -Process {
        $VMReport = New-Object PSObject
        Add-Member -InputObject $VMReport -MemberType NoteProperty -Name Guest -Value $VM.Name
        Add-Member -InputObject $VMReport -MemberType NoteProperty -Name OSName -Value $($VM.Guest.GuestFullName)
        Add-Member -InputObject $VMReport -MemberType NoteProperty -Name MemoryMB -Value $VM.summary.config.MemorySizeMB
        Add-Member -InputObject $VMReport -MemberType NoteProperty -Name VmDatastore -Value $VM.summary.Config.VmPathName.split()[0]
        if ($vm.Guest -ne $null -and $vm.Guest.Net -ne $null) {
            $i = 0
            $vm.Guest.Net | ForEach-Object -Process {
                Add-Member -InputObject $VMReport -MemberType NoteProperty -Name "networkcard${i}.Network" -Value $_.Network
                Add-Member -InputObject $VMReport -MemberType NoteProperty -Name "networkcard${i}.MacAddress" -Value $_.Macaddress
                Add-Member -InputObject $VMReport -MemberType NoteProperty -Name "networkcard${i}.IpAddress" -Value ($_.IpAddress.Where{ $_ -like "*.*" } -join '|')
                $i++
            }
        }
        $Report.add($VMReport) | Out-Null
    }

$Report | Sort-Object -Property { ($_ | Get-Member -MemberType Property).Count } -Descending |
    Export-Csv -Path c:\Scripts\Output\vmreport.csv -UseCulture -NoTypeInformation

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
wetnose88
Enthusiast
Enthusiast
Jump to solution

Hi Luc,

I tested the latest version, it works, even the scripts reported some errors for the following line of code.

Add-Member -InputObject $VMReport -MemberType NoteProperty -Name "networkcard${i}.Device" -Value $networkcards[$i].GetType().Name.Replace("Virtual", '')

🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟

Cannot index into a null array.
At C:\Docs\Scripts\Get-VMInformation.ps1:85 char:3
+ Add-Member -InputObject $VMReport -MemberType NoteProperty -N ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray

🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟

I have reviewed, some of my VMs or templates do not have either a mac address or IP address, some of them missed both

 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Leave that line out, to get the type of vNIC would require quite a bit more code.

If the VMware Tools do not capture the info, it will be missing.
Not a lot I can do I'm afraid.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
MehdiF
Enthusiast
Enthusiast
Jump to solution

Hello,

Thank you both for your feedback. 

I'll be able to manage to list what I need with the scripts you've provided 🙂

0 Kudos