2020-06-11

Microsoft VDI 內類似 Dynamic DNS (DDNS)的功能 (Part 2)

這篇 之後重新改寫 Script

不需要用 GPO 讓 Client 跑 Logon Script 寫資料到 Share Folder
再用另一台 Server 檢查資料去更新 Record

改為單一程式放在 Server 上面跑即可 (須具備相關 Remote Management Tools)

$DomainName = "Contoso.com"
$DefaultActivedRDCBMaster = "RDCB-01.$DomainName"

$ActiveRDCBMaster = $(Get-RDConnectionBrokerHighAvailability -ConnectionBroker $DefaultActivedRDCBMaster).ActiveManagementServer
$DNSServerName = (Get-DnsServer | select @{N='DNSServerName';E={$_.ServerSetting.ComputerName}}).DNSServerName

$RDVCollections = Get-RDVirtualDesktopCollection -ConnectionBroker $ActiveRDCBMaster | Select CollectionName
$RDSessions = Get-RDUserSession -ConnectionBroker $ActiveRDCBMaster | Select-Object –Property CollectionName,UserName,ServerName
$DNSCNameRecords = Get-DnsServerResourceRecord -ComputerName $DNSServerName -ZoneName "contoso.com" -RRType "CNAME" | select @{N='HostName';E={$_.HostName.ToLower()}},@{N='Record';E={$_.RecordData.HostNameAlias.ToLower()}}

write-host ('Check to Add');

foreach ($Collection in $RDVCollections) {
foreach ($RDSession in $RDSessions) {
if ($RDSession.CollectionName -eq $Collection.CollectionName) {
$CNameHost = ($RDSession.UserName + '-' + ($RDSession.CollectionName -replace "_","")).ToLower();
$CNameTarget = ($RDSession.ServerName + '.' + $DomainName + '.').ToLower();
$Hit = 0;
foreach ($DNSCNameRecord in $DNSCNameRecords) {
if ($DNSCNameRecord.HostName -eq $CNameHost) {
$Hit = 1;
break;
};
};
if ($Hit -eq 1) {
write-host ('Keep ' + $DNSCNameRecord);
Continue;
} else {
write-host ('Add ' + $CNameHost + ' @ ' + $CNameTarget);
Add-DnsServerResourceRecordCName -ComputerName $DNSServerName -Name $CNameHost -HostNameAlias $CNameTarget -ZoneName $DomainName
};
};
};
};

$DNSCNameRecords = Get-DnsServerResourceRecord -ZoneName "contoso.com" -RRType "CNAME" | select @{N='HostName';E={$_.HostName.ToLower()}},@{N='Record';E={$_.RecordData.HostNameAlias.ToLower()}}

write-host ('Check to Remove');

foreach ($DNSCNameRecord in $DNSCNameRecords) {
$Hit = 0;
$CollectionHit = 0;
foreach ($Collection in $RDVCollections) {
foreach ($RDSession in $RDSessions) {
if ($RDSession.CollectionName -eq $Collection.CollectionName) {
$CNameHost = ($RDSession.UserName + '-' + ($RDSession.CollectionName -replace "_","")).ToLower();
$CNameTarget = ($RDSession.ServerName + '.' + $DomainName + '.').ToLower();
if ($CNameHost -eq $DNSCNameRecord.HostName) {
$Hit = 1;
break;
};
};
};
if ($DNSCNameRecord.HostName.IndexOf(($RDSession.CollectionName -replace "_","").ToLower()) -ge 0) {
$CollectionHit = 1;
};
};
if ($Hit -eq 1) {
write-host ('Keep ' + $DNSCNameRecord);
Continue;
} else {
if ($CollectionHit -eq 1) {
write-host ('Remove ' + $DNSCNameRecord);
#Remove-DnsServerResourceRecord -ComputerName $DNSServerName -ZoneName $DomainName -RRType "CNAME" -Name $DNSCNameRecord.HostName -Force
} else {
write-host ('Ignore ' + $DNSCNameRecord);
};
};
};




沒有留言:

張貼留言