Public/Get-CitrixUserProfileLocation.ps1

<#
.SYNOPSIS
    Retrieves the Citrix user profile location either from UPM or FSLogix.
 
.DESCRIPTION
    This function attempts to find the user profile location for a given user on a specified server.
    It checks for a Citrix UPM (User Profile Management) profile and if not found, checks for an FSLogix profile.
    The function returns the profile location after removing a specified domain suffix.
 
.PARAMETER ServerName
    The name of the server where the profile information is stored.
 
.PARAMETER SamAccountName
    The SAM account name of the user whose profile location is being checked.
 
.EXAMPLE
    PS> Get-CitrixUserProfileLocation -ServerName "Server01" -SamAccountName "jdoe"
    This example queries the server "Server01" for the profile location of user "jdoe".
 
.OUTPUTS
    String
    Returns the user profile path without the domain suffix if found, otherwise returns an informative message.
     
.NOTES
    Requires Citrix PowerShell SDK and appropriate administrative credentials.
#>



function Get-CitrixUserProfileLocation {
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
        [ValidateNotNullOrEmpty()]
        [string]$ServerName,

        [Parameter(Mandatory = $true, Position = 1)]
        [ValidateNotNullOrEmpty()]
        [string]$SamAccountName
    )

    Process {
        Try {
            if (-not (Test-Connection $ServerName -Count 1 -Quiet)) {
                Write-Output "Unable to reach server $ServerName."
                return
            }

            # Attempt to translate the SAM account name to SID
            $localAccount = New-Object System.Security.Principal.NTAccount($SamAccountName.Trim())
            $localSID = $localAccount.Translate([System.Security.Principal.SecurityIdentifier])
            $sidValue = $localSID.Value

            # Check for UPM User Store Location
            $upmLocation = Get-UserProfileLocation -ServerName $ServerName -SID $sidValue -Key "UPMUserStoreLocation"
            if ($upmLocation) {
                return $upmLocation -replace ([Regex]::Escape(".americas.ppdi.local")), ""
            }

            # Check for FSLogix Profile Location if UPM check fails
            $fsLogixLocation = Get-UserProfileLocation -ServerName $ServerName -SID $sidValue -Key "VHDRootFilePath" -Path "SOFTWARE\FSLogix\Profiles\Sessions"
            if ($fsLogixLocation) {
                return $fsLogixLocation -replace ([Regex]::Escape(".americas.ppdi.local")), ""
            }

            Write-Output "Profile location not found."
        } Catch {
            Write-Output "Error retrieving profile information: $_"
        }
    }
}