VMware Cloud Community
golddiggie
Champion
Champion
Jump to solution

Need to gather information about VMs in specific datacenters in environment

I need to pull up a bunch of information for the VMs living in specific datacenters within a vCenter (5.1, ugh) environment right now. There are three datacenters with clusters within each (plus some hosts not in clusters, but still within the datacenter items). With about 4000 VMs total, I really need a solid way to pull the information from just one datacenter, or even from a single cluster/host within each set. The first target is the smallest, with only 18 hosts and 111 VMs listed in the summary of the DC.

I've found some scripts to pull the information (or a good chunk of it) but I really need it to be more specific (instead of shotgunning to everything).

Information I really need to pull:

VM name (obviously)

vNIC with portgroup connected to, plus how many of them (if more than one at least) along with the adapter type selected (e1000, etc.)

VMDK information including how many and which datastore it's on as well as what the type of datastore it is (VMFS, NAS, etc.).

Snapshot if present (I'd also prefer to have the total snapshot size if easy, but I can always gather that later).

Affinity rule(s) applied against the VM(s). If this can include which rule is applied, that would be great.

IMO, easy stuff to add would be the OS inside the VM, IP address, and power state. I'd also like to have the location information for the VM. Such as which datacenter, cluster and host. Or at least have that available and either enabled or disabled easily (add/remove # in front of the lines).

I've been asked to get all of this for the first target before end of the week. I'd like to have it sometime before EOD tomorrow. One of the scripts I found earlier today is still runnning.

Get-VM | Select-Object Name, Powerstate, NumCPU, MemoryGB, @{N="Noofdisk"; E={($_ | Get-HardDisk).count}}, HardDisks, @{N="Datastore"; E={($_ | Get-Datastore).Name}}, @{N="DiskState"; E={($_ | Get-HardDisk).storageformat}}, ProvisionedSpaceGB,  @{N="Snapshot"; E={($_ | Get-snapshot).count}}, VMHost,   @{N="OS"; E={$_.ExtensionData.summary.config.guestfullname}} | Export-Csv -NoTypeInformation -UseCulture -Path "C:\powercli_reports\Inventory4Result.csv$(Get-Date -f MM-dd-yyyy-H-mm-ss).csv"

The above script just finished and pulled up all the info listed in it (good sign). Can I simply add "NetworkAdapter," to the list to get the adapter type for each VM?? I'd still like to narrow down the target to just the DC/cluster I need to focus on (at each run time). Even if it means altering the script to be target specific. I'm also going to still need the rest of the info I listed above added to this. With each run taking over an hour (around 1-1/4 to 1-1/2 hours) it would be better. Especially when I can target a smaller group and get the results faster.

Hopefully someone will answer up soon (like LucD). Smiley Wink

update: added the "Get-Datacenter ="<datacenter>" | " at the start to narrow it down. I can change it to a cluster later when I start hitting the one with over 3000 VMs in it (so it takes less time).

Still trying to figure out how to get the Network Adapter Type added to the list. I've tried added "@{N="NetworkAdapter"; E={($_ | Get-NetworkAdapter)}}" but it's not giving me the desired result...

Update2: changed the NIC line to "@{N="NetworkAdapter"; E={($_ | Get-NetworkAdapter).type}}" and that pulls the info needed for that section... Need to figure out how to add a NIC count line to this too (hope what I'm thinking will work, but will see).

Update3: I've managed to get almost everything needed. Except for a way to get the DRS affinity rule for any VMs they are applied against. Since this is going against a DC, is that just not possible? I'll consider pushing the script against a cluster tomorrow.

Message was edited by: golddiggie

Tags (1)
80 Replies
golddiggie
Champion
Champion
Jump to solution

Understood... I'm looking to see what text needs to be added in order to pull up the IP address (or addresses) for any NICs in use on a VM in the list. Plus I'm thinking it would be good to add "power state" and "OS" to the list of items... I just need to see how I update the lines of great code you provided to include these items... Plus what I'll need to do to add more in the future. If I'm thinking correctly, it could just be a matter of changing the actual calls. Such as for IP address make it "IP = $IPAddress"... I'll need to look to make sure that's the correct item before I start plugging things in. Won't be difficult (I hope) to add these.

Well, I didn't get an error message for the change, but no IP address data was put into the field...

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Give it a try (that's the best way for learning things).

If you're stuck, give a shout.


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

0 Kudos
golddiggie
Champion
Champion
Jump to solution

Updated the earlier post... Didn't pull data...

0 Kudos
LucD
Leadership
Leadership
Jump to solution

How do you fetch the IP address from the VM for a specific vNic?


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

0 Kudos
golddiggie
Champion
Champion
Jump to solution

I changed my earlier code to "IP =  $vm.extensiondata.guest.IPAddress" and it works... Need to look up the power state info call next...

Progress. Smiley Happy

I'm just using it to pull the main IP address for the VM (I believe) with this... I didn't see multiple IP addresses listed in the results from yesterday, or the info I was provided for the set of VMs the other day.

0 Kudos
golddiggie
Champion
Champion
Jump to solution

So "Powered = $PowerState" doesn't pull any info into the spreadsheet... Still looking to see what I need to use instead (or add to it) to make it work... Tried a couple of variations on that line, without any joy... $VMPowerState doesn't work... Nor $vminfo.PowerState...

Any thoughts or insight that will help illuminate my granite dome??

0 Kudos
golddiggie
Champion
Champion
Jump to solution

This is frustrating... None of the things I've tried to use to pull the power state info are working... From what I've seen in other scripts, it should be a simple call... Maybe I'm doing something else incorrectly here to get the info listed.

0 Kudos
golddiggie
Champion
Champion
Jump to solution

So now I also need to add actual drive consumption for each VM that gets info pulled as well as the memory allocation for each.

Any chance you could give me the lines (or just update the code) to reflect?? plus add the power status call since I can't seem to get that bugger to work.

0 Kudos
golddiggie
Champion
Champion
Jump to solution

Managed to get the calls to work for the VM RAM allocation plus the power status is now working... So closer to having what I need. I'm hoping that I can get the VMDK consumed space shortly. I had one script that showed the provisioned space, but I really need just what each VMDK is using.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you recap what extra properties you need in the report?


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

0 Kudos
golddiggie
Champion
Champion
Jump to solution

Right now I'm just looking to add info on how much virtual disk space is actually consumed (on datastore). Provisioned space is not desired since the VMs could be provisioned for a TB but only actually using 100GB (most are thin provisioned).

I'm looking for the line I need to add for the OS of the VM to also include... Got it (updated the info below)... If you know of better code to use, I'm all ears.

This is what I've updated your script to look like...

$dcName = "DaDC (fake name)"

$report = foreach($dc in Get-Datacenter -Name $dcName){

   foreach($cluster in Get-CLuster -Location $dc){

       $vms = Get-VM -Location $cluster

       $maxHD = ($vms.ExtensionData.Layout | %{$_.Disk.Count} | Measure-Object -Maximum).Maximum

       $maxNic = ($vms.ExtensionData | %{$_.Network.Count} | Measure-Object -Maximum).Maximum

       foreach($vm in $vms){

           $hd = Get-HardDisk -VM $vm

           $nic = Get-NetworkAdapter -VM $vm

           $drsRule = Get-DrsRule -Cluster $cluster -VM $vm

           $snap = Get-Snapshot -VM $vm

     

           $obj = [ordered]@{

               Datacenter = $dc.Name

               Cluster = $cluster.Name

               VMHost = $vm.VMHost.Name

               VM = $vm.Name

               OS = $vm.Guest.OSFullName

               VMRAM_GB = [Math]::Round(($vm.MemoryGB),2)

               Powered = $vm.PowerState

               IP =  $vm.extensiondata.guest.IPAddress

               DrsRule = $drsRule.Name -join'|'

               HDNumber = $hd.Count

               vNicNumber = $nic.Count

               SnapNumber = $snap.Count

               SnapSizeGB = [math]::Round(($snap | Measure-Object -Property SizeGB -Sum).Sum,2)

           }

           1..$MaxNic | %{

               if($_ -le $nic.Count){

                   $obj.Add("NIC$($_)PG",$nic[$_ -1].NetworkName)

                   $obj.Add("NIC$($_)Type",$nic[$_ -1].Type)

               }

               else{

                   $obj.Add("NIC$($_)PG",'')

                   $obj.Add("NIC$($_)Type",'')

               }

           }

           1..$MaxHD | %{

               if($_ -le $hd.Count){

                   $ds = Get-Datastore -RelatedObject $hd[$_ -1]

                   $obj.Add("VMDK$($_)DS",$ds.Name)

                   $obj.Add("VMDK$($_)DSType",$ds.Type)

               }

               else{

                   $obj.Add("VMDK$($_)DS",'')

                   $obj.Add("VMDK$($_)DSType",'')

               }

           }

           New-Object PSObject -Property $obj

       }

   }

}

$report | Export-Csv "C:\powercli_reports\InventoryReportDC_calculating03_$(Get-Date -f MM-dd-yyyy-H-mm-ss).csv"

0 Kudos
LucD
Leadership
Leadership
Jump to solution

There is a bit of a catch here.

The VM keeps track how much space is used on a datastore, but not per harddisk.

So if you have 2 harddisks on 1 datastore, and a 3th harddisk on a second datastore, you can get 2 values.

Each value will show how much space is used on that datastore.

Meaning that for the datastores that holds 2 harddisk, you will only get the total.

How, and where, do you want to display that?


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

0 Kudos
golddiggie
Champion
Champion
Jump to solution

AFAIK, they're just looking to get the for the total SAN space consumed. If more than one datastore is used by a VM, then either getting the total for all, or just getting the total used (for space) by the VM should be fine. If two datastores are used, and the value is for each datastore, then they can run the numbers. If there are columns for these, so simple autosum can be used, that would work too. Let the rest figure out that. It should be easy enough once pulled into Excel.

I think it would be good to have it after the RAM column...

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The you just need to add a row to the object, the value should be $vm.UsedSpaceGB


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

0 Kudos
golddiggie
Champion
Champion
Jump to solution

Any easy way to get it to just go to one or two decimal places?? Or round up (ish)?? More to make it more clean than something like "120.411191524006", which could go to 120.4.. If it's more complicated, then I can leave it alone and let the rest of the guys deal with it. Smiley Wink

0 Kudos
LucD
Leadership
Leadership
Jump to solution

No, that's where the [Math] functions come in handy.

Try like this

DatastoreSizeGB = [math]::Round($vm.UsedSpaceGB,2)


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

0 Kudos
golddiggie
Champion
Champion
Jump to solution

Figured out how to round the number given for the space used. Much better now...

0 Kudos
golddiggie
Champion
Champion
Jump to solution

Yeah... Basically just used what I saw on a couple of other lines and figured it out...

At least I'm getting more time in PowerCLI now. Smiley Wink

0 Kudos
golddiggie
Champion
Champion
Jump to solution

Hope this is  a quick one...

Is there a simple way to add some kind of 'progress' bar to the script?? While it's not really needed for the smaller datacenter, for the large one (~3500 VMs) it could help to make sure the person running the script (especially if it's not me) knows it's actually running and hasn't locked up.

If that's not really an option, then I'll time the run before I release the script so that they know about how long it will take. With the info pulls in it now, it takes about two minutes to run on the ~110 VMs in the small datacenter.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this

$dcName = 'MyDC'

$report = foreach($dc in Get-Datacenter -Name $dcName){

   foreach($cluster in Get-CLuster -Location $dc){

   $vms = Get-VM -Location $cluster

   $maxHD = ($vms.ExtensionData.Layout | %{$_.Disk.Count} | Measure-Object -Maximum).Maximum

   $maxNic = ($vms.ExtensionData | %{$_.Network.Count} | Measure-Object -Maximum).Maximum

   $i = 0

   foreach($vm in $vms){

   $i++

   Write-Progress -Activity 'Gathering VM info' -Status "Looking at VM $($vm.Name)" -PercentComplete ($i/$vms.Count*100)

   $hd = Get-HardDisk -VM $vm

   $nic = Get-NetworkAdapter -VM $vm

   $drsRule = Get-DrsRule -Cluster $cluster -VM $vm

   $snap = Get-Snapshot -VM $vm

 

   $obj = [ordered]@{

   Datacenter = $dc.Name

   Cluster = $cluster.Name

   VMHost = $vm.VMHost.Name

   VM = $vm.Name

   DrsRule = $drsRule.Name -join'|'

   HDNumber = $hd.Count

   vNicNumber = $nic.Count

   SnapNumber = $snap.Count

   SnapSize = [math]::Round(($snap | Measure-Object -Property SizeGB -Sum).Sum,1)

   }

   1..$MaxNic | %{

   if($_ -le $nic.Count){

   $obj.Add("NIC$($_)PG",$nic[$_ -1].NetworkName)

   $obj.Add("NIC$($_)Type",$nic[$_ -1].Type)

   }

   else{

   $obj.Add("NIC$($_)PG",'')

   $obj.Add("NIC$($_)Type",'')

   }

   }

   1..$MaxHD | %{

   if($_ -le $hd.Count){

   $ds = Get-Datastore -RelatedObject $hd[$_ -1]

   $obj.Add("VMDK$($_)DS",$ds.Name)

   $obj.Add("VMDK$($_)DSType",$ds.Type)

   }

   else{

   $obj.Add("VMDK$($_)DS",'')

   $obj.Add("VMDK$($_)DSType",'')

   }

   }

   New-Object PSObject -Property $obj

   }

   }

}

$report | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


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

0 Kudos