Manage workstation RDP and WinRM through client GPO

This commit is contained in:
alexrg committed 2026-09-23 08:36:16 -06:00
1 parent 63fbaf65dc
commit db35d86df1
5 files changed
+199

No files matched your search

@@ -0,0 +1,77 @@
[CmdletBinding(SupportsShouldProcess)]
param(
[string]$TargetOuDn = 'OU=Laboratorio,DC=lci,DC=lasalle,DC=mx',
[string]$GpoName = 'SGU - Windows client experience',
[string]$DomainController = $env:COMPUTERNAME
)
$ErrorActionPreference = 'Stop'
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
if (-not ([Security.Principal.WindowsPrincipal]::new($identity)).IsInRole(
[Security.Principal.WindowsBuiltInRole]::Administrator)) {
throw 'Run this script elevated on the domain controller.'
}
Import-Module ActiveDirectory -ErrorAction Stop
Import-Module GroupPolicy -ErrorAction Stop
$targetOu = Get-ADOrganizationalUnit -Identity $TargetOuDn -Server $DomainController
$domain = Get-ADDomain -Server $DomainController
if (-not $targetOu.DistinguishedName.EndsWith($domain.DistinguishedName,
[StringComparison]::OrdinalIgnoreCase)) {
throw 'The target OU is outside the domain.'
}
$domainName = $domain.DNSRoot
$gpo = Get-GPO -Name $GpoName -Domain $domainName -Server $DomainController -ErrorAction Stop
$links = @(Get-GPInheritance -Target $TargetOuDn -Domain $domainName -Server $DomainController).GpoLinks
if (-not @($links | Where-Object { $_.DisplayName -eq $GpoName -and
[string]$_.Enabled -in @('True','Yes') }).Count) {
throw "The existing GPO '$GpoName' must be linked and enabled on $TargetOuDn."
}
# The existing machine GPO also manages the credential tile. The startup
# script is a native Group Policy extension, not a scheduled client task.
$scriptName = 'Enable-SguWorkstationRemoteAccess.ps1'
$sourcePath = Join-Path $PSScriptRoot $scriptName
$startupRoot = Join-Path $env:WINDIR "SYSVOL\domain\Policies\{$($gpo.Id)}\Machine\Scripts"
$startupPath = Join-Path $startupRoot "Startup\$scriptName"
$scriptsIniPath = Join-Path $startupRoot 'scripts.ini'
$source = [IO.File]::ReadAllText($sourcePath)
$unc = "\\$domainName\SYSVOL\$domainName\Policies\{$($gpo.Id)}\Machine\Scripts\Startup\$scriptName"
$ini = "[Startup]`r`n0CmdLine=powershell.exe`r`n0Parameters=-NoProfile -NonInteractive -ExecutionPolicy Bypass -File `"$unc`"`r`n"
$scriptChanged = -not (Test-Path -LiteralPath $startupPath) -or
[IO.File]::ReadAllText($startupPath) -cne $source
$iniChanged = -not (Test-Path -LiteralPath $scriptsIniPath) -or
[IO.File]::ReadAllText($scriptsIniPath) -cne $ini
if (($scriptChanged -or $iniChanged) -and
$PSCmdlet.ShouldProcess($startupRoot, 'Install computer startup script')) {
New-Item -ItemType Directory -Path (Split-Path $startupPath) -Force | Out-Null
[IO.File]::WriteAllText($startupPath, $source, [Text.UTF8Encoding]::new($false))
[IO.File]::WriteAllText($scriptsIniPath, $ini, [Text.Encoding]::Unicode)
$gpoDn = "CN={$($gpo.Id)},CN=Policies,CN=System,$($domain.DistinguishedName)"
$adGpo = Get-ADObject -Identity $gpoDn -Server $DomainController `
-Properties gPCMachineExtensionNames,versionNumber
$extension = '[{42B5FAAE-6536-11D2-AE5A-0000F87571E3}{40B6664F-4972-11D1-A7CA-0000F87571E3}]'
$names = [string]$adGpo.gPCMachineExtensionNames
if ($names.IndexOf($extension, [StringComparison]::OrdinalIgnoreCase) -lt 0) {
Set-ADObject -Identity $gpoDn -Server $DomainController `
-Replace @{gPCMachineExtensionNames = "$names$extension"}
}
$gptPath = Join-Path (Split-Path (Split-Path $startupRoot -Parent) -Parent) 'GPT.ini'
$gpt = [IO.File]::ReadAllText($gptPath)
$gptVersion = [int]([regex]::Match($gpt, '(?m)^Version=(\d+)\r?$').Groups[1].Value)
$baseVersion = [Math]::Max($gptVersion, [int]$adGpo.versionNumber)
$nextVersion = ($baseVersion -band -65536) -bor (($baseVersion + 1) -band 65535)
[IO.File]::WriteAllText($gptPath,
[regex]::Replace($gpt, '(?m)^Version=\d+\r?$', "Version=$nextVersion"),
[Text.Encoding]::ASCII)
Set-ADObject -Identity $gpoDn -Server $DomainController -Replace @{versionNumber = $nextVersion}
}
[pscustomobject]@{
GpoName = $GpoName
GpoId = $gpo.Id
TargetOu = $TargetOuDn
StartupScript = $startupPath
Changed = $scriptChanged -or $iniChanged
}