Public/Get-InfraXA7Inventory.ps1

<#
.SYNOPSIS
Retrieves inventory data for Citrix infrastructure or XenApp 7 (XA7) servers.
 
.DESCRIPTION
The Get-InfraXA7Inventory function provides inventory information for either infrastructure servers in Active Directory or Citrix XenApp 7 servers in a Citrix site. Depending on the selected type, it can query either LDAP (Infra) to fetch physical/virtual servers or Citrix Broker Service (XA7) to fetch Citrix-managed machines.
 
.PARAMETER Type
Specifies the type of inventory to retrieve. Accepts either "Infra" (for Active Directory infrastructure servers) or "XA7" (for Citrix XenApp 7 servers).
 
.PARAMETER DN
Distinguished Name (DN) of the Active Directory OU or container to search for infrastructure servers. Required only if Type is "Infra".
 
.PARAMETER Name
Specifies a server name pattern to filter the search. Default is "*" (all servers).
 
.PARAMETER AdminAddress
Specifies the Citrix Delivery Controller to connect to when Type is "XA7". Not required for "Infra" type.
 
.PARAMETER InfraType
Indicates the classification of the infrastructure type for reporting. Accepts "Static" or "Dynamic". Default is "Static".
 
.EXAMPLE
# Retrieve infrastructure server details from AD
Get-InfraXA7Inventory -Type "Infra" `
                      -DN "OU=Citrix XenDesktop 7,DC=test,DC=domain,DC=LOCAL" `
                      -InfraType "Static"
 
.EXAMPLE
# Retrieve XA7 Citrix machines from a specified Delivery Controller
Get-InfraXA7Inventory -Type "XA7" `
                      -AdminAddress "ddc001" `
                      -InfraType "Dynamic"
 
.NOTES
This function supports both static AD-based queries and dynamic Citrix-based inventory for Power BI or audit purposes.
#>



function Get-InfraXA7Inventory {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, Position = 0)]
        [ValidateSet("Infra", "XA7")]
        [string]$Type,

        [Parameter(Mandatory = $false)]
        [string]$DN = "",

        [Parameter(Mandatory = $false)]
        [string]$Name = "*",

        [Parameter(Mandatory = $false)]
        [string]$AdminAddress,

        [Parameter(Mandatory = $false)]
        [ValidateSet("Static", "Dynamic")]
        [string]$InfraType = "Static"
    )

    switch ($Type) {
        "Infra" {
            try {
                if (-not $DN) {
                    Write-LogEntry -Message "Distinguished Name (DN) is required for Infra type."
                    return
                }

                $searchRoot = "LDAP://$DN"
                $directorySearcher = New-Object System.DirectoryServices.DirectorySearcher([ADSI]$searchRoot)
                $directorySearcher.Filter = "(&(objectCategory=computer)(name=$Name))"
                $directorySearcher.PageSize = 1000
                $directorySearcher.PropertiesToLoad.AddRange(@("name", "operatingSystem"))

                $results = $directorySearcher.FindAll()
                foreach ($result in $results) {
                    $os = $result.Properties["operatingsystem"]
                    $osType = if ($os.Count -gt 0) { $os[0] } else { "Unknown" }
                    $simplifiedOS = if ($osType -match 'Windows Server (\d{4})') { "Windows $($matches[1])" } else { $osType }

                    [PSCustomObject]@{
                        InfraType  = $InfraType
                        ServerName = $result.Properties["name"][0]
                        OSType     = $simplifiedOS
                        Type       = "Infrastructure Servers"
                    }
                }
            }
            catch {
                Write-LogEntry -Message "Infra query failed: $_"
            }
        }

        "XA7" {
            try {
                $deployment = if ($AdminAddress -match "VAUS") { "DR" } else { "Prod" }

                Get-BrokerMachine -AdminAddress $AdminAddress -MaxRecordCount ([int]::MaxValue) -SessionSupport MultiSession |
                ForEach-Object {
                    [PSCustomObject]@{
                        InfraType  = "XA7"
                        ServerName = $_.HostedMachineName
                        OSType     = $_.OSType
                        Type       = $deployment
                    }
                }
            }
            catch {
                Write-LogEntry -Message "XA7 query failed: $_"
            }
        }
    }
}