The next step for performing a true-up of the ServiceNow server CMDB is to identify a source of truth for deployed servers. This could be VMware or some other virtualization platform, or maybe physical servers. In my environment I’m dealing with multiple Nutanix clusters, some foundation as Nutanix AHV and some with VMware installed.
Nutanix Prism Central:
Connecting to Prism Central provides access to VM data on all deployed clusters, AHV or VMWare. No need to separately connect to each cluster as long as the cluster is configured in Prism Central.
Required PowerShell Modules:
- NutanixCmdlets: The Nutanix commandlets are available from your Prism web console. Click the user icon in the upper-right corner and select Download Cmdlets Installer. As of the time I write this the commandlets are not installed as a typical PowerShell module. The installer places desktop shortcut Double-clicking the shortcut opens a PowerShell session that has the Nutanix cmdlets loaded. This is not great for automation scripting.
I examined the short-cut and after some trial and error I found that this worked for importing the cmdlets into the automation script by “dot” sourcing the Nutanix importModules.ps1 file into the automation script scope.
# Adding Nutanix PS cmdlets
. 'C:\Program Files (x86)\Nutanix Inc\NutanixCmdlets\powershell\import_modules\ImportModules.PS1'
# Nutanix batteries available!
The next code snippet assumes Event logging as described here.
# Start connect to Nutanix Prism Central
# Import a saved Nutanix credential that has privilege's in Nutanix Prism
# Credential must have been created using the same identity that
# the script will run under
$ntnxCred = Import-CliXml -Path "${env:\userprofile}\Nutanix.Cred"
# The Connect-NTNXCluster cmd requires the user name
$username = $ntnxCred.username
$ Convert the password to a secure-string for use by Connect-NTNXCluster
$password = ConvertTo-SecureString $($ntnxCred.GetNetworkCredential().password) -AsPlainText -Force
# List of Nutanix cluster IP's. Takes the form of 'IP1','IP2',...
# In my case I'm connecting to one PrismCentral instance
$nxIPs = '192.168.240.100'
$anyConnections = $false
foreach ($nxIP in $nxIPs)
{
$ntnxConn = Connect-NTNXCluster -Server $nxIP -UserName $UserName -Password $password -AcceptInvalidSSLCerts -ForcedConnection
If($($ntnxConn.isConnected) -eq 'True') {
$anyConnections = $true
$ntnxm = "Connection Estabished for " + [String]$($ntnxconn.CommandRuntime) +" at " + [String]$($ntnxconn.lastAccessTimestamp)
}else {$ntnxm = "Could not connect to $nxIP"}
Write-EventLog -LogName Application -Source "CustomScripts" -EventId 1000 -EntryType Information $ntnxm
}
# End Establishing connections to one or more Nutanix clusters
# If $anyConnections is True then at least one session
# is established
Much of the next snippet comes from a Blog post by Kees Baggerman and can be found here.
# Start Fetching VM data from each cluster
if($anyConnection) -eq 'True'){
Write-EventLog -LogName Application -Source "CustomScripts" -EventId 1000 -EntryType Information "Getting VM Data From Each Cluster"
$vms = @(get-ntnxvm)
$vmCount = $vms.Count
$outString = "Have gotten $vmCount VM's from Clusters"
Write-EventLog -LogName Application -Source "CustomScripts" -EventId 1000 -EntryType Information $outString
$ntnxReport = @()
foreach ($vm in $vms) {
$usedspace = 0
if (!($vm.nutanixvirtualdiskuuids.count -le 0)) {
foreach ($UUID in $VM.nutanixVirtualDiskUuids) {
$usedspace += (Get-NTNXVirtualDiskStat -Id $UUID -Metrics controller_user_bytes).values[0]
}
}
if ($usedspace -gt 0) {
$usedspace = [math]::round($usedspace /1gb, 0)
}
$container = "NA"
if (!($vm.vdiskFilePaths.count -le 0)) {
$container = $vm.vdiskFilePaths[0].split('/')[1]
}
$props = [ordered]@{
"VM Name" = $vm.vmName
"VM Description" = $vm.description
"Container" = $container
"Protection Domain" = $vm.protectionDomainName
"Host Placement" = $vm.hostName
"Power State" = $vm.powerstate
"Network adapters" = $vm.numNetworkAdapters
"IP Address(es)" = $vm.ipAddresses -join ","
"vCPUs" = $vm.numVCpus
"vRAM (GB)" = [math]::round($vm.memoryCapacityInBytes / 1GB, 0)
"Disk Count" = $vm.nutanixVirtualDiskUuids.count
"Provisioned Space (GB)" = [math]::round($vm.diskCapacityInBytes / 1GB, 0)
"Used Space (GB)" = $usedspace
} #End properties
$Reportobject = New-Object PSObject -Property $props
$ntnxreport += $Reportobject
}
Write-EventLog -LogName Application -Source "CustomScripts" -EventId 1000 -EntryType Information -Message "Server data from NTNX collected"
Write-EventLog -LogName Application -Source "CustomScripts" -EventId 1000 -EntryType Information "Disconnecting from the Nutanix Cluster"
Disconnect-NTNXCluster -Servers *
}
# End fetching Nutanix VM Data
The $ntnxReport array now has an object for each VM found and is ready to be combined with the CMDB data.