Enable workstation remote management through native domain policies
This commit is contained in:
1 parent
414a08ece6
commit
b07dbe17d0
7 files changed
+487
-26
No files matched your search
@@ -1,9 +1,53 @@
|
||||
[CmdletBinding()]
|
||||
param([string]$RemoteDesktopPrincipal = 'LCI\SG-Laboratorio-Usuarios-RDP')
|
||||
param(
|
||||
[string]$RemoteDesktopPrincipal = 'LCI\SG-Laboratorio-Usuarios-RDP',
|
||||
[string]$LocalDnsServer = '',
|
||||
[string]$FallbackDnsServer = ''
|
||||
)
|
||||
|
||||
$ErrorActionPreference = 'Stop'
|
||||
$computer = Get-CimInstance Win32_ComputerSystem
|
||||
if (-not $computer.PartOfDomain -or $computer.DomainRole -ge 4) { return }
|
||||
if (-not $computer.PartOfDomain -or $computer.DomainRole -ne 1) { return }
|
||||
|
||||
# A site-local DNS server is optional. Change only an adapter whose directly
|
||||
# connected subnet contains that server, after verifying its authoritative DNS
|
||||
# answer. Other sites, VPN adapters and Internet-only interfaces are preserved.
|
||||
if ($LocalDnsServer) {
|
||||
$dnsBytes = ([Net.IPAddress]::Parse($LocalDnsServer)).GetAddressBytes()
|
||||
foreach ($ip in @(Get-NetIPAddress -AddressFamily IPv4 | Where-Object {
|
||||
$_.AddressState -eq 'Preferred' -and $_.PrefixLength -gt 0 -and $_.PrefixLength -lt 32
|
||||
})) {
|
||||
$ipBytes = ([Net.IPAddress]::Parse($ip.IPAddress)).GetAddressBytes()
|
||||
$sameSubnet = $true
|
||||
for ($bit = 0; $bit -lt $ip.PrefixLength; $bit++) {
|
||||
$mask = 1 -shl (7 - ($bit % 8))
|
||||
$index = [int][Math]::Floor($bit / 8)
|
||||
if (($ipBytes[$index] -band $mask) -ne ($dnsBytes[$index] -band $mask)) {
|
||||
$sameSubnet = $false; break
|
||||
}
|
||||
}
|
||||
if (-not $sameSubnet) { continue }
|
||||
try {
|
||||
$authority = @(Resolve-DnsName -Name $computer.Domain -Type SOA -Server $LocalDnsServer -DnsOnly -ErrorAction Stop |
|
||||
Where-Object { $_.Name -ieq $computer.Domain -and $_.Type -eq 'SOA' })
|
||||
if (-not $authority) { throw 'The DNS server did not return the domain SOA.' }
|
||||
$desiredServers = @($LocalDnsServer)
|
||||
if ($FallbackDnsServer) {
|
||||
[Net.IPAddress]::Parse($FallbackDnsServer) | Out-Null
|
||||
$fallbackAuthority = @(Resolve-DnsName -Name $computer.Domain -Type SOA -Server $FallbackDnsServer -DnsOnly -ErrorAction Stop |
|
||||
Where-Object { $_.Name -ieq $computer.Domain -and $_.Type -eq 'SOA' })
|
||||
if (-not $fallbackAuthority) { throw 'Fallback DNS is not authoritative for this domain.' }
|
||||
$desiredServers += $FallbackDnsServer
|
||||
}
|
||||
$current = (Get-DnsClientServerAddress -InterfaceIndex $ip.InterfaceIndex -AddressFamily IPv4).ServerAddresses
|
||||
if ((@($current) -join ',') -ne ($desiredServers -join ',')) {
|
||||
Set-DnsClientServerAddress -InterfaceIndex $ip.InterfaceIndex -ServerAddresses $desiredServers
|
||||
}
|
||||
Set-DnsClient -InterfaceIndex $ip.InterfaceIndex -RegisterThisConnectionsAddress $true
|
||||
Register-DnsClient
|
||||
} catch { Write-Warning "Site DNS configuration failed: $($_.Exception.Message)" }
|
||||
}
|
||||
}
|
||||
|
||||
$terminalKey = 'HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server'
|
||||
$rdpKey = Join-Path $terminalKey 'WinStations\RDP-Tcp'
|
||||
@@ -49,10 +93,30 @@ foreach ($rule in $rules) {
|
||||
$listener = @(Get-ChildItem WSMan:\localhost\Listener -ErrorAction SilentlyContinue |
|
||||
Where-Object { $_.Keys -contains 'Transport=HTTP' })
|
||||
$endpoint = Get-PSSessionConfiguration -Name Microsoft.PowerShell -ErrorAction SilentlyContinue
|
||||
if (-not $listener -or -not $endpoint) {
|
||||
$endpointEnabled = Get-Item 'WSMan:\localhost\Plugin\Microsoft.PowerShell\Enabled' -ErrorAction SilentlyContinue
|
||||
if (-not $listener -or -not $endpoint -or $endpointEnabled.Value -eq 'false') {
|
||||
Enable-PSRemoting -Force -SkipNetworkProfileCheck | Out-Null
|
||||
}
|
||||
|
||||
# Native, language-independent rule names. The domain policy also distributes
|
||||
# these rules through the firewall CSE, so background gpupdate can repair them.
|
||||
$managementPatterns = @('RemoteEventLogSvc-*', 'RemoteSvcAdmin-*', 'WMI-*',
|
||||
'RemoteTask-*', 'RemoteFwAdmin-*', 'RVM-*', 'PerfLogsAlerts-*', 'FPS-SMB-In-TCP*')
|
||||
foreach ($pattern in $managementPatterns) {
|
||||
foreach ($nativeRule in @(Get-NetFirewallRule -Name $pattern -ErrorAction SilentlyContinue |
|
||||
Where-Object Direction -eq 'Inbound')) {
|
||||
# Do not try to mutate policy-owned rules in the workstation local store.
|
||||
if ($nativeRule.PolicyStoreSourceType -ne 'Local') { continue }
|
||||
$address = $nativeRule | Get-NetFirewallAddressFilter
|
||||
if ($nativeRule.Enabled -ne 'True' -or $nativeRule.Profile -ne 'Any' -or
|
||||
@($address.RemoteAddress).Count -ne 1 -or $address.RemoteAddress[0] -ne 'LocalSubnet') {
|
||||
$nativeRule | Set-NetFirewallRule -Enabled True -Profile Any -RemoteAddress LocalSubnet
|
||||
}
|
||||
}
|
||||
}
|
||||
$remoteRegistry = Get-Service RemoteRegistry
|
||||
if ($remoteRegistry.StartType -ne 'Manual') { Set-Service RemoteRegistry -StartupType Manual }
|
||||
|
||||
# The built-in local group has a stable SID across Windows display languages.
|
||||
$groupSid = [Security.Principal.SecurityIdentifier]::new('S-1-5-32-555')
|
||||
$groupName = ($groupSid.Translate([Security.Principal.NTAccount]).Value -split '\\', 2)[1]
|
||||
|
||||
Reference in new issue
Block a user