Private/Get-UserSamAccountNameByEmail.ps1

<#
.SYNOPSIS
Retrieves the SamAccountName of a user based on their email address in the current logon domain.
 
.DESCRIPTION
This function queries Active Directory to find the SamAccountName of a user by their email address.
It automatically detects the user's current logon domain and uses it to construct the LDAP search path.
If the user is not found or the function is executed outside of a domain environment, an appropriate error message is returned.
 
.PARAMETER EmailAddress
The email address of the user whose SamAccountName needs to be retrieved.
 
.EXAMPLE
Get-UserSamAccountNameByEmail -EmailAddress "user@example.com"
This command retrieves the SamAccountName for the user with the email "user@example.com"
in the domain of the currently logged-on user.
 
.NOTES
- This function requires the script to be run in a domain environment.
- The function automatically uses the current user's logon domain for the search.
- It supports searching for one user at a time.
 
.OUTPUTS
[string]
Returns the SamAccountName of the user if found.
 
.ERRORS
- If the user is not found in the domain, an error is returned.
- If the script is run outside of a domain environment, an error is returned.
 
#>

function Get-UserSamAccountNameByEmail {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory = $true)]
        [string]$EmailAddress
    )

    # Helper function to construct LDAP search root from domain name
    function Get-LDAPSearchRoot {
        param (
            [string]$Domain
        )
        return "LDAP://" + ($Domain -replace "\.", ",DC=")
    }

    # Helper function to perform AD search
    function Search-AD {
        Param(
            [string]$SearchRoot
        )

        Try {
            $searcher = New-Object System.DirectoryServices.DirectorySearcher
            $searcher.SearchRoot = New-Object System.DirectoryServices.DirectoryEntry($SearchRoot)
            $searcher.PageSize = 1000
            $searcher.Filter = "(&(objectCategory=User)(mail=$EmailAddress))"
            $searcher.SearchScope = "Subtree"

            $result = $searcher.FindOne()

            if ($null -ne $result) {
                $samaccountname = $result.Properties["samaccountname"][0]
                return $samaccountname
            } else {
                return $null
            }
        } Catch {
            Write-Warning "Error accessing AD on $SearchRoot : $_"
            return $null
        }
    }

    # Automatically get the current user's domain
    $userDomain = [Environment]::GetEnvironmentVariable("USERDOMAIN")
    if (-not $userDomain) {
        Write-Error "Unable to determine the user's domain. Ensure the script is run in a domain environment."
        return $null
    }

    # Construct LDAP search root
    $searchRoot = Get-LDAPSearchRoot -Domain $userDomain

    # Perform the AD search
    $samaccountname = Search-AD -SearchRoot $searchRoot
    if ($samaccountname) {
        return $samaccountname
    } else {
        Write-Error "User with email '$EmailAddress' not found in the domain '$userDomain'."
        return $null
    }
}