VMware Cloud Community
LittleNickey
Enthusiast
Enthusiast
Jump to solution

Custom reports

I have created a report over all VM's in our environment with a few fields of interest, but I'd like to streamline it a bit.

The script (shorted down):

foreach($vm in Get-VM){

     #$countnics = foreach($vm in get-vm){$vm.NetworkAdapters.count}

     #$maxnics = $countnics | measure -maximum

     #$k = $maxnics.maximum

     ## Create custom table

     $Summary = "" | Select "VM Name", "Power Status", "vCPU", "MemGB", "Nic 1", "Nic 2", "Nic 3", "Nic 4"

     ## Add info to custom table

     $Summary."VM Name" = $vmview.Name

     $Summary."Power Status" = $vm.PowerState

     $Summary."vCPU" = $vm.NumCPU

     $Summary."MemGB" = [Math]::Round(($vm.MemoryGB),0)

     ## Get Network info

     $i = 1

     foreach ($nic in (Get-NetworkAdapter -vm $vm.Name)){

          $Summary."Nic $i" = $nic.NetworkName

          $i++

     }

}

So if no server has more than 3 NIC's, the last field will be empty for all servers.

So, what I wonder is if its possible to use the value $k which I commented out and do something like:

     $Summary = "" | Select "VM Name", "Power Status", "vCPU", "MemGB", "Nic 1..$k"


Any ideas or pointers?

-- Oskar
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

There is no need to define a maximum, you can add the properties dynamically.

$report = @()
foreach($vm in Get-VM){
    
## Create custom table
     $Summary = "" | Select "VM Name", "Power Status", "vCPU", "MemGB"

    
## Add info to custom table
     $Summary."VM Name" = $vmview.Name
    
$Summary."Power Status" = $vm.PowerState
    
$Summary."vCPU" = $vm.NumCPU
    
$Summary."MemGB" = [Math]::Round(($vm.MemoryGB),0)

    
## Get Network info
     $i = 1
    
Get-NetworkAdapter -VM $vm | %{
        
Add-Member -InputObject $Summary -MemberType NoteProperty -Name "NIC$i" -Value $_.NetworkName
       
$i++
     }
    
$report += $Summary
}
$report

Just watch out when you want to export the result to a CSV file, make sure the row containing the VM with the most NICs appears first (use the Sort-Object cmdlet).


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

View solution in original post

0 Kudos
5 Replies
Bawb
Enthusiast
Enthusiast
Jump to solution

(Get-NetworkAdapter -vm $vm.Name).count

that will give you a count of the number of objects

and you can increment i to till matches k

0 Kudos
LucD
Leadership
Leadership
Jump to solution

There is no need to define a maximum, you can add the properties dynamically.

$report = @()
foreach($vm in Get-VM){
    
## Create custom table
     $Summary = "" | Select "VM Name", "Power Status", "vCPU", "MemGB"

    
## Add info to custom table
     $Summary."VM Name" = $vmview.Name
    
$Summary."Power Status" = $vm.PowerState
    
$Summary."vCPU" = $vm.NumCPU
    
$Summary."MemGB" = [Math]::Round(($vm.MemoryGB),0)

    
## Get Network info
     $i = 1
    
Get-NetworkAdapter -VM $vm | %{
        
Add-Member -InputObject $Summary -MemberType NoteProperty -Name "NIC$i" -Value $_.NetworkName
       
$i++
     }
    
$report += $Summary
}
$report

Just watch out when you want to export the result to a CSV file, make sure the row containing the VM with the most NICs appears first (use the Sort-Object cmdlet).


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

0 Kudos
LittleNickey
Enthusiast
Enthusiast
Jump to solution

Thanks Luc, I will sure give that a go on Monday!

I've seen someone use Add-Member before (probably you amongst others), but never got the hang of it. Guess this is a good opportunity to learn!

what is modulus (%) an alias for btw? Don't remember if I've figured it out before or not.

And thanks for the heads up regarding the csv export.

-- Oskar
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That's an alias for the ForEach-Object cmdlet.

You can always check aliases by doing

Get-Alias %


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

0 Kudos
LittleNickey
Enthusiast
Enthusiast
Jump to solution

Thanks, it all worked great!

-- Oskar
0 Kudos