Public/Stop-AvdSessionHost.ps1
function Stop-AvdSessionHost { <# .SYNOPSIS Stops AVD Session hosts in a specific hostpool. .DESCRIPTION This function stops sessionshosts in a specific Azure Virtual Desktop hostpool. If you want to start a specific session host then also provide the name, .PARAMETER HostpoolName Enter the AVD Hostpool name .PARAMETER ResourceGroupName Enter the AVD Hostpool resourcegroup name .PARAMETER SessionHostName Enter the session hosts name .EXAMPLE Stop-AvdSessionHost -HostpoolName avd-hostpool-personal -ResourceGroupName rg-avd-01 .EXAMPLE Stop-AvdSessionHost -HostpoolName avd-hostpool-personal -ResourceGroupName rg-avd-01 -SessionHostName avd-host-1.avd.domain #> [CmdletBinding(DefaultParameterSetName = 'All')] param ( [parameter(Mandatory, ParameterSetName = 'All')] [parameter(Mandatory, ParameterSetName = 'Hostname')] [ValidateNotNullOrEmpty()] [string]$HostpoolName, [parameter(Mandatory, ParameterSetName = 'All')] [parameter(Mandatory, ParameterSetName = 'Hostname')] [ValidateNotNullOrEmpty()] [string]$ResourceGroupName, [parameter(Mandatory, ParameterSetName = 'All')] [parameter(Mandatory, ParameterSetName = 'Hostname')] [ValidateNotNullOrEmpty()] [string]$Name, # [ValidatePattern('^(?:(?!\/).)*$', ErrorMessage = "It looks like you also provided a hostpool, a sessionhost name is enough. Provided value {0}")] [parameter(Mandatory, ParameterSetName = 'Resource', ValueFromPipelineByPropertyName)] [ValidateNotNullOrEmpty()] [object]$Id, [parameter(ParameterSetName = 'All')] [parameter(ParameterSetName = 'Resource')] [parameter(ParameterSetName = 'Hostname')] [ValidateNotNullOrEmpty()] [switch]$Deallocate, [parameter(ParameterSetName = 'All')] [ValidateNotNullOrEmpty()] [switch]$Force ) Begin { Write-Verbose "Stopping session hosts" AuthenticationCheck $token = GetAuthToken -resource $Script:AzureApiUrl $apiVersion = "?api-version=2021-11-01" $sessionHostParameters = @{ hostpoolName = $HostpoolName resourceGroupName = $ResourceGroupName } $task = 'powerOff' if ($Deallocate.IsPresent) { $task = 'deallocate' } } Process { switch ($PsCmdlet.ParameterSetName) { All { CheckForce -Force:$force -Task $MyInvocation.MyCommand } Hostname { $Name = ConcatSessionHostName -name $Name $sessionHostParameters.Add("Name", $Name) } Resource { Write-Verbose "Got a resource object, looking for $Id" $sessionHostParameters = @{ Id = $Id } } default { } } try { $sessionHosts = Get-AvdSessionHost @sessionHostParameters } catch { Throw "No sessionhosts ($name) found in $HostpoolName ($ResourceGroupName), $_" } $sessionHosts | ForEach-Object { try { Write-Verbose "Found $($sessionHosts.Count) host(s)" Write-Verbose "Starting $($_.name)" $powerOffParameters = @{ uri = "{0}{1}/{2}{3}" -f $Script:AzureApiUrl, $_.properties.resourceId, $task, $apiVersion Method = "POST" Headers = $token } Invoke-RestMethod @powerOffParameters Write-Information -MessageData "$($_.name) stopped" -InformationAction Continue } catch { Throw "Not able to stop $($_.name), $_" } } } } |