VMware Cloud Community
kraney
Contributor
Contributor

Script Slow: VCD Pulling External/Internal IP from Org/vApp

I was curious if any of the script masters could review the following.

This works fine, but it is pretty slow.  I'm hoping that the speed can be improved.  When I query against an organization, vapp, vms, networks that has 900+ vm, it is 4-6 hours to complete.

I would think that I could do this quicker, but I need a set of fresh eyes.

*The echos are just so I know that the script is doing something.  Watching a black screen for half a day is boring.*

$myOrg = Get-Org -Name "MyCoolOrg"
$vApps = Get-CIVApp -Org $myOrg
$vAppNetworkAdapters = @()
foreach ($vApp in $vApps) {
        $vAPPLabel = "vAPP:" + " " + $vApp.Name
        echo $vAPPLabel
        $vms = Get-CIVM -VApp $vApp
        foreach ($vm in $vms) {
                $vmLabel = "VM Name:" + " " + $vm.Name
                echo $vmLabel
                $vAppNicInfo = Get-CINetworkAdapter -VM $vm | Select  `
                                                                    @{Label="vApp";Expression={$vApp.name}}, `
                                                                    @{Label="VM";Expression={$vm.name}}, `
                                                                    @{Label="NIC";Expression={$_.Index}}, `
                                                                    @{Label="Internal IP Address";Expression={$_.IPAddress}}, `
                                                                    @{Label="External IP Address";Expression={$_.ExternalIPAddress}}
                $vAppNetworkAdapters += , $vAppNicInfo
               
         }
        
}
$vAppNetworkAdapters | export-csv c:\MyCoolOrg.csv -NoTypeInformation

Thanks in advance.

-Kevin

0 Kudos
6 Replies
LucD
Leadership
Leadership

Moved to the vCD PowerCLI community


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

0 Kudos
CRad14
Hot Shot
Hot Shot

I am not familiar with the VCD cmdlets at all, but I can provide maybe a couple of tips in general that could help.

The one thing you may want to take a look at though is the number of  "Get" commands you are running.... Namely that you are doing a Get-CINetworkAdapter for each and every $vm and  a Get-CIVM for every vapp... that is probably where most of your slowness is coming from....

It would be must faster to do those Gets before the FOREach statements, and then just do a filter,  or a where-object in the foreach...

I hope this helps

Conrad www.vnoob.com | @vNoob | If I or anyone else is helpful to you make sure you mark their posts as such! 🙂
0 Kudos
kraney
Contributor
Contributor

I guess that's what I'm looking for.

I'm not sure how to get the information for each machine without having to execute a get command for each machine.

The slowness is definately when it's polling the network information for the specified machine.  Is it possible to collect the netowrk information in another way?

0 Kudos
CRad14
Hot Shot
Hot Shot

Again, I don't use VCD, but this is a relative example

$vApps=Get-vApp

$vms=get-vm | Where-Object {$_.vapp -like $vapps}

$Nics=$vms |get-vmnetworkinterface

Now that I have all the data I need, I just need to filter it down with the foreach statements.....

Also, it looks like there really isn't much point to your "Foreach ($vapp in $vapps)" loop

You could do "Get-CIVapp  | get-CIVM"  to get all your VMs, and for your vapp label, just do "$vm.civapp.name"

Alright, you convinced me I am just going to rewrite this for you real quick....

$myOrg = Get-Org -Name "MyCoolOrg"
$vApps = Get-CIVApp -Org $myOrg
$vAppNetworkAdapters = @()

$vms= $vapps | Get-CIVM

$allnics= Get-CIVM | Get-CINetworkAdapter


                $vmLabel = "VM Name:" + " " + $vm.Name
                echo $vmLabel
                $vAppNicInfo = $allnics|  Select  `
                                                                    @{Label="vApp";Expression={$_.vm.vApp.name}}, `
                                                                    @{Label="VM";Expression={$_.vm.name}}, `
                                                                    @{Label="NIC";Expression={$_.Index}}, `
                                                                    @{Label="Internal IP Address";Expression={$_.IPAddress}}, `
                                                                    @{Label="External IP Address";Expression={$_.ExternalIPAddress}}
                $vAppNetworkAdapters += , $vAppNicInfo
               

$vAppNetworkAdapters | export-csv c:\MyCoolOrg.csv -NoTypeInformation

Here I was able to take out all the loops, so it won't give you a "Status" as you were having it do before, but it should run MUCH faster since it won't have to do several hundred Gets.....

Also I completed this using the reference http://pubs.vmware.com/vsphere-51/topic/com.vmware.powercli.cmdletref.doc/CIVM.html

So i haven't been able to test, you may want to make sure it work with just one get-civm or something first Smiley Happy

Conrad www.vnoob.com | @vNoob | If I or anyone else is helpful to you make sure you mark their posts as such! 🙂
0 Kudos
CRad14
Hot Shot
Hot Shot

Did this work for you?

Conrad www.vnoob.com | @vNoob | If I or anyone else is helpful to you make sure you mark their posts as such! 🙂
0 Kudos
drebsdorf
Contributor
Contributor

I don't know about that guy. But I ran into the same problem, with slow queries.

I've tested your code, and it hangs for quite a while on "$allnics= Get-CIVM | Get-CINetworkAdapter"

Feels like the same amount if time it would have used with the old Loop-Get-Loop method.

0 Kudos