VMware Cloud Community
juliennas
Contributor
Contributor
Jump to solution

Powercli to report VM Nics AdapterType

Hi all,

I'm trying to make a report of my VMs (400). I'm managing 2 Vcenters in linked mode, and I ve seen some errors in the VM creation process, e1000, Vmxnet2 cards on servers.

I use Powershell/Powercli for a couple of years from now, but I m facing some problems ... If servers have more than 1 NIC, my report just write 1 card ...

If you could help me, I guess my script is not correctly written ...

Here it is :

##### CREATE EXCEL #####
$Excel = New-Object -Com Excel.Application
$Excel.visible = $true
$Excel = $Excel.Workbooks.Add(1)

$VM = $Excel.WorkSheets.Item(1)
$VM.Name = "Network Type"
$VM.Cells.Item(1,1) = "VM Name"
$VM.Cells.Item(1,2) = "OS Name"
$VM.Cells.Item(1,3) = "IP Address"
$VM.Cells.Item(1,4) = "VM HOST"
$VM.Cells.Item(1,5) = "VM STATUS"
$VM.Cells.Item(1,6) = "Adapter Type"
$VM.Cells.Item(1,7) = "Adapter Name"
$VM.Cells.Item(1,8) = "DataCenter"


$WorkBook = $VM.UsedRange
$WorkBook.Interior.ColorIndex = 0
$WorkBook.Font.ColorIndex = 5
$WorkBook.Font.Bold = $True
$WorkBook.HorizontalAlignment = -4108
$VmRange = $VM.range("A1:Z500")
$Vmrange.font.size = 8
$intRow = 2

##### CONNECTING VCENTER #####
Connect-VIServer -server $VCenter -User $User -Password $PWD -AllLinked:$true

$VMs = Get-VM  | Sort-Object Name

Foreach ($ColVM in $VMs) {
    #Gathering Info on $ColVM   
    $VMsItems     = Get-VM -Name $ColVM | Select Name, VMHost, PowerState,
            @{N="OSFullName";E={($_ | Get-VMGuest).OSFullName}},
            @{N="IPAddress";E={($_ | Get-VMGuest).IPAddress}},
            @{N="AdapterType";E={($_ | Get-NetworkAdapter).Type}},
            @{N="AdapterName";E={($_ | Get-NetworkAdapter).Name}},
            @{N="Datacenter";E={($_ | Get-Datacenter).Name}},
            @{N="CardCount";E={($_ | Get-NetworkAdapter).Count}}

    #Filling Excel Report
    Foreach ($Name in ($VMsItems.Name)){
        $VM.Cells.Item($intRow,1) = [STRING]$Name.Name
        $VM.Cells.Item($intRow,2) = [STRING]$Name.OSFullName
        $VM.Cells.Item($intRow,3) = [STRING]$Name.IPAddress
        $VM.Cells.Item($intRow,4) = [STRING]$Name.VMHost
        $VM.Cells.Item($intRow,5) = [STRING]$Name.PowerState
            If        ($VMsItems.PowerState -eq "PoweredOn"){$VM.Cells.Item($intRow,5).Interior.ColorIndex = 4}
            Else    {$VM.Cells.Item($intRow,5).Interior.ColorIndex = 3}
        $VM.Cells.Item($intRow,8) = [STRING]$Name.Datacenter
        $VM.Cells.Item($intRow,6) = [STRING]$Name.AdapterType
            If        ($Name.AdapterType -eq "Vmxnet3")    {$VM.Cells.Item($intRow,6).Interior.ColorIndex = 4}
            Else     {$VM.Cells.Item($intRow,6).Interior.ColorIndex = 3}
        $VM.Cells.Item($intRow,7) = [STRING]$Name.AdapterName
        $IntRow ++    }
    }   
$WorkBook.EntireColumn.AutoFit()
$VM.Columns.Item(2).ColumnWidth = 25

##### SAUVEGARDE #####
$Excel.SaveAs($FILENAME)

##### DECONNEXION VCENTER & KILL EXCEL PROCESS #####
Disconnect-VIServer -server * -Confirm:$false -Force:$true
#Stop-Process -processname excel

Thanks,

Julien

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Did you also try

$VMs =  Get-ResourcePool -Name "REC" | Get-VM

That should only give you the VMs in that specific resourcepool.

And you can have more than 1 name on the -Name parameter


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

View solution in original post

0 Kudos
29 Replies
LucD
Leadership
Leadership
Jump to solution

You should indeed be looping over all the NICs.

With the Join method you can combine all types in 1 string, separated by a comma.

Your Expression could something like this

[string]::Join(',',($_ | Get-NetworkAdapter | %{$_.Type)})

Btw, you could also try to use my Export-Xls function to create an Excel spreadsheet, instead of filling it out cell by cell.


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

juliennas
Contributor
Contributor
Jump to solution

Hi LucD,

Thanks for your reply, but I m a bit confused with this

[string]::Join(',',($_ | Get-NetworkAdapter | %{$_.Type)})

Where should I add this into my commands ?

$VMsItems     = Get-VM -Name $ColVM | Select Name, VMHost, PowerState,
            @{N="OSFullName";E={($_ | Get-VMGuest).OSFullName}},
            @{N="IPAddress";E={($_ | Get-VMGuest).IPAddress}},
            @{N="AdapterType";E={($_ | Get-NetworkAdapter).Type}},        <-- I guess it s here right ?
            @{N="AdapterName";E={($_ | Get-NetworkAdapter).Name}},
            @{N="Datacenter";E={($_ | Get-Datacenter).Name}},
            @{N="CardCount";E={($_ | Get-NetworkAdapter).Count}}

Change to this ?

@{N="AdapterType";E={[string]::Join(',',($_ | Get-NetworkAdapter | %{$_.Type)})},

Also, I'll have a look at your Excel Function Smiley Wink

Thanks for your support,

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That is correct, in the Expression of the calculated property


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

0 Kudos
juliennas
Contributor
Contributor
Jump to solution

LucD,

I've changed script to this, in bold :

##### CREATE EXCEL #####
$Excel = New-Object -Com Excel.Application
$Excel.visible = $true
$Excel = $Excel.Workbooks.Add(1)

$VM = $Excel.WorkSheets.Item(1)
$VM.Name = "Network Type"
$VM.Cells.Item(1,1) = "VM Name"
$VM.Cells.Item(1,2) = "OS Name"
$VM.Cells.Item(1,3) = "IP Address"
$VM.Cells.Item(1,4) = "VM HOST"
$VM.Cells.Item(1,5) = "VM STATUS"
$VM.Cells.Item(1,6) = "Adapter Type"
$VM.Cells.Item(1,7) = "Adapter Name"
$VM.Cells.Item(1,8) = "DataCenter"


$WorkBook = $VM.UsedRange
$WorkBook.Interior.ColorIndex = 0
$WorkBook.Font.ColorIndex = 5
$WorkBook.Font.Bold = $True
$WorkBook.HorizontalAlignment = -4108
$VmRange = $VM.range("A1:Z500")
$Vmrange.font.size = 8
$intRow = 2

##### CONNECTING VCENTER #####
Connect-VIServer -server $VCenter -User $User -Password $PWD -AllLinked:$true

$VMs =  Get-VM  | Sort-Object Name

Foreach ($ColVM in $VMs) {
    #Gathering Info on $ColVM   
    $VMsItems     = Get-VM -Name $ColVM | Select Name, VMHost, PowerState,
            @{N="OSFullName";E={($_ | Get-VMGuest).OSFullName}},
            @{N="IPAddress";E={($_ | Get-VMGuest).IPAddress}},
            @{N="AdapterName";E={([STRING]::JOIN(',',($_ | Get-NetworkAdapter | %{$_.Name})))}},
            @{N="AdapterType";E={([STRING]::JOIN(',',($_ | Get-NetworkAdapter | %{$_.Type})))}},

            @{N="Datacenter";E={($_ | Get-Datacenter).Name}},
            @{N="CardCount";E={($_ | Get-NetworkAdapter).Count}}

    #Filling Excel Report
    #Foreach ($Name in ($VMsItems.AdapterType)){
        $VM.Cells.Item($intRow,1) = [STRING]$VMsItems.Name
        $VM.Cells.Item($intRow,2) = [STRING]$VMsItems.OSFullName
        $VM.Cells.Item($intRow,3) = [STRING]$VMsItems.IPAddress
        $VM.Cells.Item($intRow,4) = [STRING]$VMsItems.VMHost
        $VM.Cells.Item($intRow,5) = [STRING]$VMsItems.PowerState
            If        ($VMsItems.PowerState -eq "PoweredOn"){$VM.Cells.Item($intRow,5).Interior.ColorIndex = 4}
            Else    {$VM.Cells.Item($intRow,5).Interior.ColorIndex = 3}
        $VM.Cells.Item($intRow,8) = [STRING]$VMsItems.Datacenter
        $VM.Cells.Item($intRow,6) = [STRING]$VMsItems.AdapterType
            If        ($VMsItems.AdapterType -like "*Vmxnet3*")    {$VM.Cells.Item($intRow,6).Interior.ColorIndex = 4}
            Else     {$VM.Cells.Item($intRow,6).Interior.ColorIndex = 3}
        $VM.Cells.Item($intRow,7) = [STRING]$VMsItems.AdapterName
        $IntRow ++    }
   #}  
$WorkBook.EntireColumn.AutoFit()
$VM.Columns.Item(2).ColumnWidth = 25

##### SAUVEGARDE #####
$Excel.SaveAs($FILENAME)

##### DECONNEXION VCENTER & KILL EXCEL PROCESS #####
Disconnect-VIServer -server * -Confirm:$false -Force:$true
#Stop-Process -processname excel

Now, I see another problem, objects are correctly populated in the command, $VMs =  Get-VM  | Sort-Object Name, but they are not filled in excel ...

Here is a sample of the file attached

Do you have any ideas to correct this ?

Thanks,

Julien

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Not sure I get the question.

Do you mean there are VMs for which there is no entry in the spreadsheet ?

Or do some of the cells have an incorrect value ?


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

0 Kudos
juliennas
Contributor
Contributor
Jump to solution

There is no entry in the spreadsheet for some VMs.

I m still looking for errors in the script, but I'm really confused ...

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Just ran your script and it produced a complete report in the spreadsheet, no VMs missing.

Perhaps you're seeing some templates that do not appeear in the XLS ?

Or is there anything special about the name of the VMs that are missing from the XLS ?

Accented letters perhaps ?


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

0 Kudos
juliennas
Contributor
Contributor
Jump to solution

Some of them have no tools, or not running tools.

No specials characters, or accented letters...

I have 2 datacenters, with 2 VCs in linked mode, and I'm running this throught PowerGui, using Powershell 3 & PowerCli 5.

Julien

0 Kudos
LucD
Leadership
Leadership
Jump to solution

If you do

Get-VM | Select Name

do you see all the VMs ?


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

0 Kudos
juliennas
Contributor
Contributor
Jump to solution

Yes, all off them in both datacenters.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Let's try to determine if the missing entries are caused by the XLS part.

The following is a slimmed-down ersion of your script that creates a CSV file.

Check if that file has all the VMs.

##### CONNECTING VCENTER #####
Connect-VIServer -server $VCenter -User $User -Password $PWD -AllLinked:$true
$Report = @()

$VMs = Get-VM | Sort-Object Name
Foreach ($ColVM in $VMs) {
  #Gathering Info on $ColVM   
  $report += $ColVM | Select Name, VMHost, PowerState,
 
@{N="OSFullName";E={($_ | Get-VMGuest).OSFullName}},
 
@{N="IPAddress";E={($_ | Get-VMGuest).IPAddress}},
 
@{N="AdapterName";E={([STRING]::JOIN(',',($_ | Get-NetworkAdapter | %{$_.Name})))}},
  @{N="AdapterType";E={([STRING]::JOIN(',',($_ | Get-NetworkAdapter | %{$_.Type})))}},
 
@{N="Datacenter";E={($_ | Get-Datacenter).Name}},
 
@{N="CardCount";E={($_ | Get-NetworkAdapter).Count}} } ##### DECONNEXION VCENTER & KILL EXCEL PROCESS ##### Disconnect-VIServer -server * -Confirm:$false -Force:$true
Export-Csv
C:\vmreport.csv -NoTypeInformation -UseCulture


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

0 Kudos
juliennas
Contributor
Contributor
Jump to solution

I launched the script. It should finished in a couple of minutes

Le 5 déc. 2012 15:03, "LucD" <communities-emailer@vmware.com<mailto:communities-emailer@vmware.com>> a écrit :

VMware Communities<http://communities.vmware.com/index.jspa>

Powercli to report VM Nics AdapterType

reply from LucD<http://communities.vmware.com/people/LucD> in VMware vSphere™ PowerCLI - View the full discussion<http://communities.vmware.com/message/2158651#2158651

0 Kudos
juliennas
Contributor
Contributor
Jump to solution

I think, the problem is located with SRM protected VMs.

The command doesn t fill excel while it checks protected VMs, which are in a specific ressource pool and a specific folder ...

Do you know a way to filter VMs ? To get all the VMs excepted SRM ressource pool or excepted SRM folder

I have 1 datacenter, with no ressource pool, which is Production

I have a 2sd datacenter, with SRM ressource pool and "Hors-Prod" ressource pool

By the way, thanks a lot for your help Smiley Happy

Julien

0 Kudos
LucD
Leadership
Leadership
Jump to solution

There are many ways to filter VMs, but you need a condition to use in the filter.

What distinguishes these SRM VMs from the rest.

Are they stored on a sperate datastore, in a specific resourcepool, do they have a custom attribute... ?

In your case would the test if they NOT belong to the Hors-Prod resourcepool sufficient ?


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

LucD
Leadership
Leadership
Jump to solution

Something  like this

$vms = Get-ResourcePool | where {$_.Name -ne "SRM Resource Pool"} | Get-VM 


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

0 Kudos
juliennas
Contributor
Contributor
Jump to solution

Thanks Lucd for your help.

I ll have a try on this tomorrow morning.

Good evening,

Julien

Le 5 déc. 2012 18:09, "LucD" <communities-emailer@vmware.com<mailto:communities-emailer@vmware.com>> a écrit :

VMware Communities<http://communities.vmware.com/index.jspa>

Powercli to report VM Nics AdapterType

reply from LucD<http://communities.vmware.com/people/LucD> in VMware vSphere™ PowerCLI - View the full discussion<http://communities.vmware.com/message/2158797#2158797

0 Kudos
juliennas
Contributor
Contributor
Jump to solution

Hi LucD,

I've tried the command, but with no success. I don't know why ! Protected VMs with SRM are in the Folder "SRM"  and in the resource pool "Prod-SRM" in the vCenter inventory, and the command

$VMs =  Get-ResourcePool | where {$_.Name -ne "Prod-SRM"} | Get-VM

returns nothing ...

If I do only Get-ResourcePool I see Resources twice (2 clusters with vCenters in linked mode), Rec and Prod-SRM

If I do Get-ResourcePool | where {$_.Name -eq "Prod-SRM"} | Get-VM , it returns nothing

If I do Get-ResourcePool | where {$_.Name -eq "Rec"} | Get-VM , it returns all the VMs, including those which should be in Prod-SRM ...

If I do Get-Folder -Type VM | where {$_.Name -ne "SRM" } | Get-VM, it returns all the VMs, including those which should be in SRM folder

I'm searching a way to filter this, including datacenter name and folder name in excel. So I can filter value in the report.

Julien

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Does

Get-ResourcePool | where {$_.Name -ne "Prod-SRM"}

return the resourcepool ?

If not, try

Get-ResourcePool | where {$_.Name -match "^Prod"}


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

0 Kudos
juliennas
Contributor
Contributor
Jump to solution

Get-ResourcePool | where {$_.Name -ne "Prod-SRM"}

returns resource pool "resources" and "REC"

Get-ResourcePool | where {$_.Name -ne "Prod-SRM"} | Get-VM

returns still all VMs like if the filter doesn't exist ... 😕

Also this does the same VMs listing Get-ResourcePool | where {$_.Name -notmatch "^Prod"} | Get-VM | Sort Name

I've modified the script to do like this,

CLS
#Init
$VCenter         = ""
$User             = ""
$PWD            = ""
$Date            = Get-Date -format yyyy-MM-dd
$ExportPath        = "C:\Scripts\PS\02 - Exports\VMware\VMs_Management\"
$ReportName        = $Date  + "--VMsNicTypeReport.xls"
$FILENAME        = $ExportPath + $ReportName

##### CREATE EXCEL #####
$Excel = New-Object -Com Excel.Application
$Excel.visible = $false
$Excel = $Excel.Workbooks.Add(1)

$VM = $Excel.WorkSheets.Item(1)
$VM.Name = "Network Type"
$VM.Cells.Item(1,1) = "VM Name"
$VM.Cells.Item(1,2) = "OS Name"
$VM.Cells.Item(1,3) = "IP Address"
$VM.Cells.Item(1,4) = "VM HOST"
$VM.Cells.Item(1,5) = "VM STATUS"
$VM.Cells.Item(1,6) = "Adapter Type"
$VM.Cells.Item(1,7) = "Adapter Name"
$VM.Cells.Item(1,8) = "Folder"
$VM.Cells.Item(1,9) = "DataCenter"


$WorkBook = $VM.UsedRange
$WorkBook.Interior.ColorIndex = 0
$WorkBook.Font.ColorIndex = 5
$WorkBook.Font.Bold = $True
$WorkBook.HorizontalAlignment = -4108
$VmRange = $VM.range("A1:Z500")
$Vmrange.font.size = 8
$intRow = 2

##### Functions          #####
Function ExcelReport {
        #Gathering Info on $ColVM   
        $VMsItems     = Get-VM -Name $ColVM | Select Name, VMHost, PowerState,
            @{N="OSFullName"    ;E={($_ | Get-VMGuest).OSFullName}},
            @{N="IPAddress"        ;E={($_ | Get-VMGuest).IPAddress}},
            @{N="Folder"        ;E={$_.Folder.Name}},
            @{N="AdapterName"    ;E={([STRING]::JOIN(',',($_ | Get-NetworkAdapter | %{$_.Name})))}},
            @{N="AdapterType"    ;E={([STRING]::JOIN(',',($_ | Get-NetworkAdapter | %{$_.Type})))}}
       
        #Filling Excel Report
            $VM.Cells.Item($IntRow,1) = $ColVM.Name
            $VM.Cells.Item($IntRow,2) = [STRING]$VMsItems.OSFullName
            $VM.Cells.Item($IntRow,3) = [STRING]$VMsItems.IPAddress
            $VM.Cells.Item($IntRow,4) = [STRING]$ColVM.VMHost
            $VM.Cells.Item($IntRow,5) = [STRING]$ColVM.PowerState
                If        ($ColVM.PowerState -eq "PoweredOn")
                        {$VM.Cells.Item($IntRow,5).Interior.ColorIndex = 4}
                Else    {$VM.Cells.Item($IntRow,5).Interior.ColorIndex = 3}
            $VM.Cells.Item($IntRow,6) = [STRING]$VMsItems.AdapterType
                If        ($VMsItems.AdapterType -like "*Vmxnet3*")   
                        {$VM.Cells.Item($IntRow,6).Interior.ColorIndex = 4}
                Else     {$VM.Cells.Item($IntRow,6).Interior.ColorIndex = 3}
            $VM.Cells.Item($IntRow,7) = [STRING]$VMsItems.AdapterName
            $VM.Cells.Item($IntRow,8) = $VMsItems.Folder
            $VM.Cells.Item($IntRow,9) = $DataCenter.Name
            $IntRow ++    }

##### CONNECTING VCENTER #####
Connect-VIServer -server $VCenter -User $User -Password $PWD
$DataCenters     = Get-Datacenter | Sort-Object Name

Foreach ($DataCenter in $DataCenters){
    $VMs = Get-VM -Location $DataCenter | Sort Name
    Foreach ($ColVM in $VMs) {
            ExcelReport    }}

$WorkBook.EntireColumn.AutoFit()
$VM.Columns.Item(2).ColumnWidth = 25

##### Saving Report #####
$Excel.SaveAs($FILENAME)

##### Disconnect VCENTER & Kill EXCEL Process #####
Disconnect-VIServer -server * -Confirm:$false -Force:$true
#Stop-Process -processname excel

The script is running now, I'll tell you the result when it'll be over.

Julien

0 Kudos