VMware Cloud Community
pbalderos
Enthusiast
Enthusiast

PowerCli Script To Inventory VMs in a vCenter?

Hello Peeps!

I am teaching myself powerCLI and just found a need for a script. I am doing an inventory of all of our virtual machines across several vCenter servers. Does anyone have a script that can spit out the following info into a csv? I know I would have to connect to each vCenter in PowerCli and the script. Any help would be great. As for now I have just been trying to make sense of what the google is providing.

VMName

Domain

vCenter

IP Address (s)

Operating System

vmversion

CPUs

Cores

Memory

VMware tools version

ClusterName

Phillip Balderos VCP6-DCV
5 Replies
UmeshAhuja
Commander
Commander

Hi,

Everything that you need with inventory via script --> Script Complete vCenter Inventory with Powershell and PowerCLI

Thanks n Regards
Umesh Ahuja

If your query resolved then please consider awarding points by correct or helpful marking.
pbalderos
Enthusiast
Enthusiast

Thank you Umesh

I am a little bit confused about that script. I don't see the part where I should add my vCenter server as the variable for $vc at the line Connect-VIServer -Server $vc -WarningAction 0 -ErrorAction Stop | Out-Null

Phillip Balderos VCP6-DCV
0 Kudos
ScottDriver42
Enthusiast
Enthusiast

I keep a script for just this purpose called "query_all_vcenters".

### this needs to be updated for each vCenter in the environment

$VC="vc01", "vc02"

foreach($server in $VC){

    connect-viserver $server -WarningAction SilentlyContinue

    $server

     <#

     ######query goes here

     #>

    Disconnect-VIServer -confirm:$false

}

For your need, you are probably going to want to create a custom hashtable. The below code is plucked from several scripts, and hasn't been tested, so you'll need to validate.

###array object to hold all of your server objects

$colwholeenv=@()

###iterate through all your VM's, build a custom PS object for each and dump it into the array

foreach ($machine in $(get-vm)){

$serverobject = new-object system.object

   $serverobject | Add-Member -type NoteProperty -name Name -value $machine.Name

   $serverobject | Add-Member -type NoteProperty -name Memory -value $($(get-vm $serverobject.Name.ToLower()).MemoryGB.ToString() + " GB") -Force

   $serverobject | Add-Member -type NoteProperty -name CPU_Cores -value $(get-vm $serverobject.Name.ToLower()).NumCPU.ToString() -Force

   $serverobject| Add-Member -type NoteProperty -name OS -value $machine.guestid    ###could also use guestFullName

   $serverobject| Add-Member -type NoteProperty -name ToolsVersion -value $machine.ExtensionData.Guest.ToolsVersion

   $serverobject| Add-Member -type NoteProperty -name IP -value $machine.ExtensionData.Guest.IPAddress

   $serverobject| Add-Member -type NoteProperty -name Cluster -value $($machine|get-cluster).name

   $serverobject| Add-Member -type NoteProperty -name VMversion -value $machine.version

    $serverobject| Add-Member -type NoteProperty -name Domain -value ###placeholder for domain. perhaps invoke-vmscript?

   $colwholeenv+=$serverobject

}

### For some reason I've never been very good with export-csv, but *I think* this should work for you. Drop this at the end of your script once you've iterated through all of your vCenters

$colwholeenv | export-csv -path PathToYourCSVfile.csv

I'm newly active to the community, so if you have found this to be helpful, I'd appreciate you hitting the like button.

Cheers!

Blog: https://virtualvt.wordpress.com/ | Twitter: VTsnowboarder42
pbalderos
Enthusiast
Enthusiast

Thanks Scott!

I'll play around with it and test it in my lab before production.

Phillip Balderos VCP6-DCV
ScottDriver42
Enthusiast
Enthusiast

pbalderos‌ Just wondering how those snips worked for you? Let me know if you need additional help.

Blog: https://virtualvt.wordpress.com/ | Twitter: VTsnowboarder42
0 Kudos