It’s been a busy few weeks. Between work and preparing the house for an imminent new baby, there hasn’t been much time for blogging. But that doesn’t mean I haven’t been scripting, I just haven’t written about them!
I was recently working on a script for a customer to automatically configure backup policies based on vSphere Resource Pools. This isn’t native to Rubrik, but I didn’t think it would take much to get it going with PowerCLI and the Rubrik PowerShell module. However, the PowerCLI command Get-ResourcePool doesn’t return any information as to which Datacenter it is in! See:
Not a problem in a single DC vCenter, but the customer I was working with had multiple DCs, clusters, a standard Resource Pool naming convention (I.E prod, devtest, etc) and wanted unique backup policies for each of them. My solution was to get a list of DCs, then iterate through each one with “Get-ResourcePool -Location DCName”, with the results added to a table array:
$vCenterDCs = Get-Datacenter # Building array $vCenterResourcePools = @() # For Each DC ForEach ($vCenterDC in $vCenterDCs) { # Getting DC variables $DatacenterName = $vCenterDC.name $DatacenterID = $vCenterDC.id # Getting Resource Pools for the DC $DCResourcePools = Get-ResourcePool -Location $vCenterDCName | Select * ForEach ($DCResourcePool in $DCResourcePools) { # Setting variables $ResourcePoolName = $DCResourcePool.Name $ResourcePoolID = $DCResourcePool.Id $ResourcePoolCluster = $DCResourcePool.Parent.Name $ResourcePoolClusterID = $DCResourcePool.Parent.Id # Adding each resource pool to the array $vCenterResourcePool = New-Object PSObject $vCenterResourcePool | Add-Member -MemberType NoteProperty -Name "DC" -Value "$DatacenterName" $vCenterResourcePool | Add-Member -MemberType NoteProperty -Name "DCID" -Value "$DatacenterID" $vCenterResourcePool | Add-Member -MemberType NoteProperty -Name "Cluster" -Value "$ResourcePoolCluster" $vCenterResourcePool | Add-Member -MemberType NoteProperty -Name "ClusterID" -Value "$ResourcePoolClusterID" $vCenterResourcePool | Add-Member -MemberType NoteProperty -Name "ResourcePool" -Value "$ResourcePoolName" $vCenterResourcePool | Add-Member -MemberType NoteProperty -Name "ResourcePoolID" -Value "$ResourcePoolID" $vCenterResourcePools += $vCenterResourcePool } # End of for each DC below } # End of for each DC below
Here’s the result:
Simple! The script is tested in PowerShell 5.1, includes saving vSphere credentials securely, and you can download it from here:
vSphereResourcePoolsByDCv1.zip
Or you can copy and paste it from the below:
################################################ # Configure the variables below for the vCenter ################################################ $vCenter = "192.168.1.10" $ScriptDirectory = "C:\vSphereResourcePoolsByDCv1" ################################################ # Running the script, nothing to change below ################################################ ####################### # Importing vCenter credentials ####################### # Setting credential file $vCenterCredentialsFile = $ScriptDirectory + "\vCenterCredentials.xml" # Testing if file exists $vCenterCredentialsFileTest = Test-Path $vCenterCredentialsFile # IF doesn't exist, prompting and saving credentials IF ($vCenterCredentialsFileTest -eq $False) { $vCenterCredentials = Get-Credential -Message "Enter vCenter login credentials" $vCenterCredentials | EXPORT-CLIXML $vCenterCredentialsFile -Force } ELSE { # Importing credentials $vCenterCredentials = IMPORT-CLIXML $vCenterCredentialsFile } ####################### # Installing then importing PowerCLI module ####################### $PowerCLIModuleCheck = Get-Module -ListAvailable VMware.PowerCLI IF ($PowerCLIModuleCheck -eq $null) { Install-Module -Name VMware.PowerCLI –Scope CurrentUser -Confirm:$false -AllowClobber } # Importing PowerCLI Import-Module VMware.PowerCLI ####################### # Connecting to vCenter ####################### Connect-VIServer -Server $vCenter -Credential $vCenterCredentials ####################### # Building a list of Resource Pools by Datacenter ####################### $vCenterDCs = Get-Datacenter # Building array $vCenterResourcePools = @() # For Each DC ForEach ($vCenterDC in $vCenterDCs) { # Getting DC variables $DatacenterName = $vCenterDC.name $DatacenterID = $vCenterDC.id # Getting Resource Pools for the DC $DCResourcePools = Get-ResourcePool -Location $vCenterDCName | Select * ForEach ($DCResourcePool in $DCResourcePools) { # Setting variables $ResourcePoolName = $DCResourcePool.Name $ResourcePoolID = $DCResourcePool.Id $ResourcePoolCluster = $DCResourcePool.Parent.Name $ResourcePoolClusterID = $DCResourcePool.Parent.Id # Adding each resource pool to the array $vCenterResourcePool = New-Object PSObject $vCenterResourcePool | Add-Member -MemberType NoteProperty -Name "DC" -Value "$DatacenterName" $vCenterResourcePool | Add-Member -MemberType NoteProperty -Name "DCID" -Value "$DatacenterID" $vCenterResourcePool | Add-Member -MemberType NoteProperty -Name "Cluster" -Value "$ResourcePoolCluster" $vCenterResourcePool | Add-Member -MemberType NoteProperty -Name "ClusterID" -Value "$ResourcePoolClusterID" $vCenterResourcePool | Add-Member -MemberType NoteProperty -Name "ResourcePool" -Value "$ResourcePoolName" $vCenterResourcePool | Add-Member -MemberType NoteProperty -Name "ResourcePoolID" -Value "$ResourcePoolID" $vCenterResourcePools += $vCenterResourcePool } # End of for each DC below } # End of for each DC below ####################### # Showing result in gridview ####################### $vCenterResourcePools | Out-gridview -Title "vCenter Resource Pools" ####################### # Disconnecting from vCenter, un-comment if needed ####################### # Disconnect-VIServer -Server $vCenter -Confirm:$false ####################### # End of script #######################
I hope you found this useful. Happy scripting,