Enable workstation remote management through native domain policies

This commit is contained in:
alexrg committed 2026-09-30 10:39:52 -06:00
1 parent 414a08ece6
commit b07dbe17d0
7 files changed
+487 -26

No files matched your search

+92
View File
@@ -0,0 +1,92 @@
[CmdletBinding()]
param(
[Parameter(Mandatory)][uri]$Endpoint,
[Parameter(Mandatory)][PSCredential]$Credential,
[string]$DomainController = $env:COMPUTERNAME,
[string[]]$VerifyNodes = @()
)
$ErrorActionPreference = 'Stop'
Import-Module ActiveDirectory
Import-Module Microsoft.PowerShell.Utility
$base = $Endpoint.AbsoluteUri.TrimEnd('/')
$webSession = [Microsoft.PowerShell.Commands.WebRequestSession]::new()
# WAC v2's native form-login protocol, including CSRF and RSA-OAEP-SHA256.
# This signs in with the supplied existing account; no authentication settings
# or stored passwords are changed. Cookies remain only in this process.
try {
$page = Invoke-WebRequest -Uri "$base/" -WebSession $webSession -UseBasicParsing
$html = $page.Content
} catch {
if ([int]$_.Exception.Response.StatusCode -ne 403) { throw }
$reader = [IO.StreamReader]::new($_.Exception.Response.GetResponseStream())
try { $html = $reader.ReadToEnd() } finally { $reader.Dispose() }
}
$csrfMatch = [regex]::Match($html, 'var csrf\s*=\s*"([^"]+)"')
if (-not $csrfMatch.Success) {
$input = [regex]::Match($html, '<input\b[^>]*\bid="csrf"[^>]*>').Value
$csrfMatch = [regex]::Match($input, '\bvalue="([^"]+)"')
}
if (-not $csrfMatch.Success) { throw 'WAC form-login page did not supply a CSRF token.' }
$csrf = [Net.WebUtility]::HtmlDecode($csrfMatch.Groups[1].Value)
$key = Invoke-RestMethod -Uri "$base/api/user/key" -Method Post -WebSession $webSession `
-ContentType 'application/json' -Body (@{csrf=$csrf} | ConvertTo-Json -Compress)
function ConvertFrom-Base64Url([string]$Text) {
$text = $Text.Replace('-', '+').Replace('_', '/')
while ($text.Length % 4) { $text += '=' }
return [Convert]::FromBase64String($text)
}
$rsa = [Security.Cryptography.RSACng]::new()
try {
$parameters = [Security.Cryptography.RSAParameters]::new()
$parameters.Modulus = ConvertFrom-Base64Url $key.jwk.n
$parameters.Exponent = ConvertFrom-Base64Url $key.jwk.e
$rsa.ImportParameters($parameters)
$plainPassword = ([Net.NetworkCredential]::new('', $Credential.Password)).Password
$data = @{username=$Credential.UserName; password=$plainPassword; csrf=$csrf} | ConvertTo-Json -Compress
$bytes = [Text.Encoding]::UTF8.GetBytes($data)
$packet = [Convert]::ToBase64String($rsa.Encrypt($bytes, [Security.Cryptography.RSAEncryptionPadding]::OaepSHA256))
$plainPassword = $null; $data = $null
[Array]::Clear($bytes, 0, $bytes.Length)
} finally { $rsa.Dispose() }
$login = Invoke-WebRequest -Uri "$base/api/user/login" -Method Post -WebSession $webSession `
-UseBasicParsing -ContentType 'application/json' -Body (@{packet=$packet;csrf=$csrf} | ConvertTo-Json -Compress)
if ($login.StatusCode -ne 200) { throw 'WAC rejected the supplied account.' }
$existing = Invoke-RestMethod -Uri "$base/api/connections" -WebSession $webSession
$computers = @(Get-ADComputer -Server $DomainController -Filter 'Enabled -eq $true' -Properties OperatingSystem,DNSHostName |
Where-Object { $_.DNSHostName -and $_.OperatingSystem -like 'Windows*' })
$pending = @(
foreach ($computer in $computers) {
$type = 'msft.sme.connection-type.windows-client'
if ($computer.OperatingSystem -like '*Server*') { $type = 'msft.sme.connection-type.server' }
$name = $computer.DNSHostName.ToLowerInvariant()
$id = "$type!$name!global"
$tags = @('Dominio', 'La Salle')
$current = $existing.value | Where-Object { $_.id -eq $id -or $_.name -eq $id -or
($_.properties.name -ieq $name -and $_.properties.type -eq $type -and $_.properties.groupId -eq 'global') }
if ($current) { continue }
[pscustomobject]@{name=$name;type=$type;tags=$tags;groupId='global';id=$id}
}
)
if ($pending.Count) {
$response = Invoke-RestMethod -Uri "$base/api/connections" -Method Put -WebSession $webSession `
-ContentType 'application/json' -Body (ConvertTo-Json -InputObject @($pending) -Depth 4)
if ($response.errors) { throw ($response.errors | ConvertTo-Json -Depth 4) }
}
$verified = Invoke-RestMethod -Uri "$base/api/connections" -WebSession $webSession
$shared = @($verified.value | Where-Object { $_.properties.groupId -eq 'global' })
$missing = @($computers | Where-Object {
$shared.properties.name -inotcontains $_.DNSHostName
})
if ($missing.Count) { throw "WAC inventory verification failed: $($missing.Name -join ', ')" }
[pscustomobject]@{DomainComputers=$computers.Count;Added=$pending.Count;SharedConnections=$shared.Count;Missing=$missing.Count}
foreach ($node in $VerifyNodes) {
if ($node -notmatch '^[A-Za-z0-9.-]+$') { throw 'Invalid verification hostname.' }
$uri = "$base/api/services/WinREST/CIM/nodes/$node/namespaces/root.cimv2/classes/Win32_OperatingSystem/instances"
$response = Invoke-WebRequest -Uri $uri -WebSession $webSession -UseBasicParsing
$result = $response.Content | ConvertFrom-Json
$instances = @($result.value | Where-Object { $null -ne $_ })
if (-not $instances.Count) { throw "WAC returned no OS instances for $node." }
[pscustomobject]@{Node=$node;Status=$response.StatusCode;Instances=$instances.Count;ReturnedData=$true}
}