Public/Set-CitrixPrivateDesktopAssignment.ps1

<#
.SYNOPSIS
    Manages assignment of users to private desktops in a Citrix environment.
 
.DESCRIPTION
    This function assigns or removes a user from a private desktop within a Citrix environment
    based on specified parameters. It uses Citrix cmdlets to modify broker user settings.
 
.PARAMETER AdminAddress
    The address of the Citrix administration server.
 
.PARAMETER MachineName
    The name of the machine (private desktop) to which the user will be assigned or from which they will be removed.
 
.PARAMETER UserName
    The username of the account to be assigned or removed from the machine.
 
.PARAMETER Add
    If specified, the user will be added to the specified machine.
 
.PARAMETER Remove
    If specified, the user will be removed from the specified machine.
 
.EXAMPLE
    PS> Set-CitrixPrivateDesktopAssignment -AdminAddress "CitrixServer" -MachineName "VDI01" -UserName "john.doe" -Add
    This example adds the user 'john.doe' to the private desktop 'VDI01'.
 
.EXAMPLE
    PS> Set-CitrixPrivateDesktopAssignment -AdminAddress "CitrixServer" -MachineName "VDI01" -UserName "john.doe" -Remove
    This example removes the user 'john.doe' from the private desktop 'VDI01'.
 
.OUTPUTS
    String
    Outputs the status of the assignment or removal process.
 
.NOTES
    Requires Citrix PowerShell SDK and appropriate administrative credentials.
#>


Function Set-CitrixPrivateDesktopAssignment {
    [CmdletBinding()]
    [OutputType([String])]
    Param (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [string]$AdminAddress,
        
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [string]$MachineName,

        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [string]$UserName,

        [Alias('A')]
        [switch]$Add,

        [Alias('R')]
        [switch]$Remove
    )
    
    Begin {
        $CitrixParams = @{
            AdminAddress   = $AdminAddress
            Name           = $UserName
            PrivateDesktop = $MachineName
            ErrorAction    = 'Stop'
        }
    }
    
    Process {
        try {
            switch ($true) {
                {$Add}    {
                    Add-BrokerUser @CitrixParams
                    Write-Output "$UserName is added successfully to $MachineName"
                }
                {$Remove} {
                    Remove-BrokerUser @CitrixParams
                    Write-Output "$UserName is removed successfully from $MachineName"
                }
                default {
                    Write-Error "Please specify either -Add or -Remove."
                }
            }
        } catch {
            Write-Error "Error managing the sessions from $AdminAddress : $_"
        }
    }

    End {
        # Clean-up code if necessary
    }
}