Public/Get-MasterimageList.ps1

<#
.SYNOPSIS
    Retrieves a list of computer names from Active Directory based on a specified name filter.
 
.DESCRIPTION
    The Get-MasterimageList function queries Active Directory to get computer accounts that match a specific naming pattern. It is particularly useful for identifying temporary or specialized computer accounts within an organizational unit (OU).
 
.PARAMETER DN
    The Distinguished Name (DN) of the Active Directory organizational unit (OU) where the search will be performed. This parameter is mandatory.
 
.PARAMETER Name
    The name pattern to filter the computer accounts. Default is "*-TMP", which searches for all computers whose names end with '-TMP'.
 
.EXAMPLE
    PS> Get-MasterimageList -DN "OU=Workstations,DC=example,DC=com"
    This example searches for computer accounts in the "Workstations" OU that have names ending with '-TMP'.
 
.EXAMPLE
    PS> Get-MasterimageList -DN "OU=Workstations,DC=example,DC=com" -Name "*-Server"
    This example searches for computer accounts in the "Workstations" OU that have names ending with '-Server'.
     
.NOTES
    Requires Active Directory module for PowerShell and appropriate permissions to query AD objects.
#>


function Get-MasterimageList
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [string]$DN,

        [Parameter(Mandatory = $false, ValueFromPipeline = $true)]
        [string]$Name = "*-TMP"
    )

    Begin
    {
        $OU = "LDAP://$DN"
        Write-Verbose "Search will be conducted in: $OU with filter: $Name"
    }

    Process
    {
        try
        {
            $searcher = New-Object System.DirectoryServices.DirectorySearcher([ADSI]"$OU")
            $searcher.PageSize = 1000  # Controls the page size of the results
            $searcher.Filter = "(name=$Name)"

            $searcher.PropertiesToLoad.AddRange(@('name'))
            foreach ($Computer in $searcher.FindAll())
            {
                $Computer.Properties['name'][0]
            }
        }
        catch
        {
            Write-Error "Failed to retrieve or process data: $_"
        }
    }

    End
    {
        Write-Verbose "Cleaning up resources..."
        $searcher.Dispose()
        Remove-Variable -Name searcher,DN,OU,Name -ErrorAction SilentlyContinue
    }
}