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

OK, can you look at the file I copied into my earlier message?? It appears to be scanning the entire configuration, not just the cluster I selected.

0 Kudos
golddiggie
Champion
Champion
Jump to solution

I also get the following message when I run your script...

Export-Csv : Cannot bind argument to parameter 'InputObject' because it is null.

At C:\Users\v-agoldberg\Desktop\powercli_scripts\DC_scripts\test.ps1:51 char:11

+ $report | Export-Csv C:\powercli_reports\InventoryReport_$(Get-Date - ...

+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidData: (:) [Export-Csv], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCsvCommand

Ideas???

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The following line in your script does that.

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

The error message might have multiple reasons, the $report variable might be empty.

Also for the filename you seem to be using, you are using variable substitution to get the date in the filename.

Then you better place the filename between double quotes.

Something like this

$report | Export-Csv -Path "C:\powercli_reports\InventoryReport_$(Get-Date -Format 'yyyyMMdd').csv"


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

0 Kudos
golddiggie
Champion
Champion
Jump to solution

I altered line 7 on your uploaded script to include "$report =" (so it became "$report = foreach($vm in $vms){")

That cleared out the first error... Going to try running it again with all the parameters I had already created in the version I've been using...

Should I alter the script I pasted additionally to make it just grab the cluster that's the target??

0 Kudos
golddiggie
Champion
Champion
Jump to solution

Latest attempt/version loops on the target cluster...

$cluster = Get-Cluster | Out-GridView -OutputMode Single -Title 'Select 1 cluster'

$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

$report = foreach($vm in $vms){

        $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

               VM_Host = $vm.VMHost.Name

               VM = $vm.Name

   "Power state" = $vm.PowerState

   "IP Address" =  $vm.extensiondata.guest.IPAddress

   "Operating System" = $vm.Guest.OSFullName

   "VMware Tools Status" = $vm.ExtensionData.Guest.ToolsStatus

   "Tools Version" = $vm.ExtensionData.Guest.ToolsVersion

   "RAM Allocated(GB)" = [Math]::Round(($vm.MemoryGB),3)

   "NumCPU" = $vm.NumCPU

   "Cores/Socket" = $vm.CoresPerSocket

   "Disk Space(GB consumed)" = [Math]::Round(($vm.UsedSpaceGB),3)

               "DRS Affinity Rule" = $drsRule.Name -join'|'

               "HD Count" = $hd.Count

               "vNic Count" = $nic.Count

               "SnapShots" = $snap.Count

               "Size(GB)" = [math]::Round(($snap | Measure-Object -Property SizeGB -Sum).Sum,3)

   "Created" = $snap.Created

           }

           1..$MaxNic | %{

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

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

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

               }

               else{

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

                   $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

       }

   Write-Progress -Activity 'Gathering VM info' -Completed

   }

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

Ideas???

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try this one.


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

0 Kudos
golddiggie
Champion
Champion
Jump to solution

File won't download... Keeps failing... See a "file incomplete' message

0 Kudos
golddiggie
Champion
Champion
Jump to solution

Did a little head scratching and tried to think more logically about this and got it to not loop on me by cleaning up a couple of lines (removed two that were essentially repeating the lines before them).

Updated file:

$cluster = Get-Cluster | Out-GridView -OutputMode Single -Title 'Select 1 cluster'

$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

$report = 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

               VM_Host = $vm.VMHost.Name

               VM = $vm.Name

   "Power state" = $vm.PowerState

   "IP Address" =  $vm.extensiondata.guest.IPAddress

   "Operating System" = $vm.Guest.OSFullName

   "VMware Tools Status" = $vm.ExtensionData.Guest.ToolsStatus

   "Tools Version" = $vm.ExtensionData.Guest.ToolsVersion

   "RAM Allocated(GB)" = [Math]::Round(($vm.MemoryGB),3)

   "NumCPU" = $vm.NumCPU

   "Cores/Socket" = $vm.CoresPerSocket

   "Disk Space(GB consumed)" = [Math]::Round(($vm.UsedSpaceGB),3)

               "DRS Affinity Rule" = $drsRule.Name -join'|'

               "HD Count" = $hd.Count

               "vNic Count" = $nic.Count

               "SnapShots" = $snap.Count

               "Size(GB)" = [math]::Round(($snap | Measure-Object -Property SizeGB -Sum).Sum,3)

   "Created" = $snap.Created

           }

           1..$MaxNic | %{

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

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

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

               }

               else{

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

                   $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

       }

   Write-Progress -Activity 'Gathering VM info' -Completed

  

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

Only thing now is I need to figure out how to get the datacenter name field to populate from inventory. IIRC, it was using the information provided at the top of the script previously. So now I need to get it to pull that from inventory. I'll do some digging around for that command.

Basically the line "Datacenter = $dc.Name" needs to have "$dc.Name" replaced with the command to retrieve the DC name that the cluster is within.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I attached the file again, try again please.

In there I also have the Datacenter solution, you can just do

$dc = Get-Datacenter -VM $vm


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

0 Kudos
golddiggie
Champion
Champion
Jump to solution

It doesn't like that...

Get-Datacenter : 7/24/2018 1:51:38 PM   Get-Datacenter          Index was outside the bounds of the array.

At C:\Users\XXXXXXXXX\Desktop\powercli_scripts\DC_scripts\test.ps1:11 char:11

+     $dc = Get-Datacenter -VM $vm

+           ~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Get-Datacenter], VimException

    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetDatacenter

0 Kudos
golddiggie
Champion
Champion
Jump to solution

I checked the file it output from the last run... It has the datacenter info only for VMs that are powered on. :smileyconfused:

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Let me check that.


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

golddiggie
Champion
Champion
Jump to solution

I'm curious to know what you find... It probably won't help where I've been contracting but it could become useful where I'll be starting in <2 weeks. Smiley Happy

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Works for me, independent of the powerstate of the VM.

Can you check again with

Get-VM |

Select Name,PowerState,@{N='Datacenter';E={(Get-Datacenter -VM $_).Name}}


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

0 Kudos
golddiggie
Champion
Champion
Jump to solution

You want me to swap that with which line?? Or run it by itself?

Could it be because the environment here is running 5.1?? I didn't try it out on my home test lab (host is 6.5, vCenter is 6.7) last night. I could try it tonight to see if it works right there (at least I have one cluster there).

I don't expect to need to run the script where I've been contracting again. Mostly because I'm ending the contract to start a direct hire/perm role in less than two weeks. Smiley Happy

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Those lines are intended to be run separately.

To check if the datacenter is returned correctly.

And I haven't tested in 5.1, heck, I don't even have 5.5 anymore :smileygrin:

Good luck at the new job.


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

0 Kudos
golddiggie
Champion
Champion
Jump to solution

I suspect it's an issue with the installed level here. The project was to get them from 5.1 to 6u2 over the next ~8 months. Well, until I accepted a job offer at another company. MUCH larger environment at the new company (about 12x the size for hosts and about 7x the VM count). Going from a place that has Cisco UCS (and bitches about it constantly) to one using the HP C7000 chassis with either half, or full, height blades. Plus the new company is using all flash storage for all new setups. Also a major 'plus' is the "on-call" rotation won't start (for me) for about 6 months from my start date. Then once every 7 weeks (IIRC) and even then it's only from 9am to 9pm. Smiley Happy Great health benefits at a better cost (to me) than I've seen before. Which is important since I need to make Dr appointments in the coming month, or two, since I had to cancel them while unemployed. Not to mention needing lab work done (blood lab mostly, which isn't cheap).

I'm sure the people here aren't going to be happy about me bolting. But, I've been on this contract for not even 6 weeks, and have yet to actually DO anything. Well, other than run some scripts to gather info to help plan shit.

I plan to talk with the manager here today to let him know that I COULD finish the week, but I think it would just be wasting funds on their side. Since nothing was slated to start until the next two weeks. Hell, I don't even have all the accesses needed to DO anything for building hosts and such.

Waiting to see if I'll have a desktop or laptop at the new company. Smiley Happy

0 Kudos
golddiggie
Champion
Champion
Jump to solution

Just ran it on my home lab setup (6.7 vCSA, 6.5 host) and had no errors or missing information (that should be there). So, I'll just take it as being something screwy with an OLD environment and using the script.

0 Kudos
RJ4719
Contributor
Contributor
Jump to solution

Would you be willing to post the final version of your script, the last ver that LucD​ uploaded isn't running for me, Thanks so much

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Which version?
And what kind of errors are you getting?


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

0 Kudos