How to gather your vCenter inventory data with this VMware PowerShell script

3 years ago 372

Inventory reports are a communal petition erstwhile administering a VMware vCenter environment. Learn however this VMware PowerShell publication tin marque specified requests speedy and easy.

shutterstock-590267255.jpg

Image: Shutterstock/SidorArt

As idiosyncratic who administers a VMware virtual situation and has been done respective institution mergers, moving inventory audits is 2nd quality to me. It's ever a bully thought to support a existent inventory of your virtual machines successful bid to champion negociate the environment, but owed to the ever-changing quality of technology, wherever systems are built oregon decommissioned astatine a regular pace, it tin beryllium similar trying to deed a moving target.

As usual, scripting is the cardinal here. This VMware-based PowerShell publication tin assistance marque collecting your inventory information speedy and easy. My workfellow Brad Doran, elder method relationship manager astatine Amazon Web Services, provided maine this publication and I americium utilizing it present with his permission. You tin transcript and paste it to a section record called VMware_Inventory.ps1, for instance.

Prerequisites

Use Windows 10 (it's theoretically imaginable to tally this successful Windows 7, but you whitethorn acquisition headaches with outdated PowerShell versions since this publication requires PowerShell 4.0 astatine minimum).

Run Windows PowerShell ISE arsenic Administrator (you tin besides usage regular PowerShell, but I find ISE champion for maximum results arsenic good arsenic mistake evaluation).

Install the VMware PowerCLI module utilizing this remark wrong the PowerShell ISE Window:

Install-Module -Name VMware.PowerCLI

If you get an mistake that you request NuGet supplier mentation 2.8.201 conscionable click Yes and the process volition download and instal it for you. It volition instrumentality a fewer moments and you'll past request to click Yes to proceed, and successful a fewer much moments you volition spot the programme installing the related VMWare.PowerCLI packages.

SEE: Microsoft PowerShell: Learn however to automate your workday (TechRepublic Academy)

Since the publication requires the VMware.VimAutomation.Core module besides marque definite it's the latest mentation (as of October 2021) installed via this command:

Install-Module -Name VMware.VimAutomation.Core -RequiredVersion 10.0.0.7893909

Click Yes to proceed and successful a fewer much moments you volition spot the programme installing the related VMWare package.

If you person an mistake stating: "PackageManagement\Install-Package : Authenticode issuer 'CN=VeriSign Class 3 Public Primary Certification Authority - G5, OU="(c) 2006 VeriSign, Inc. - For authorized usage only", OU=VeriSign Trust Network, O="VeriSign, Inc.", C=US' of the caller module 'VMware.VimAutomation.Core' with mentation '10.0.0.7893909' is not matching with the authenticode issuer 'CN=DigiCert Trusted Root G4, OU=www.digicert.com, O=DigiCert Inc, C=US' of the previously-installed module 'VMware.VimAutomation.Core' with mentation '12.4.0.18627056'. If you inactive privation to instal oregon update, use-SkipPublisherCheck parameter"

run this command:

Install-Module -Name VMware.VimAutomation.Core -RequiredVersion 10.0.0.7893909 -SkipPublisherCheck

You tin besides spell present and download the latest mentation to ("C:\Program Files (x86)\VMWare\Infrastructure\PowerCLI\Modules"):

https://www.powershellgallery.com/packages/VMware.VimAutomation.Core/10.0.0.7893909

Customizing the script

The publication relies connected these variables (Figure A):

$vcs = @("vCenterFQDN") # FQDN of your vCenter server.
$logFile = "C:\Temp\VMInventory.csv" # Where you privation to prevention the CSV record
$vcsCluster = "*" # Filters which clump you privation to propulsion VM stats from
$businessUnitName = "Element" # Name of concern portion that the publication is gathering stats for

1.jpg

Figure A

For $vcs, you tin regenerate @("vCenterFQDN") with the sanction of your vCenter server successful quotes; different the publication volition punctual you to participate the name.

For $logFile, the logfile way and sanction tin beryllium changed accordingly.

For $vcsCluster you tin specify the clump to stitchery information from, oregon permission this enactment arsenic is to propulsion accusation from each clusters.

For $businessUnitName, regenerate the illustration "Element" introduction with your concern unit, section oregon group.

SEE: Checklist: Server inventory (TechRepublic Premium)

Running the script

Click File past Open and browse to wherever you saved the publication record to prime it past load it into PowerShell ISE. Hit the greenish fastener to execute it. 

If you get an mistake related to information policies oregon the publication being unsigned, tally this bid to let section scripts and distant signed scripts:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned 

If you near the $vcs adaptable arsenic is you volition beryllium prompted to participate the vCenter server sanction (Figure B).

2.jpg

Figure B

Once you participate the sanction you'll beryllium prompted to authenticate to vCenterj (Figure C). Make definite to usage an relationship with administrator-level credentials.

3.jpg

Figure C

You volition past spot the publication moving and actively gathering information (Figure D).

4.jpg

Figure D

The publication volition show the results, arsenic good arsenic prevention the details to "C:\Temp\VMInventory.csv" if you near the related adaptable arsenic is (Figure E).

5.jpg

Figure E

(Note I person removed the FQDN entries that were listed since this was tally against an progressive concern environment.)  

Thank you, Brad, for an fantabulous instrumentality each VMware head should person successful their toolkit!

Please interaction maine via the email nexus beneath (to the near of "Full Bio | See each of Scott's content") if you person immoderate questions oregon issues moving this script.

#Requires -Version 4 #Requires -Modules VMware.VimAutomation.Core <# .SYNOPSIS Create an inventory successful CSV format of virtual machines successful vCenter. .DESCRIPTION This publication is meant to execute an inventory of Virtual Machines. It tin link to aggregate vCenters to pull statistic from and it tin propulsion statistic from aggregate Host Clusters. This publication performs read-only operations. Output is formatted arsenic CSV utilizing a modular layout. Variable Details $vcs - An array containing the database of vCenter servers to link to. $logFile - The determination of wherever to prevention the output successful CSV format. $vcsCluster - The sanction of the cluster. It accepts wildcard characters, but it cannot beryllium empty. $businessUnitName - the busines unit, radical oregon section which owns/supports this environment Credential Requirements $vcsCreds - A idiosyncratic credential that has entree to login to vCenter with Read-Only rights astatine a minimum. $wmiCreds - A idiosyncratic credential that has entree to execute WMI queries locally connected the Windows Virtual Machines #> Clear-Host # Edit these variables for your circumstantial environment $vcs = @("vCenterFQDN") # FQDN of your vCenter server. $logFile = "C:\Temp\VMInventory.csv" # Where you privation to prevention the CSV file $vcsCluster = "*" # Filters which clump you privation to propulsion VM stats from. $businessUnitName = "Element" # Name of Business Unit that the publication is gathering stats for if($vcs -contains "vCenterFQDN"){ $vcs = Read-Host -Prompt "FQDN of vCenter Server" } $vcsCreds = Get-Credential -Message "vCenter Credentials" #$wmiCreds = Get-Credential -Message "WMI Credentials" Import-Module VMware.VimAutomation.Core Connect-VIServer $vcs -Credential $vcsCreds | Out-Null $vms = Get-Cluster -Name $vcsCluster | Get-VM $count = 0 $results = @() $Script:ProgressPreferenceOriginal = $ProgressPreference Clear-Host foreach($vm successful $vms){ # Progress Bar setup $count++ $percentComplete = [math]::Round(($count / $vms.Count) * 100,1) Write-Progress -Activity "Collecting info connected $($vm.Name)" -Status "$percentComplete% Complete" -PercentComplete $percentComplete # Store VM stat info successful PSObject $object = New-Object PSObject Add-Member -InputObject $object -MemberType NoteProperty -Name BusinessUnit -Value $businessUnitName Add-Member -InputObject $object -MemberType NoteProperty -Name Name -Value $vm.Name Add-Member -InputObject $object -MemberType NoteProperty -Name Domain -Value (($vm.Guest.Hostname.Split('.') | Select-Object -Skip 1) -join '.') Add-Member -InputObject $object -MemberType NoteProperty -Name Location -Value " " Add-Member -InputObject $object -MemberType NoteProperty -Name IPAddress -Value ($vm.Guest.IPAddress -join ", ") Add-Member -InputObject $object -MemberType NoteProperty -Name Function -Value " " Add-Member -InputObject $object -MemberType NoteProperty -Name PorV -Value "Virtual" Add-Member -InputObject $object -MemberType NoteProperty -Name vCluster -Value ($vm | Get-Cluster).Name Add-Member -InputObject $object -MemberType NoteProperty -Name vHost -Value $vm.VMHost Add-Member -InputObject $object -MemberType NoteProperty -Name Make -Value "N/A" Add-Member -InputObject $object -MemberType NoteProperty -Name Model -Value "N/A" Add-Member -InputObject $object -MemberType NoteProperty -Name SerialNumber -Value "N/A" Add-Member -InputObject $object -MemberType NoteProperty -Name CPU -Value $vm.NumCpu Add-Member -InputObject $object -MemberType NoteProperty -Name vSocket -Value ($vm.NumCpu / $vm.CoresPerSocket) Add-Member -InputObject $object -MemberType NoteProperty -Name CoreCount -Value $vm.CoresPerSocket Add-Member -InputObject $object -MemberType NoteProperty -Name MemoryGB -Value $vm.MemoryGB Add-Member -InputObject $object -MemberType NoteProperty -Name OperatingSystem -Value $vm.Guest.OSFullName Add-Member -InputObject $object -MemberType NoteProperty -Name UsedSpaceGB -Value ([math]::Round($vm.UsedSpaceGB, 0)) Add-Member -InputObject $object -MemberType NoteProperty -Name ProvisionedSpaceGB -Value ([math]::Round($vm.ProvisionedSpaceGB,0)) Add-Member -InputObject $object -MemberType NoteProperty -Name Environment -Value " " # Stores the PSObject containing VM stats successful to an PSObject array $results += $object } # The Sort-Object is specifically acceptable up truthful that the Export-Csv and Out-GridView bash not truncate the properties of the idiosyncratic PSObjects in # the array. $results | Out-GridView $results | Export-Csv -Path $logFile -NoTypeInformation Write-Host "Output saved to $logFile" Disconnect-VIServer -Server * -Confirm:$false -Force | Out-Null

Microsoft Weekly Newsletter

Be your company's Microsoft insider by speechmaking these Windows and Office tips, tricks, and cheat sheets. Delivered Mondays and Wednesdays

Sign up today

Also see

  • Windows 11 cheat sheet: Everything you request to cognize (free PDF) (TechRepublic)
  • More must-read Microsoft coverage (TechRepublic connected Flipboard)
  • Manage Active Directory with these 11 PowerShell scripts (TechRepublic Premium)
  • Top 5 programming languages for systems admins to larn (free PDF) (TechRepublic)
  • Read Entire Article