Virtual Instance Discovery and Analysis

As an Infrastructure Engineer or Architect you need to have a good grasp on what systems comprise your environment. In the past this was somewhat straight forward. You kept a configuration item database in your CMDB and teams had their workbooks and playbooks. However in this new world of DevOps and CI-CD and their automation tool sets such as Terraform, Chef, Ansible, Bladelogic, and many others; developers can stand up their own virtual instances and tare them down. This can make it hard to have a complete picture of your environment. This can be especially difficult for storage infrastructure since many virtual instances can be deployed on large data stores and when there is an IO performance problem tracking down the related hardware can be like following the rabbit to wonderland.

I ran into this while leading several projects for storage infrastructure servicing an ESX environment and developed a powercli script to pull the necessary virtual instance and data-store data from the VSphere systems. First you will need to install powercli for VMware which can be found here:
https://developer.vmware.com/web/tool/12.4/vmware-powercli

Next is the powercli script. There are many examples of powercli scripts which can be found with a quick search on the internet. To use power-shell with VMWare’s powercli the first step is to set the powercli configuration. If you aren’t using signed certificates then you will want to ignore invalid certificates and you will want to step through each vsi server so you don’t get interleaved output. Then you will need to store credentials to use for each login using the Get-Credential function which may be stored as xml to a file for subsequent use.

Now comes our code which will include a list of VSI servers (VCenter) to be queried. Loop through these and use the Get-VM, Get-HardDisk, Get-Datastore and Get-Datacenter to pull the data from each VSI VCenter server. Output a line for each virtual machine instance in a comma delimited format. This can then be imported into excel and cross referenced with CMDB data and storage reports. Using Excel for Infrastructure Analysis is another topic for another day.

#################################################################################
# Get List of VMs, HDD Size, Datastores Size, Folder name, Operating System
#
# Version: 2.0  Author: Hicham Hassan IBM  - Feb 2021
# Version: 3.0  Author: Devin Adint IBM - Feb 2021 - added credentials, vcenter
#               server list and loop, and requires powercli module 
#               added to fields device-diskname, IP, DataStoreGB, etc.
#################################################################################

#Check that Modules are available and loaded - exit if not found
#requires -module VMware.VimAutomation.Core
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -DefaultVIServerMode single -Confirm:$false


# Load my credentials from file or if not exist request credentials and save to file
if (-not (Test-Path ".\userid.cred" )) {
$MyCredentials = Get-Credential
$MyCredentials | Export-CliXml -Path ".\userid.cred"
} else {
$MyCredentials = IMPORT-CLIXML ".\userid.cred"
}

# Load list of vCenter Servers from File
if (Test-Path ".\viservers.csv" ) {
$VMSearch = Import-Csv ".\viservers.csv" -Header ("VIServer","DataCenter")
$VIServer  = $VMSearch | % { $_.VIServer }
$DataCenter = $VMSearch | % { $_.DataCenter }
} else {
$VIServer=@( "esx001.domain.com", 
"esx002.domain.com", 
"esx003.domain.com", 
"esx004.domain.com");
}

# 
Foreach ($vCenter in $VIServer){

    #Connects to the vCenter
    connect-viserver -Server $vCenter -Credential $MyCredentials
	
Get-VM -PipelineVariable vm |
ForEach-Object -Process {
    Get-HardDisk -VM $vm |
    Select @{N='VCName';E={$vCenter}},
		@{N="DCname";E={(Get-Datacenter -Cluster $vm.VMhost.Parent).Name}},
		@{N='Cluster';E={$vm.VMhost.Parent}},
		@{N='VMHost';E={$vm.VMhost.Name}},
        @{N='VMName';E={$vm.Name}},
        @{N='PowerState';E={$vm.PowerState}},
        @{N='OS';E={$vm.Guest.OSFullName}},
        @{N='OSConfigured';E={$vm.ExtensionData.Config.GuestFullName}},
		@{N="IP";E={$vm.Guest.IPaddress}},
        @{N='HDDName';E={$_.Name}},
        @{N='Datastore';E={
            $script:ds = Get-Datastore -RelatedObject $vm
            $script:ds.Name}},
		 @{N='DataStoreGB';E={[math]::Round($script:ds.CapacityGB)}},
		 @{N='DataStoreFreeGB';E={[math]::Round($script:ds.FreeSpaceGB)}},

        @{N='HDDAllocatedGB';E={[math]::Round($_.CapacityGB)}},
		@{N='Device';E={$script:ds.Extensiondata.Info.Vmfs.Extent[0].DiskName}},
		@{N="Type";E={$script:ds.Extensiondata.Info.Vmfs.Type.ToLower() + $script:ds.Extensiondata.Info.Vmfs.MajorVersion}},
		@{N="Status";E={$script:ds.Extensiondata.OverallStatus}},
        StorageFormat

} | Export-Csv ".\${vCenter}_report.csv" -NoTypeInformation -UseCulture

Disconnect-VIServer -Confirm:$false

}	# foreach vCenter

#Export-Csv .\report.csv -NoTypeInformation -UseCulture
Export-Excel -Path C:\Temp\Report.xlsx -WorkSheetname Usage -TableName Usage
Advertisement

About Last Fiddle
I have always had many interests; technology, science, philosophy, theology, politics, history, etc... Currently, life for the past twelve years has placed me in the area of technology fulfilling roles in System Administration and Architecture. But I have always been involved in the local church and enjoy researching and discussing issues of theology, philosophy, history and politics...

Comments are closed.

%d bloggers like this: