Tue. Oct 26th, 2021

Stock studies are a standard request when administering a VMware vCenter atmosphere. Learn the way this VMware PowerShell script could make such requests fast and simple.


Picture: Shutterstock/SidorArt

As somebody who administers a VMware digital atmosphere and has been by way of a number of firm mergers, working stock audits is second nature to me. It is at all times a good suggestion to keep up a present stock of your digital machines with the intention to greatest handle the atmosphere, however as a result of ever-changing nature of know-how, the place techniques are constructed or decommissioned at an everyday tempo, it may be like attempting to hit a transferring goal.

As common, scripting is the important thing right here. This VMware-based PowerShell script will help make amassing your stock information fast and simple. My colleague Brad Doran, senior technical account supervisor at Amazon Net Providers, offered me this script and I’m utilizing it right here together with his permission. You possibly can copy and paste it to an area file referred to as VMware_Inventory.ps1, for example.


Use Home windows 10 (it is theoretically attainable to run this in Home windows 7, however it’s possible you’ll expertise complications with outdated PowerShell variations since this script requires PowerShell 4.0 at minimal).

Run Home windows PowerShell ISE as Administrator (you can too use common PowerShell, however I discover ISE greatest for optimum outcomes in addition to error analysis).

Set up the VMware PowerCLI module utilizing this remark inside the PowerShell ISE Window:

Set up-Module -Identify VMware.PowerCLI

In case you get an error that you simply want NuGet supplier model 2.8.201 simply click on Sure and the method will obtain and set up it for you. It’ll take just a few moments and you may then have to click on Sure to proceed, and in just a few extra moments you will notice this system putting in the associated VMWare.PowerCLI packages.

SEE: Microsoft PowerShell: Discover ways to automate your workday (TechRepublic Academy)

Because the script requires the VMware.VimAutomation.Core module additionally make sure that it is the most recent model (as of October 2021) put in through this command:

Set up-Module -Identify VMware.VimAutomation.Core -RequiredVersion

Click on Sure to proceed and in just a few extra moments you will notice this system putting in the associated VMWare package deal.

In case you obtain an error stating: “PackageManagementInstall-Bundle : Authenticode issuer ‘CN=VeriSign Class 3 Public Main Certification Authority – G5, OU=”(c) 2006 VeriSign, Inc. – For approved use solely”, OU=VeriSign Belief Community, O=”VeriSign, Inc.”, C=US’ of the brand new module ‘VMware.VimAutomation.Core’ with model ‘’ is just 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 model ‘’. In case you nonetheless wish to set up or replace, use-SkipPublisherCheck parameter”

run this command:

Set up-Module -Identify VMware.VimAutomation.Core -RequiredVersion -SkipPublisherCheck

You can even go right here and obtain the most recent model to (“C:Program Recordsdata (x86)VMWareInfrastructurePowerCLIModules”):


Customizing the script

The script depends on these variables (Determine A):

$vcs = @(“vCenterFQDN”) # FQDN of your vCenter server.
$logFile = “C:TempVMInventory.csv” # The place you wish to save the CSV file
$vcsCluster = “*” # Filters which cluster you wish to pull VM stats from
$businessUnitName = “Factor” # Identify of enterprise unit that the script is gathering stats for


Determine A

For $vcs, you’ll be able to change @(“vCenterFQDN”) with the identify of your vCenter server in quotes; in any other case the script will immediate you to enter the identify.

For $logFile, the logfile path and identify may be modified accordingly.

For $vcsCluster you’ll be able to specify the cluster to collect information from, or depart this line as is to drag data from all clusters.

For $businessUnitName, change the instance “Factor” entry with your small business unit, division or group.

SEE: Guidelines: Server stock (TechRepublic Premium)

Working the script

Click on File then Open and browse to the place you saved the script file to pick it then load it into PowerShell ISE. Hit the inexperienced button to execute it. 

In case you get an error associated to safety insurance policies or the script being unsigned, run this command to permit native scripts and distant signed scripts:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned 

In case you left the $vcs variable as is you can be prompted to enter the vCenter server identify (Determine B).


Determine B

When you enter the identify you may be prompted to authenticate to vCenterj (Determine C). Be sure to make use of an account with administrator-level credentials.


Determine C

You’ll then see the script working and actively gathering information (Determine D).


Determine D

The script will show the outcomes, in addition to save the main points to “C:TempVMInventory.csv” in the event you left the associated variable as is (Determine E).


Determine E

(Observe I’ve eliminated the FQDN entries that had been listed since this was run in opposition to an energetic enterprise atmosphere.)  

Thanks, Brad, for a wonderful software each VMware administrator ought to have of their toolkit!

Please contact me through the e-mail hyperlink under (to the left of “Full Bio | See all of Scott’s content material”) if in case you have any questions or points working this script.

#Requires -Model 4
#Requires -Modules VMware.VimAutomation.Core
    Create a listing in CSV format of digital machines in vCenter.
    This script is supposed to carry out a listing of Digital Machines. It could actually connect with a number of vCenters to drag
    statistics from and it might pull statistics from a number of Host Clusters. This script performs read-only operations.
    Output is formatted as CSV utilizing an ordinary format.
    Variable Particulars
    $vcs - An array containing the checklist of vCenter servers to hook up with.
    $logFile - The placement of the place to save lots of the output in CSV format.
    $vcsCluster - The identify of the cluster. It accepts wildcard characters, nevertheless it can't be empty.
    $businessUnitName - the busines unit, group or division which owns/helps this atmosphere
    Credential Necessities
    $vcsCreds - A person credential that has entry to login to vCenter with Learn-Solely rights at a minimal.
    $wmiCreds - A person credential that has entry to carry out WMI queries regionally on the Home windows Digital Machines
# Edit these variables on your particular atmosphere
$vcs = @("vCenterFQDN")                 # FQDN of your vCenter server.
$logFile = "C:TempVMInventory.csv"    # The place you wish to save the CSV file
$vcsCluster = "*"                       # Filters which cluster you wish to pull VM stats from.
$businessUnitName = "Factor"     # Identify of Enterprise Unit that the script is gathering stats for
if($vcs -contains "vCenterFQDN"){
    $vcs = Learn-Host -Immediate "FQDN of vCenter Server"
$vcsCreds = Get-Credential -Message "vCenter Credentials"
#$wmiCreds = Get-Credential -Message "WMI Credentials"
Import-Module VMware.VimAutomation.Core
Join-VIServer $vcs -Credential $vcsCreds | Out-Null
$vms = Get-Cluster -Identify $vcsCluster | Get-VM
$rely = 0
$outcomes = @()
$Script:ProgressPreferenceOriginal = $ProgressPreference
foreach($vm in $vms) Get-Cluster).Identify
    Add-Member -InputObject $object -MemberType NoteProperty -Identify vHost -Worth $vm.VMHost
    Add-Member -InputObject $object -MemberType NoteProperty -Identify Make -Worth "N/A"
    Add-Member -InputObject $object -MemberType NoteProperty -Identify Mannequin -Worth "N/A"
    Add-Member -InputObject $object -MemberType NoteProperty -Identify SerialNumber -Worth "N/A"
    Add-Member -InputObject $object -MemberType NoteProperty -Identify CPU -Worth $vm.NumCpu
    Add-Member -InputObject $object -MemberType NoteProperty -Identify vSocket -Worth ($vm.NumCpu / $vm.CoresPerSocket)
    Add-Member -InputObject $object -MemberType NoteProperty -Identify CoreCount -Worth $vm.CoresPerSocket
    Add-Member -InputObject $object -MemberType NoteProperty -Identify MemoryGB -Worth $vm.MemoryGB
    Add-Member -InputObject $object -MemberType NoteProperty -Identify OperatingSystem -Worth $vm.Visitor.OSFullName
    Add-Member -InputObject $object -MemberType NoteProperty -Identify UsedSpaceGB -Worth ([math]::Spherical($vm.UsedSpaceGB, 0))
    Add-Member -InputObject $object -MemberType NoteProperty -Identify ProvisionedSpaceGB -Worth ([math]::Spherical($vm.ProvisionedSpaceGB,0))
    Add-Member -InputObject $object -MemberType NoteProperty -Identify Atmosphere -Worth " "
    # Shops the PSObject containing VM stats in to an PSObject array
    $outcomes += $object

# The Kind-Object is particularly arrange in order that the Export-Csv and Out-GridView don't truncate the properties of the person PSObjects in
# the array.
$outcomes | Out-GridView
$outcomes | Export-Csv -Path $logFile -NoTypeInformation
Write-Host "Output saved to $logFile"
Disconnect-VIServer -Server * -Affirm:$false -Power | Out-Null

Additionally see

  • Home windows 11 cheat sheet: All the things it’s essential to know (free PDF) (TechRepublic)
  • Extra must-read Microsoft protection (TechRepublic on Flipboard)
  • Handle Energetic Listing with these 11 PowerShell scripts (TechRepublic Premium)
  • Prime 5 programming languages for techniques admins to study (free PDF) (TechRepublic)

  • Source link

    By admin

    Leave a Reply

    Your email address will not be published. Required fields are marked *