This is a blogpost about a script which initiates a logoff for all users in a WVD Hostpool via Powershell. The script populates the hostpool(s) based on a Resource Group within Azure. If there are multiple hostpools within this Resource Group the users are logged off also.
Script Syntaxis
This is the syntax for the script. Below I will show an example of the script.
$ResourceGroupName = "RESOURCEGROUPNAME" $ExistingHostPool = Get-AzResource -ResourceGroupName $ResourceGroupName | Where-Object ResourceType -eq Microsoft.DesktopVirtualization/hostpools if (($ExistingHostPool).count -gt "0") { # Log off connected Users foreach($Hostpool in $ExistingHostPool){ $WVDUserSessions = Get-AzWvdUserSession -HostPoolName $HostPool.Name -ResourceGroupName $ResourceGroupName $NumberofWVDSessions = ($WVDUserSessions).count if ($NumberofWVDSessions -gt "0") { try { Write-Host "There are $NumberofWVDSessions logged on, they now will be logged off" foreach ($WVDUserSession in $WVDUserSessions){ $InputString = $WVDUserSession.Name $WVDUserArray = $InputString.Split("/") $WVDUserArray[0] $WVDUserArray[1] $WVDUserArray[2] Remove-AzWvdUserSession -HostPoolName $HostPool.Name -ResourceGroupName $ResourceGroupName -SessionHostName $WVDUserArray[1] -Id $WVDUserArray[2] } } catch { Write-Host "There 0 users logged on to Windows Virtual Desktop" } } #Check and Wait for users to be logged off while($true) { $NumberofWVDSessions = Get-AzWvdUserSession -HostPoolName $HostPool.Name -ResourceGroupName $ResourceGroupName; $CountNumberofWVDSessions = ($NumberofWVDSessions).count; $Hostpool.Name = $HostpoolName ; if($CountNumberofWVDSessions -gt "0") { Write-Output "There are still $CountNumberofWVDSessions user(s) on $HostpoolName logged on"; Start-Sleep -s 10 } else { break } } } }
Example
Firstly, check whether you have correct Powershell modules installed. Use the following code to do so:
$installedPackageProvider = Get-PackageProvider if ($installedPackageProvider.Name -notmatch "NuGet") { Install-PackageProvider -Name NuGet -force Write-Host("Install powershell module NuGet") } $installedModules = Get-InstalledModule if ($installedModules.Name -notmatch "Az.Accounts") { Install-Module Az.Accounts -Force -AllowClobber Write-Host("Install powershell module Az Accounts") } if ($installedModules.Name -notmatch "Az.Resources") { Install-Module Az.Resources -Force -AllowClobber Write-Host("Install powershell module Az Resources") } if ($installedModules.Name -notmatch "Az.DesktopVirtualization") { Install-Module Az.DesktopVirtualization -Force -AllowClobber Write-Host("Install powershell module Az DesktopVirtualization") }
Secondly, you need to connect to Azure Powershell. You can do so by using the following command:
Connect-AzAccount
Now it’s time to run the script. Save the script as a ps1 file and run it:
.\WVDLogoffScript.ps1
This is the output for the script:
First, .
Second, the name of the hostpool and sessionhost are shown.
After that, a “While Loop” waits for all the users to logoff.
The line “There are still 1 user(s) on WVDDEVRel67 logged on” keeps track of the while loop. Every 10 seconds this runs again.
NOTE: Users are logged off immediately. There is no warning. This is what the user sees:
And that’s how you logoff users via Powershell on a WVD Hostpool.
4 thoughts on “WVD Logoff users Hostpool Powershell”