再来分享两个很实用的脚本,都是关于Azure VDI的,第一个是批量给用户发送通知的脚本,这个在执行一些变更的时候非常有用,在变更之前可以跑一下然后批量给用户发送一些提醒,这样可以让用户提前知道,以便安排好时间
实现的效果基本就是下图这样的,会有个很大的框出来
代码直接就贴出来了,我自己是放在runbook里跑的,如果想直接在本地PowerShell中跑就把前边的身份验证部分改下就行,不过还是那句话,跑之前自己要先测试环境测试下
#这个没试过如果有用remote app的会是什么样
$AzureContext = (Connect-AzAccount -Identity -Environment AzureChinaCloud).context
# set and store context
$AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription -DefaultProfile $AzureContext
Select-AzSubscription -SubscriptionId "xxx";
# 定义Hostpool和resource group的对应关系
$VDIPools = [ordered]@{
'xxx' = 'xxx'
'xxxx' = 'xxxx'
'xxx' = 'xxx'
'xxxx' = 'xxxx'
}
foreach ($VDIPool in $VDIPools.GetEnumerator()) {
$Sessions = Get-AzWvdUserSession -ResourceGroupName $VDIPool.value -HostPoolName $VDIPool.key -ErrorAction stop
if ($Sessions.count -ne 0) {
foreach ($Session in $Sessions) {
try {
$Error.clear()
$SessionHostName = $Session.Name.Split("/")[-2]
$SessionID = $Session.Name.Split("/")[-1]
Write-Output "Sending notification to user $($Session.UserPrincipalName), session host name:$SessionHostName"
Send-AzWvdUserSessionMessage -ResourceGroupName $VDIPool.value -HostPoolName $VDIPool.key -SessionHostName $SessionHostName -UserSessionId $SessionID -MessageBody 'Dear users, the VDI will be in maintenance mode after 30 mins, maintenance window will be one hour, users will not able to use VDI during this period' -MessageTitle 'Maintenance Notification' -ErrorAction stop
}
catch {
Write-Output $Error[0].exception.message
}
}
}
else {
Write-Output "No session found for VDI Pool $($VDIPool.key)"
}
}
在runbook里的输出内容大概就是这样,因为是顺序执行的,不是在后台并行的,所以一般会需要一点时间