2020-11-24

WVD Scale session hosts Without using Azure Automation

There is a lot of stuff to study to use Azure Automation.
Therefor I Write a simple PowerShell Script to manage the Scaling of WVD Pool.

You have to use the script with saved credential. (Reference)

######## Script Start ########

$ResourceGroupName = 'WVD_Infrastructure'; # ResourceGroup that contains HostPool 

$WorkDayTimeStart = '08:30';

$WorkDayTimeEnd = '18:30';


$WorkTimeAvailableSessionThreshold = 2; #New VM will Start if remain this session only

$RestTimeAvailableSessionThreshold = 0; #New VM will Start if remain this session only

$CredentailFileName = "AzCredential.txt";

 


[string[]]$CredentialRead = Get-Content -Path $CredentailFileName;


$UserName = [System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String($CredentialRead[0]));

$Password = $CredentialRead[1] | ConvertTo-SecureString -Key (1..16)


$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $UserName, $Password;


$ConnectAzAccount = Connect-AzAccount -Credential $Credential;


$AvailableSessionThreshold = $RestTimeAvailableSessionThreshold;


$WorkDay = 1;

if ( (((Get-Date).DayOfWeek.value__) -eq 6) -or (((Get-Date).DayOfWeek.value__) -eq 0) ) {

$WorkDay = 0;

};


if ($WorkDay -eq 1) {

$WorkDayTimeStart = Get-Date $WorkDayTimeStart;

$WorkDayTimeEnd = Get-Date $WorkDayTimeEnd;

$NowTime = Get-Date;


if (($NowTime.TimeOfDay -ge $WorkDayTimeStart.TimeOfDay) -and ($NowTime.TimeOfDay -le $WorkDayTimeEnd.TimeOfDay)) {

  $AvailableSessionThreshold = $WorkTimeAvailableSessionThreshold;

} else {

$WorkDay = 0;

};

};


if ($WorkDay -eq 1) {

write-host ("It's WorkDay");

} else {

write-host ("It's Holiday");

};


$HostPoolInfo = Get-AzWvdHostPool -ResourceGroupName $ResourceGroupName


ForEach ($HostPool in $HostPoolInfo) {


$HostList = Get-AzWvdSessionHost -ResourceGroupName $ResourceGroupName -HostPoolName $HostPool.Name | select Name,Status,Session,UpdateState,ResourceId;

$UserList = Get-AzWvdUserSession -ResourceGroupName $ResourceGroupName -HostPoolName $HostPool.Name;


$HostListCount = 0;

$ConnectedSessions = 0;

$AvailableSessionsRemain = 0;

$MaxSessionLimitOnAvailableHost = 0;

ForEach ($SessionHost in $HostList) {

$HostListCount++;

if ($SessionHost.Status -eq 'Available') {

$ConnectedSessions += $SessionHost.Session;

$AvailableSessionsRemain += ($HostPool.MaxSessionLimit - $SessionHost.Session);

$MaxSessionLimitOnAvailableHost += $HostPool.MaxSessionLimit;

Foreach ($User in $UserList) {

if ($User.Name -Like ('*'+$SessionHost.Name+'*')) {

write-host ('HostPool: ' + $SessionHost.Name + "`t" + $User.ActiveDirectoryUserName);

};

};

};

};

$MaxSessions = ($HostListCount * $HostPool.MaxSessionLimit);


if ($AvailableSessionsRemain -ge $AvailableSessionThreshold) {

Write-Host ('HostPool: ' + $HostPool.Name + ' -> No need to add Host (' + $ConnectedSessions + '/' + $MaxSessionLimitOnAvailableHost + ' of MAX ' + $MaxSessions + ')');

ForEach ($SessionHost in $HostList) {

if ( ($SessionHost.Status -eq 'Available') -and ($SessionHost.Session -eq 0) ) {

$AvailableSessionsRemain = 0;

$MaxSessionLimitOnAvailableHost = 0;

ForEach ($AvailableHost in $HostList) {

if ($AvailableHost.Status -eq 'Available') {

if ($AvailableHost.Name -ne $SessionHost.Name) {

$MaxSessionLimitOnAvailableHost += $HostPool.MaxSessionLimit;

# A VM can stand for how many sessions

$AvailableSessionsRemain += $AvailableHost.Session;

# Online Sessions Count

};

};

};

if (($MaxSessionLimitOnAvailableHost - $AvailableSessionsRemain) -ge $AvailableSessionThreshold) {

Write-Host ('HostPool: ' + $SessionHost.Name + ' -> Stop to Save Money');

$StopHostResult = Get-AzResource -ResourceID $SessionHost.ResourceId | Stop-AzVM -Force;

if ($StartExtraHostResult.Status -eq 'Succeeded') {

Write-Host ('HostPool: ' + $SessionHost.Name + ' -> Stop Succeeded @ ' + $StartExtraHostResult.EndTime);

};

break;

};

};

};

Continue;

};

Write-Host ('HostPool: ' + $HostPool.Name + ' -> Need to add Host (' + $ConnectedSessions + '/' + $MaxSessionLimitOnAvailableHost + ' of MAX ' + $MaxSessions + ')');


if (($MaxSessions - $ConnectedSessions) -le $AvailableSessionThreshold) {

Write-Host ('HostPool: ' + $HostPool.Name + ' -> No more VM to Start (' + $ConnectedSessions + '/' + $MaxSessionLimitOnAvailableHost + ' of MAX ' + $MaxSessions + ')');

Continue;

};


if ($StartExtraHost -eq 1) {

ForEach ($SessionHost in $HostList) {

if ($SessionHost.Status -ne 'Available') {

Write-Host ('HostPool: ' + $SessionHost.Name + ' -> Staring');

$StartExtraHostResult = Get-AzResource -ResourceID $SessionHost.ResourceId | Start-AzVM;

if ($StartExtraHostResult.Status -eq 'Succeeded') {

Write-Host ('HostPool: ' + $SessionHost.Name + ' -> Start Succeeded @ ' + $StartExtraHostResult.EndTime);

break;

};

};

};

};

};

沒有留言:

張貼留言