VMware Cloud Community
Vlad_Belo
Enthusiast
Enthusiast
Jump to solution

VMs within vApps

Hi All,

I have this script which exporting all VMs within vApps with the info I need.

for the vApp name its fine.

but for rest of the list 'VM_Name', 'State', 'CPU' , 'RAM_GB' , 'IPAddress' 

its showing duplications.

for example:
vApp1 have 3 VMs - VM1, VM2, VM3. all different IP all different CPU and RAM.

but in the report it shows like so:

vApp1 - VM1 , PoweredOn, CPU:4, RAM:12, IP:10.10.10.10

vApp1 - VM1 , PoweredOn, CPU:4, RAM:12, IP:10.10.10.10

vApp1 - VM1 , PoweredOn, CPU:4, RAM:12, IP:10.10.10.10

instead of showing the different VMs with different cpu, ram, ip, state.

which should be like so (for example) :

vApp1 - VM1 , PoweredOn, CPU:4, RAM:12, IP:10.10.10.10

vApp1 - VM2 , PoweredOn, CPU:2, RAM:12, IP:10.10.10.11

vApp1 - VM3 , PoweredOff, CPU:6, RAM:16, IP:10.10.10.12

this is the script:

 

 

 

$vApps = Get-Cluster -Name "cluster" | Get-VApp | Select Name -ExpandProperty name
$outputcsv = @()

ForEach ($vApp in $vApps) {
        $VMs = Get-VApp -Name $vApp | Get-VM
        $itemline = "" | Select "vApp_Name", "VM_Name", "State", "CPU", "RAM_GB" , "IPAddress"
        $itemline.vApp_Name = $($vApp.name)
        
        ForEach ($VM in $VMs) {
            $itemline.VM_Name = $VM.Name
            $itemline.State = ($VM.PowerState)
            $itemline.CPU = ($VM.NumCpu)
            $itemline.RAM_GB = ($VM.MemoryGB)
            $itemline.IPAddress = $($VM.Guest.IPAddress)
            $outputcsv += $itemline
        }
}
$outputcsv | Export-Csv -Path "path\vapplist.csv" -NoTypeInformation -UseCulture

 

 

 

 

can any one see what I'm missing? 

trying to figure it out

 

Thanks in advance for the help!

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You are creating $itemline outside the loop over all the VMs in the vApp, hence it will be the same variable each loop inside the loop over all the VMs in the vApp.

But why so complicated with nested loops, use the pipeline.
Something like this for example

Get-Cluster  -Name 'cluster' -PipelineVariable cluster |
Get-VApp -PipelineVariable vapp |
Get-VM -PipelineVariable vm |
Select-Object @{N = 'vApp_Name'; E = { $vapp.Name } },
@{N = 'VM_Name'; E = { $vm.Name } },
@{N = 'State'; E = { $vm.PowerState } },
@{N = 'CPU'; E = { $vm.NumCpu } },
@{N = 'RAM_GB'; E = { $vm.MemoryGB } },
@{N = 'IPAddress'; E = { $vm.Guest.IPAddress } }

The PipelineVariable allows you store the object in the pipeline at that point in a variable for later reference.


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

View solution in original post

2 Replies
LucD
Leadership
Leadership
Jump to solution

You are creating $itemline outside the loop over all the VMs in the vApp, hence it will be the same variable each loop inside the loop over all the VMs in the vApp.

But why so complicated with nested loops, use the pipeline.
Something like this for example

Get-Cluster  -Name 'cluster' -PipelineVariable cluster |
Get-VApp -PipelineVariable vapp |
Get-VM -PipelineVariable vm |
Select-Object @{N = 'vApp_Name'; E = { $vapp.Name } },
@{N = 'VM_Name'; E = { $vm.Name } },
@{N = 'State'; E = { $vm.PowerState } },
@{N = 'CPU'; E = { $vm.NumCpu } },
@{N = 'RAM_GB'; E = { $vm.MemoryGB } },
@{N = 'IPAddress'; E = { $vm.Guest.IPAddress } }

The PipelineVariable allows you store the object in the pipeline at that point in a variable for later reference.


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

Vlad_Belo
Enthusiast
Enthusiast
Jump to solution

thanks LucD ! 
learned something new now

its working great now

0 Kudos