VMware Cloud Community
niklasak
Contributor
Contributor
Jump to solution

VM nic/mac/fqdn report with powercli slow..

Hi

I made this script to get all VMs name,fqdn name,mac address, generate type

It takes about 5 minutes to run when i run it on 250 VMs on a single vCenter, is there anyway to make it go faster?

The customer wanted to get both the vm name and the host name of the vm itself as they could differ incase of someone not following procedures when setting it up.

The customer has a vSphere 4.1 u1 environment

# Get the Virtual Network Adapter
#
# Niklas Akerlund / RTS

$VMs = Get-VM *
$Data = @()

foreach ($VM in $VMs){
     $VMGuest = Get-View $VM.Id   
    $NICs = $VM.NetworkAdapters
    foreach ($NIC in $NICs) {
        $into = New-Object PSObject
        Add-Member -InputObject $into -MemberType NoteProperty -Name VMname $VM.Name
        Add-Member -InputObject $into -MemberType NoteProperty -Name VMfqdn $VM.Guest.HostName
        Add-Member -InputObject $into -MemberType NoteProperty -Name NICtype $NIC.Type
        Add-Member -InputObject $into -MemberType NoteProperty -Name MacAddress $NIC.MacAddress
        Add-Member -InputObject $into -MemberType NoteProperty -Name AddresType $NIC.ExtensionData.AddressType
        $Data += $into
       
    }

}
$Data | Export-Csv -Path C:\VMNICinfo2.csv -NoTypeInformation

twitter: vniklas blog: http://vniklas.djungeln.se
Reply
0 Kudos
1 Solution

Accepted Solutions
mattboren
Expert
Expert
Jump to solution

Hello, niklasak-

Yes, you can make it faster by using just the Get-View, instead of using Get-VM as well.  Not too different from your script in appearance (you were already using Get-View some), but considerably faster:

&{Get-View -ViewType VirtualMachine -Property Name, Guest.HostName, Config.Hardware.Device | %{
   
$viewThisVM = $_
   
$viewThisVM.Config.Hardware.Device | ?{$_ -is [VMware.Vim.VirtualEthernetCard]} | %{
       
New-Object -Type PSObject -Property @{
            VMname
= $viewThisVM.Name
            VMfqdn
= $viewThisVM.Guest.HostName
            NICtype
= $_.GetType().Name
            MacAddress
= $_.MacAddress
            AddressType
= $_.AddressType
        }
## end new-object
    } ## end foreach-object
} ## end foreach-object
} | Select VMname,VMfqdn,NICtype,MacAddress,AddressType | Export-Csv -Path C:\VMNICinfo2.csv -NoTypeInformation -UseCulture

In a test environment with about 350 VMs, it took about five (5) seconds.  The previous way took about 7.5 minutes (447 seconds) in this environment.  So, it was about ninety (90) times faster -- not bad.  Should be a bit lighter on memory consumption, too.  If you are seeking speed, sticking with Get-View (and specifying only the Properties necessary) is a great way to go.

How does this do for you?

View solution in original post

Reply
0 Kudos
3 Replies
mattboren
Expert
Expert
Jump to solution

Hello, niklasak-

Yes, you can make it faster by using just the Get-View, instead of using Get-VM as well.  Not too different from your script in appearance (you were already using Get-View some), but considerably faster:

&{Get-View -ViewType VirtualMachine -Property Name, Guest.HostName, Config.Hardware.Device | %{
   
$viewThisVM = $_
   
$viewThisVM.Config.Hardware.Device | ?{$_ -is [VMware.Vim.VirtualEthernetCard]} | %{
       
New-Object -Type PSObject -Property @{
            VMname
= $viewThisVM.Name
            VMfqdn
= $viewThisVM.Guest.HostName
            NICtype
= $_.GetType().Name
            MacAddress
= $_.MacAddress
            AddressType
= $_.AddressType
        }
## end new-object
    } ## end foreach-object
} ## end foreach-object
} | Select VMname,VMfqdn,NICtype,MacAddress,AddressType | Export-Csv -Path C:\VMNICinfo2.csv -NoTypeInformation -UseCulture

In a test environment with about 350 VMs, it took about five (5) seconds.  The previous way took about 7.5 minutes (447 seconds) in this environment.  So, it was about ninety (90) times faster -- not bad.  Should be a bit lighter on memory consumption, too.  If you are seeking speed, sticking with Get-View (and specifying only the Properties necessary) is a great way to go.

How does this do for you?

Reply
0 Kudos
niklasak
Contributor
Contributor
Jump to solution

Hi Matt

with your script it took about 30 seconds running on 304 VMs, much faster thanks..

//niklas

twitter: vniklas blog: http://vniklas.djungeln.se
Reply
0 Kudos
steve31783
Enthusiast
Enthusiast
Jump to solution

Possible to add IP address to this?

Reply
0 Kudos