181 lines
11 KiB
PowerShell
181 lines
11 KiB
PowerShell
#region Get-RackpadInventory
|
|
Function Get-RackpadInventory
|
|
{
|
|
<#
|
|
.SYNOPSIS
|
|
Pre-loads every Rackpad collection required by the population process into memory.
|
|
|
|
.DESCRIPTION
|
|
Reconciliation requires the current state of every managed collection. Retrieving each collection once and performing all
|
|
subsequent lookups in memory reduces the number of API calls from thousands to roughly twenty per run.
|
|
|
|
The returned object exposes one generic list per collection. Lookups are performed by the caller using Where-Object with
|
|
case insensitive comparison operators.
|
|
|
|
.PARAMETER Session
|
|
The session object produced by Connect-RackpadInstance.
|
|
|
|
.PARAMETER LabID
|
|
Optionally scopes the lab aware collections to a single lab.
|
|
|
|
.PARAMETER IncludeDHCPScopes
|
|
Retrieves DHCP scopes. Omitted by default because DHCP is intentionally left as a manual concern within Rackpad.
|
|
|
|
.EXAMPLE
|
|
$RackpadInventory = Get-RackpadInventory -Session $RackpadSession -LabID 'lab_home'
|
|
|
|
.NOTES
|
|
Author: Grace Solutions
|
|
Version: 2026.08.02.0000
|
|
#>
|
|
|
|
[CmdletBinding()]
|
|
Param
|
|
(
|
|
[Parameter(Mandatory=$True)]
|
|
[ValidateNotNullOrEmpty()]
|
|
[PSObject]$Session,
|
|
|
|
[Parameter(Mandatory=$False)]
|
|
[AllowEmptyString()]
|
|
[String]$LabID,
|
|
|
|
[Parameter(Mandatory=$False)]
|
|
[Switch]$IncludeDHCPScopes
|
|
)
|
|
|
|
Try
|
|
{
|
|
#region Define the collections that will be retrieved
|
|
$CollectionDefinitionList = New-Object -TypeName 'System.Collections.Generic.List[PSObject]'
|
|
|
|
[ScriptBlock]$AddCollectionDefinition = {
|
|
Param
|
|
(
|
|
[String]$PropertyName,
|
|
[String]$Route,
|
|
[Boolean]$SupportsLabScope
|
|
)
|
|
|
|
$CollectionDefinitionProperties = New-Object -TypeName 'System.Collections.Specialized.OrderedDictionary'
|
|
$CollectionDefinitionProperties.PropertyName = $PropertyName
|
|
$CollectionDefinitionProperties.Route = $Route
|
|
$CollectionDefinitionProperties.SupportsLabScope = $SupportsLabScope
|
|
|
|
$CollectionDefinitionList.Add((New-Object -TypeName 'System.Management.Automation.PSObject' -Property ($CollectionDefinitionProperties)))
|
|
}
|
|
|
|
$Null = $AddCollectionDefinition.InvokeReturnAsIs('LabList', 'labs', $False)
|
|
$Null = $AddCollectionDefinition.InvokeReturnAsIs('DeviceTypeList', 'device-types', $False)
|
|
$Null = $AddCollectionDefinition.InvokeReturnAsIs('PortTemplateList', 'ports/templates', $False)
|
|
$Null = $AddCollectionDefinition.InvokeReturnAsIs('RoomList', 'rooms', $True)
|
|
$Null = $AddCollectionDefinition.InvokeReturnAsIs('RackList', 'racks', $True)
|
|
$Null = $AddCollectionDefinition.InvokeReturnAsIs('DeviceList', 'devices', $True)
|
|
$Null = $AddCollectionDefinition.InvokeReturnAsIs('VirtualSwitchList', 'virtual-switches', $True)
|
|
$Null = $AddCollectionDefinition.InvokeReturnAsIs('PortList', 'ports', $True)
|
|
$Null = $AddCollectionDefinition.InvokeReturnAsIs('PortLinkList', 'port-links', $False)
|
|
$Null = $AddCollectionDefinition.InvokeReturnAsIs('VLANList', 'vlans', $True)
|
|
$Null = $AddCollectionDefinition.InvokeReturnAsIs('VLANRangeList', 'vlans/ranges', $True)
|
|
$Null = $AddCollectionDefinition.InvokeReturnAsIs('SubnetList', 'subnets', $True)
|
|
$Null = $AddCollectionDefinition.InvokeReturnAsIs('IPZoneList', 'ip-zones', $True)
|
|
$Null = $AddCollectionDefinition.InvokeReturnAsIs('IPAssignmentList', 'ip-assignments', $True)
|
|
$Null = $AddCollectionDefinition.InvokeReturnAsIs('DeviceServiceList', 'device-services', $True)
|
|
$Null = $AddCollectionDefinition.InvokeReturnAsIs('DeviceMonitorList', 'device-monitors', $True)
|
|
$Null = $AddCollectionDefinition.InvokeReturnAsIs('WirelessControllerList', 'wifi/controllers', $True)
|
|
$Null = $AddCollectionDefinition.InvokeReturnAsIs('WirelessSSIDList', 'wifi/ssids', $True)
|
|
$Null = $AddCollectionDefinition.InvokeReturnAsIs('WirelessAccessPointList', 'wifi/access-points', $True)
|
|
$Null = $AddCollectionDefinition.InvokeReturnAsIs('WirelessRadioList', 'wifi/radios', $True)
|
|
$Null = $AddCollectionDefinition.InvokeReturnAsIs('WirelessAssociationList', 'wifi/associations', $True)
|
|
|
|
Switch ($IncludeDHCPScopes.IsPresent)
|
|
{
|
|
{($_ -eq $True)}
|
|
{
|
|
$Null = $AddCollectionDefinition.InvokeReturnAsIs('DHCPScopeList', 'dhcp-scopes', $True)
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region Retrieve every collection
|
|
$InventoryProperties = New-Object -TypeName 'System.Collections.Specialized.OrderedDictionary'
|
|
$InventoryProperties.RetrievedAt = [System.DateTime]::UtcNow
|
|
$InventoryProperties.LabID = "$($LabID)"
|
|
|
|
$LoadStopwatch = New-Object -TypeName 'System.Diagnostics.Stopwatch'
|
|
$Null = $LoadStopwatch.Start()
|
|
|
|
$CollectionDefinitionListCount = $CollectionDefinitionList.Count
|
|
|
|
For ($CollectionDefinitionIndex = 0; $CollectionDefinitionIndex -lt $CollectionDefinitionListCount; $CollectionDefinitionIndex++)
|
|
{
|
|
$CollectionDefinition = $CollectionDefinitionList[$CollectionDefinitionIndex]
|
|
|
|
$WriteProgressParameters = New-Object -TypeName 'System.Collections.Specialized.OrderedDictionary'
|
|
$WriteProgressParameters.Activity = 'Loading the Rackpad inventory'
|
|
$WriteProgressParameters.Status = "Retrieving `"$($CollectionDefinition.Route)`" ($($CollectionDefinitionIndex + 1) of $($CollectionDefinitionListCount))"
|
|
$WriteProgressParameters.PercentComplete = [System.Math]::Round((($CollectionDefinitionIndex + 1) / $CollectionDefinitionListCount) * 100, 2)
|
|
$WriteProgressParameters.Id = 10
|
|
|
|
$Null = Write-Progress @WriteProgressParameters
|
|
|
|
$QueryParameter = New-Object -TypeName 'System.Collections.Specialized.OrderedDictionary'
|
|
|
|
Switch (($CollectionDefinition.SupportsLabScope -eq $True) -and ([String]::IsNullOrWhiteSpace($LabID) -eq $False))
|
|
{
|
|
{($_ -eq $True)}
|
|
{
|
|
$QueryParameter.labId = "$($LabID)"
|
|
}
|
|
}
|
|
|
|
$Response = Invoke-RackpadRequest -Session ($Session) -Route ($CollectionDefinition.Route) -Method 'GET' -QueryParameter ($QueryParameter)
|
|
|
|
$CollectionList = New-Object -TypeName 'System.Collections.Generic.List[PSObject]'
|
|
|
|
Switch ($Response.IsSuccess)
|
|
{
|
|
{($_ -eq $True)}
|
|
{
|
|
ForEach ($CollectionItem In @($Response.Content))
|
|
{
|
|
Switch ($Null -ine $CollectionItem)
|
|
{
|
|
{($_ -eq $True)}
|
|
{
|
|
$CollectionList.Add($CollectionItem)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
{($_ -eq $False)}
|
|
{
|
|
Write-Warning -Message "[$([System.DateTime]::UtcNow.ToString('yyyy/MM/dd HH:mm:ss.FFF'))] - [WARNING] - The `"$($CollectionDefinition.Route)`" collection could not be retrieved. [Status Code: $($Response.StatusCode)] [$($Response.ErrorMessage)]"
|
|
}
|
|
}
|
|
|
|
$InventoryProperties.$($CollectionDefinition.PropertyName) = $CollectionList
|
|
|
|
Write-Verbose -Message "[$([System.DateTime]::UtcNow.ToString('yyyy/MM/dd HH:mm:ss.FFF'))] - [INFO] - Retrieved $($CollectionList.Count) object(s) from the `"$($CollectionDefinition.Route)`" collection."
|
|
}
|
|
|
|
$Null = $LoadStopwatch.Stop()
|
|
|
|
$Null = Write-Progress -Activity 'Loading the Rackpad inventory' -Id 10 -Completed
|
|
#endregion
|
|
|
|
$InventoryProperties.LoadDuration = $LoadStopwatch.Elapsed
|
|
|
|
Write-Verbose -Message "[$([System.DateTime]::UtcNow.ToString('yyyy/MM/dd HH:mm:ss.FFF'))] - [INFO] - The Rackpad inventory was loaded in $([System.Math]::Round($LoadStopwatch.Elapsed.TotalSeconds, 2)) second(s)."
|
|
|
|
$InventoryObject = New-Object -TypeName 'System.Management.Automation.PSObject' -Property ($InventoryProperties)
|
|
|
|
Write-Output -InputObject ($InventoryObject)
|
|
}
|
|
Catch
|
|
{
|
|
Throw
|
|
}
|
|
}
|
|
#endregion
|