Public/Get-VDIComputerList.ps1

<#
.SYNOPSIS
    Retrieves a list of computer names from a specified organizational unit.
 
.DESCRIPTION
    This function queries Active Directory to find computer accounts in a specified organizational unit (OU).
    It can optionally filter computers based on a naming pattern.
 
.PARAMETER DN
    The Distinguished Name (DN) of the organizational unit (OU) from which to retrieve the computer accounts.
 
.PARAMETER Name
    Optional. The name pattern to filter the computer accounts. If omitted, all computers in the OU will be listed.
 
.EXAMPLE
    PS> Get-VDIComputerList -DN "OU=VDI,DC=example,DC=com"
    This example retrieves all computer accounts in the "VDI" organizational unit.
 
.EXAMPLE
    PS> Get-VDIComputerList -DN "OU=VDI,OU=Citrix XenDesktop 7,OU=Resources,OU=Enterprise,DC=americas,DC=PPDI,DC=LOCAL" -Name "*RPAPB*" |Sort-Object
    This example retrieves computer accounts in the "VDI" organizational unit that include "VDI" in their names.
 
.OUTPUTS
    String
    Outputs the names of the computer accounts.
 
.NOTES
    Requires Citrix PowerShell SDK and appropriate administrative credentials.
#>


Function Get-VDIComputerList {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [string]$DN,
        
        [Parameter(Mandatory = $false, ValueFromPipeline = $true)]
        [string]$Name = "*"
    )

    Begin {
        $OU = "LDAP://$DN"
        $dateTime = Get-Date -Format "\[dd/MM/yy HH:mm:ss\]"
        Write-Verbose "Query started at $dateTime"
    }

    Process {
        Try {
            $searcher = New-Object DirectoryServices.DirectorySearcher([ADSI]$OU)
            $searcher.PageSize = 1000
            $searcher.Filter = "(&(objectCategory=computer)(name=$Name))"
            $searcher.PropertiesToLoad.Add('name')
            
            $results = $searcher.FindAll()

            if ($results.Count -eq 0) {
                Write-Host "No computers found matching criteria in $DN."
            } else {
                foreach ($computer in $results) {
                    $computer.Properties['name'][0]
                }
            }
        } Catch {
            Write-Error "Failed to query Active Directory: $_"
        } finally {
            $searcher.Dispose()
        }
    }

    End {
        Write-Verbose "Query completed at $(Get-Date -Format "\[dd/MM/yy HH:mm:ss\]")"
    }
}