Get-VM

The script below is a basic example to collect all available information from a VMware platform, The script will connect to a VMware vCenter and export the information to a “.CSV” file format replacing the delimiter with a “^” symbol

 

The first part of the script will Load the required VMware Powershell snap-in’s.

foreach ($snapin in $LoadedSnapins){
$snapin
if ($snapin.name -eq “VMware.VimAutomation.Core”)
{$moduleLoaded = “TRUE”}
}
if ($moduleLoaded -ne “TRUE”){Add-PSSnapin VMware.VimAutomation.Core}

Then the next line will set the powershell configuration, Ignoring any certificates and set the user scope.

Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Scope User -Confirm:$false

In the next part of the script we define the script Variables and set the Powershell commands into the variables, in the below we are extracting the information in a specific order, this is to enable an import into a MS SQL Database to help plan capacity.

$Servers = @(“Your vCenter FQDN or IP Address“) # accepts multiple inputs use a comma to separate the VMware vCenter Servers

$user = “Domain\ReadOnlyServiceAccount

 

$Pass = “!!!YourPassword!!!

$getvm = “Get-VM | select PowerState,Version,Guest,NumCpu,MemoryMB,MemoryGB,Host,HostId,VMHostId,VMHost,VApp,FolderId,Folder,ResourcePoolId,ResourcePool,PersistentId,UsedSpaceGB,ProvisionedSpaceGB,HARestartPriority,HAIsolationResponse,DrsAutomationLevel,VMSwapfilePolicy,VMResourceConfiguration,Name,Id,Uid”

#Define the Output Directory
$dirRoot = “Somewhere local to your laptop, server\”

We now move on to connecting to the VMware vCenter Server and executing the powershell script.

foreach ($Server in $Servers){
$VIConnection = Connect-VIServer -Server $Server -user $user -Password $Pass
$getVMGuestFilePath = $dirRoot + “get-vmguest_” + $VIConnection.name + “.csv”
# Invoke Script
invoke-expression $getvm | export-csv $getVMGuestFilePath -delimiter ^ -notype
# Take out the quotation marks in the CSV file.
(get-content $getVMGuestFilePath) | % {$_ -replace ‘”‘, “”} | out-file $getVMGuestFilePath -Fo -En ascii#Disconnect from the VCenter
Disconnect-VIServer * -Confirm:$False
}

And that’s it you will now have a CSV file with the required VM Guest information, with a delimiter “^”, this file can then be used to populate a Database to track capacity.

 

 

#Load Powershell snapins
foreach ($snapin in $LoadedSnapins){
$snapin
if ($snapin.name -eq “VMware.VimAutomation.Core”)
{$moduleLoaded = “TRUE”}
}
if ($moduleLoaded -ne “TRUE”){Add-PSSnapin VMware.VimAutomation.Core}

#Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Scope User -Confirm:$false

Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Scope User -Confirm:$false

#Create list of vCenter Servers  to connect to
$Servers = @(“vCenter FQDN or IP Address“) # accepts multiple inputs use a comma to separate the VMware vCenter Servers
$user = “Domain\ServiceAccount
$Pass = “!SecurePassword!

#Place CMDLET into a variable.
$getvm = “Get-VM | select PowerState,Version,Guest,NumCpu,MemoryMB,MemoryGB,Host,HostId,VMHostId,VMHost,VApp,FolderId,Folder,ResourcePoolId,ResourcePool,PersistentId,UsedSpaceGB,ProvisionedSpaceGB,HARestartPriority,HAIsolationResponse,DrsAutomationLevel,VMSwapfilePolicy,VMResourceConfiguration,Name,Id,Uid”

#Define the Output Directory
$dirRoot = “Share:\SomeLocation\

#Connect to vCenter servers in a loop and collect information from CMDLET
foreach ($Server in $Servers){
$VIConnection = Connect-VIServer -Server $Server -user $user -Password $Pass
$getVMGuestFilePath = $dirRoot + “get-vmguest_” + $VIConnection.name + “.csv”
# Invoke Script
invoke-expression $getvm | export-csv $getVMGuestFilePath -delimiter ^ -notype
# Take out the quotation marks in the CSV file.
(get-content $getVMGuestFilePath) | % {$_ -replace ‘”‘, “”} | out-file $getVMGuestFilePath -Fo -En ascii#Disconnect from the VCenter
# Disconnect vCenter Session
Disconnect-VIServer * -Confirm:$False
}