Public/Get-ServerInformation.ps1

<#
.SYNOPSIS
    Retrieves information about servers based on the specified criteria.
 
.DESCRIPTION
    This function retrieves information about servers based on the specified criteria. It supports two types of servers: infrastructure servers and XenApp 7 servers.
 
.PARAMETER Type
    Specifies the type of servers to retrieve. Valid values are "Infra" for infrastructure servers and "XA7" for XenApp 7 servers.
 
.PARAMETER DN
    Distinguished Name (DN) of the OU for infrastructure servers. Required if Type is "Infra".
 
.PARAMETER Name
    Name pattern of the infrastructure servers to filter. Default is "*" (wildcard).
 
.PARAMETER AdminAddress
    Admin address for XenApp 7 servers. Required if Type is "XA7".
 
.PARAMETER InfraType
    Type of infrastructure servers. Default is "Static".
 
.EXAMPLE
    # Retrieve information about infrastructure servers in the OU "OU=Servers,DC=example,DC=com" with the name pattern "Server*" and InfraType as "Static"
    Get-ServerInformation -Type Infra -DN "OU=Servers,DC=example,DC=com" -Name "Server*" -InfraType Static
 
.EXAMPLE
    # Retrieve information about XenApp 7 servers with the AdminAddress "XA7-Prod"
    Get-ServerInformation -Type XA7 -AdminAddress <ddc> -InfraType Dynamic
 
.NOTES
    Requires Active Directory module for PowerShell and appropriate permissions to query AD objects.
#>

function Get-ServerInformation {
    [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"
    )

    process {
        if ($Type -eq "Infra") {
            $OU = "LDAP://" + $DN
            $searcher = [adsisearcher]"(&(objectcategory=computer)(name=$Name))"
            $searcher.SearchRoot = [adsi]$OU
            $searcher.PageSize = 1000
            $searcher.PropertiesToLoad.AddRange(('name', 'operatingSystem'))

            try {
                $searcher.FindAll() | ForEach-Object {
                    $osType = if ($_.properties['operatingsystem']) { $_.properties['operatingsystem'][0] } else { "Unknown" }
                    $simplifiedOSType = if ($osType -match 'Windows Server (\d{4})') { "Windows $($matches[1])" } else { $osType }

                    [PSCustomObject]@{
                        InfraType  = $InfraType
                        ServerName = $_.properties['name'][0]
                        OSType     = $simplifiedOSType
                        Type       = "Infrastructure Servers"
                    }
                }
            }
            catch {
                Write-Warning "An error occurred: $_"
            }
            finally {
                $searcher.Dispose()
            }
        }
        elseif ($Type -eq "XA7") {
            $deployedState = if ($AdminAddress -match "VAUS") { "DR" } else { "Prod" }
            Get-BrokerMachine -AdminAddress $AdminAddress -MaxRecordCount 50000 -SessionSupport MultiSession |
            Select-Object @{Name='InfraType'; Expression={'XA7'}},
                          @{Name='ServerName'; Expression={$_.HostedMachineName}},
                          @{Name='OSType'; Expression={$_.OSType}},
                          @{Name='Type'; Expression={$deployedState}}
        }
        else {
            Write-Warning "Invalid Type specified. Use 'Infra' for Infrastructure Servers or 'XA7' for XenApp 7 Servers."
        }
    }
}