Get-CLUSTER

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.Get-CLUSTER

Get-CLUSTERGet-CLUSTERGet-CLUSTER

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!!!

$getCluster = “Get-Cluster | select vsanEnabled,vSanDiskClaimMode,HATotalSlots,HAUsedSlots,HAAVailableSlots,HASlotCPUMHz,HASlotMemoryMb,HASlotnumvcpus,parentid,parentfolder,HAenabled,HAAdmissionControlEnabled,HAFailoverLevel,HARestartPriority,HAIsolationResponse,VMSwapfilePolicy,DrsEnabled,DRSMode,DRSAutomationLevel,Name,ExtentionData,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
$getClusterFilePath = $dirRoot + “get-Cluster_” + $VIConnection.name + “.csv”
# Invoke Script
invoke-expression $getCluster | export-csv $getClusterFilePath -delimiter ^ -notype
# Take out the quotation marks in the CSV file.
(get-content $getClusterFilePath) | % {$_ -replace ‘”‘, “”} | out-file $getClusterFilePath-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.
$getVMHost = “Get-VMhost | select State,ConnectionState,PowerState,VMSwapfileDatastoreId,VMSwapfilePolicy,ParentId,IsStandalone,Manufacturer,Model,NumCpu,CpuTotalMhz,CpuUsageMhz,LicenseKey,MemoryTotalMB,MemoryTotalGB,MemoryUsageMB,MemoryUsageGB,ProcessorType,HyperthreadingActive,TimeZone,Version,Build,Parent,VMSwapfileDatastore,StorageInfo,NetworkInfo,DiagnosticPartition,FirewallDefaultPolicy,ApiVersion,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
$getClusterFilePath = $dirRoot + “get-Cluster_” + $VIConnection.name + “.csv”
# Invoke Script
invoke-expression $getCluster | export-csv $getClusterFilePath -delimiter ^ -notype
# Take out the quotation marks in the CSV file.
(get-content $getClusterFilePath) | % {$_ -replace ‘”‘, “”} | out-file $getClusterFilePath-Fo -En ascii
#Disconnect from the VCenter
Disconnect-VIServer * -Confirm:$False
}