VMware virtual machine inventory script

For reporting purposes, I maintain an Excel sheet containing information on my server base. I update this sheet on a weekly basis. Part of the information displayed in this sheet comes from a PowerShell script run against our vCenter servers. This post is not about the Excel sheet (I will share this with you in a later post), but about the PowerShell code.

A requirement for this script to work is the VMware PowerCLI PowerShell module, which can be installed with this PowerShell command:

1
 Install-Module -Name VMware.PowerCLI -Scope CurrentUser

SeeĀ https://blogs.vmware.com/PowerCLI/2018/02/powercli-10.html for more information about VMware PowerCLI.

In order for this script to work for your environment, make sure to adjust variableĀ $vCenterServers to match your vCenter server(s).
Also have a look at variable $VMList. The script below simply gets information about all your virtual machines. For my environment, I limit the number of queried virtual machines, by using a filter. I have left some examples as comments.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
@"
=========================================================================================================================
Title          : Report-Vmware-All-VMs-info v1.3.ps1
Description    : Exports VM Information for all Infra Windows VMs from vCenter into a .CSV file for importing into anything
Usage          : .\Report-Vmware-All-VMs-info v1.3.ps1
=========================================================================================================================
"
@

#. "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1"
If (-not (Get-Module -Name 'VMware.PowerCLI')) {
    Import-Module VMware.PowerCLI
}

# Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Scope Session # Needs to be set only once !!!
# Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Scope User # Needs to be set only once !!!

Set-PowerCLIConfiguration -InvalidCertificateAction ignore -confirm:$false

$DateTime = $((Get-Date).ToString('yyyy-MM-dd_hh-mm-ss'))
$OutputPath = "$env:USERPROFILE\Documents\Reports"

If( -Not (Test-Path -Path $OutputPath)) {
    New-Item -ItemType directory -Path $OutputPath
}

Set-Location $OutputPath
$Outputfile = "$DateTime-All-VMs-Output"

function Connect-vCenter {
    Param (

        [Parameter(Mandatory = $True)]
        $vCenterServer,
        [System.Management.Automation.PSCredential]$Credential
    )

    If ($Credential) {
        # Add-PSSnapin VMware.VimAutomation.Core
        Connect-VIServer $vCenterServer -Credential $Credential
    }
    Else {
        # Add-PSSnapin VMware.VimAutomation.Core
        Connect-VIServer $vCenterServer
    }
}

$username = $env:USERNAME
$password = Read-Host -Prompt "Please enter your password:" -AsSecureString
$credential = New-Object System.Management.Automation.PsCredential($username, $password)
$vCenterServers = @('vcenter01.yourdomain.local','vcenter02.yourdomain.local','vcenter03.yourdomain.local')

Connect-VCenter $vCenterServers $credential

$VMList = $null
$VMList = Get-VM | Where-Object {$_.Name -like "*text*"}
$VMList = $VMList + (Get-VM | Where-Object {$_.Name -like "*text*"})
$VMList = $VMList | Sort-Object -Property Name

$Counter = 1
$Report = @()

While ($Counter -lt $VMList.Count) {
    ForEach ($VM in $VMList) {
        Write-Progress -Activity "Retrieving data from servers" -Status "Processing..." -PercentComplete ( $Counter * 100 / $VMList.Count )
        Write-Host $Counter"/"($VMList).Count "[$VM]"

        $VMView = $VM | Get-View
        $NICs = (Get-NetworkAdapter -VM $VM)
        $NumNICs = $NICs.Count

        #Collect info
        $Cluster = $null
        $Folder = $null
        $NumberofNICs = $null

        $Cluster = (Get-Cluster -VM $VM).Name
        $Folder = (Get-VM $VM).Folder.Name
        $NumberofNICs = (Get-NetworkAdapter -VM $VM).Count

        $VMInfo = {} | Select Name,HardwareVersion,ToolsStatus,NumCPU,MemGB,Host,Cluster,Folder,PowerState,NumNICs,NIC0Type,NIC1Type,NIC2Type,NIC3Type

        $VMInfo.Name = $VM.Name
        $VMInfo.HardwareVersion = $VM.HardwareVersion
        $VMInfo.ToolsStatus = $VMView.Guest.ToolsStatus
        $VMInfo.NumCPU = $VM.NumCpu
        $VMInfo.MemGB = $VM.MemoryGB
        $VMInfo.Host = $VM.VMHost.Name
        $VMInfo.Cluster = $VM.VMHost.Parent.name
        $VMInfo.Folder = $VM.Folder.Name
        $VMInfo.PowerState = $VM.PowerState
        $VMInfo.NumNICs = $NICs.Count
        $VMInfo.NIC0Type = (Get-NetworkAdapter -VM $VM).Type[0]
        $VMInfo.NIC1Type = (Get-NetworkAdapter -VM $VM).Type[1]
        $VMInfo.NIC2Type = (Get-NetworkAdapter -VM $VM).Type[2]
        $VMInfo.NIC3Type = (Get-NetworkAdapter -VM $VM).Type[3]
               
        $Report += $VMInfo
        $Counter++
    }
}

$Report | Export-Csv "$Outputfile.csv" -NoTypeInformation -UseCulture

Disconnect-VIserver $vCenterServers -Confirm:$False

Leave a Reply

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