WVD Set Drain Mode Powershell

This is a blogpost about a script to set drain mode for all session hosts via powershell in one WVD hostpool. This is a script for the spring release. The fall release CMDLets of WVD uses other CMDlets.

WVD Script

Firstly, we need to connect to Azure using Powershell. We can do this by using the following command:

Connect-AzAccount

After that you need to make sure you are using the correct Azure subscription in context. You can make sure by using this command:

Select-AzSubscription -SubscriptionId <SubScriptionID> | Set-AzContext

Next, we can run the “WVD set drain mode Powershell script”:

The update-AzWvdSessionHost CMDlet is used. You can read more about it here.

$ResourceGroupName = "RESOURCEGROUPNAME"
$Hostpoolname = "HOSTPOOLNAME"
$WVDSessionhosts = Get-AzWvdSessionHost -HostPoolName $Hostpoolname -ResourceGroupName $ResourceGroupName

foreach ($WVDSessionHost in $WVDSessionhosts) {
    $InputString = $WVDSessionhost.Name
    $WVDArray = $InputString.Split("/")
    $WVDArray[0]
    $WVDArray[1]

    Write-host Disabling new sessions for session host: $WVDArray[1]
    Update-AzWvdSessionHost -HostPoolName $Hostpoolname -ResourceGroupName $ResourceGroupName -Name $WVDArray[1] -AllowNewSession:$false
}

When you run the script the output is shown per Session Host and looks like this:

WVD set drain mode powershell output

The for-each loop shown in the script example is somewhat special and needs some explanation. This the for-each loop:

foreach ($WVDSessionHost in $WVDSessionhosts) {
    $InputString = $WVDSessionhost.Name
    $WVDArray = $InputString.Split("/")
    $WVDArray[0]
    $WVDArray[1]

    Write-host Disabling new sessions for session host: $WVDArray[1]
    Update-AzWvdSessionHost -HostPoolName $Hostpoolname -ResourceGroupName $ResourceGroupName -Name $WVDArray[1] -AllowNewSession:$false
}

We need to split the array that contains the string which is the name of the WVD Sessionhost. The original name of this value is. For example:

DEVRel46/VMWVDDEVRel46-2.nielskok.tech

When you use this to disallow session you will see this error:

Error for setting drain mode in powershell

We need to make sure that only this name remains:

VMWVDDEVRel46-2.nielskok.tech

So we need to split the array at the “/”. We are achieving this by using this piece of code:

    $InputString = $WVDSessionhost.Name
    $WVDArray = $InputString.Split("/")
    $WVDArray[0]
    $WVDArray[1]

This puts the hostname of the WVD Session Host in the “$WVDArray[1]” array to make sure the correct name is selected and can be found as a session host.

After this you can add this script in to your Azure DevOps configuration to help you simplify your WVD Deployment!

References

Microsoft docs about update-wvdsessionhost

Split array in Powershell

Blog about Azure information Protection



12 thoughts on “WVD Set Drain Mode Powershell”

  1. Pingback: essay
  2. thanks for sharing, I have follow your script but how I can run it through Azure Automate or any other which run every evening automatically to enable the drain mode?

    Reply
    • Hello,

      You can create an Azure Automation account and give the Service Principal from that Azure Automation account permissions on your subscription. After that you can run this script via the Azure Run As option in the Runbook of the automation account.

      Kind Regards,

      Niels

      Reply
  3. Hi Niels,
    You helped me before 😉 hope you have an answer to my question.
    How can I set drain mode on on newly released AvD VM’s in DevOps.
    I don’t want all sessionshosts in the Hostpool in drain mode, only the new machines.
    So I’m looking for a pipeline variable that only contains the VM’s that are released.
    Do you know if this variable?

    Regards,

    Reply
    • Hi Floris,

      Thanks for your response. I think you should add a tag which holds the date (and maybe the time) that you deployed the VM’s. You can add these tags via the ARM or BICEP template as variables in your deployment pipelines. After that, you could select the newly created VM’s via the tag that you added with the date.

      Hope this is what you need.

      Niels

      Reply

Leave a Comment