79 lines
3.8 KiB
PowerShell
79 lines
3.8 KiB
PowerShell
[CmdletBinding()]
|
|
param([string]$RemoteDesktopPrincipal = 'LCI\SG-Laboratorio-Usuarios-RDP')
|
|
|
|
$ErrorActionPreference = 'Stop'
|
|
$computer = Get-CimInstance Win32_ComputerSystem
|
|
if (-not $computer.PartOfDomain -or $computer.DomainRole -ge 4) { return }
|
|
|
|
$terminalKey = 'HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server'
|
|
$rdpKey = Join-Path $terminalKey 'WinStations\RDP-Tcp'
|
|
if ((Get-ItemPropertyValue -Path $terminalKey -Name fDenyTSConnections) -ne 0) {
|
|
Set-ItemProperty -Path $terminalKey -Name fDenyTSConnections -Value 0
|
|
}
|
|
if ((Get-ItemPropertyValue -Path $rdpKey -Name UserAuthentication) -ne 1) {
|
|
Set-ItemProperty -Path $rdpKey -Name UserAuthentication -Value 1
|
|
}
|
|
|
|
foreach ($name in @('TermService', 'WinRM')) {
|
|
$service = Get-Service -Name $name
|
|
if ($service.StartType -ne 'Automatic') { Set-Service -Name $name -StartupType Automatic }
|
|
if ($service.Status -ne 'Running') { Start-Service -Name $name }
|
|
}
|
|
|
|
# Each domain refresh carries this script through SYSVOL. Apply only the
|
|
# narrowly scoped inbound rules required by an administrator on the same LAN.
|
|
$rules = @(
|
|
@{ Name = 'SGU-Workstation-WinRM-HTTP'; Protocol = 'TCP'; Port = '5985' },
|
|
@{ Name = 'SGU-Workstation-RDP-TCP'; Protocol = 'TCP'; Port = '3389' },
|
|
@{ Name = 'SGU-Workstation-RDP-UDP'; Protocol = 'UDP'; Port = '3389' }
|
|
)
|
|
foreach ($rule in $rules) {
|
|
$existing = Get-NetFirewallRule -Name $rule.Name -ErrorAction SilentlyContinue
|
|
if ($existing) {
|
|
$port = $existing | Get-NetFirewallPortFilter
|
|
$address = $existing | Get-NetFirewallAddressFilter
|
|
$valid = $existing.Enabled -eq 'True' -and $existing.Action -eq 'Allow' -and
|
|
$existing.Direction -eq 'Inbound' -and $existing.Profile -eq 'Any' -and
|
|
$port.Protocol -eq $rule.Protocol -and [string]$port.LocalPort -eq $rule.Port -and
|
|
@($address.RemoteAddress).Count -eq 1 -and $address.RemoteAddress -eq 'LocalSubnet'
|
|
if ($valid) { continue }
|
|
$existing | Remove-NetFirewallRule
|
|
}
|
|
New-NetFirewallRule -Name $rule.Name -DisplayName $rule.Name -Enabled True `
|
|
-Direction Inbound -Action Allow -Profile Any -Protocol $rule.Protocol `
|
|
-LocalPort $rule.Port -RemoteAddress LocalSubnet | Out-Null
|
|
}
|
|
|
|
# WinRM's administrative-template policy creates the listener. This also
|
|
# restores the Windows PowerShell session endpoint when it was disabled.
|
|
$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) {
|
|
Enable-PSRemoting -Force -SkipNetworkProfileCheck | Out-Null
|
|
}
|
|
|
|
# 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]
|
|
try {
|
|
$principalSid = ([Security.Principal.NTAccount]::new($RemoteDesktopPrincipal)).Translate(
|
|
[Security.Principal.SecurityIdentifier]).Value
|
|
$group = [ADSI]("WinNT://$env:COMPUTERNAME/$groupName,group")
|
|
$memberSids = @($group.psbase.Invoke('Members') | ForEach-Object {
|
|
try {
|
|
$bytes = $_.GetType().InvokeMember('objectSid',
|
|
[Reflection.BindingFlags]::GetProperty, $null, $_, $null)
|
|
if ($bytes) { ([Security.Principal.SecurityIdentifier]::new([byte[]]$bytes, 0)).Value }
|
|
}
|
|
catch { }
|
|
})
|
|
if ($memberSids -notcontains $principalSid) {
|
|
Add-LocalGroupMember -Group $groupName -Member $principalSid
|
|
}
|
|
}
|
|
catch {
|
|
# A temporarily unavailable DC must not prevent RDP and WinRM from starting.
|
|
Write-Warning "Remote Desktop group membership could not be verified: $($_.Exception.Message)"
|
|
}
|