Grant domain Builtin administrators local rights on lab workstations
This commit is contained in:
1 parent
77091c023d
commit
738df9ceea
6 files changed
+180
-13
No files matched your search
@@ -0,0 +1,105 @@
|
||||
[CmdletBinding()]
|
||||
param()
|
||||
|
||||
$ErrorActionPreference = 'Stop'
|
||||
$computer = Get-CimInstance Win32_ComputerSystem
|
||||
if (-not $computer.PartOfDomain -or $computer.DomainRole -ge 4) { return }
|
||||
|
||||
# The domain's Builtin\Administrators group is not the local Administrators
|
||||
# group on a member workstation, even though both have SID S-1-5-32-544.
|
||||
# Resolve its *users* through LDAP, then grant their domain SIDs locally.
|
||||
# The directory lookup must succeed before any local membership is changed.
|
||||
$domainDn = 'DC=' + (($computer.Domain -split '\.') -join ',DC=')
|
||||
$domainAdministratorsDn = "CN=Administrators,CN=Builtin,$domainDn"
|
||||
$directoryRoot = [DirectoryServices.DirectoryEntry]::new("LDAP://$domainDn")
|
||||
$searcher = [DirectoryServices.DirectorySearcher]::new($directoryRoot)
|
||||
$searcher.Filter = "(&(objectCategory=person)(objectClass=user)(memberOf:1.2.840.113556.1.4.1941:=$domainAdministratorsDn))"
|
||||
$searcher.PageSize = 500
|
||||
$searcher.ClientTimeout = [TimeSpan]::FromSeconds(5)
|
||||
$searcher.ServerTimeLimit = [TimeSpan]::FromSeconds(5)
|
||||
[void]$searcher.PropertiesToLoad.Add('objectSid')
|
||||
|
||||
try {
|
||||
$results = $searcher.FindAll()
|
||||
try {
|
||||
$desiredSids = @(
|
||||
foreach ($result in $results) {
|
||||
$sidBytes = [byte[]]$result.Properties['objectsid'][0]
|
||||
if ($sidBytes.Length -gt 0) {
|
||||
[Security.Principal.SecurityIdentifier]::new($sidBytes, 0).Value
|
||||
}
|
||||
}
|
||||
) | Sort-Object -Unique
|
||||
}
|
||||
finally { $results.Dispose() }
|
||||
}
|
||||
catch {
|
||||
Write-Warning "Cannot read domain Administrators; local membership was left unchanged: $($_.Exception.Message)"
|
||||
return
|
||||
}
|
||||
finally {
|
||||
$searcher.Dispose()
|
||||
$directoryRoot.Dispose()
|
||||
}
|
||||
|
||||
$localAdministratorsSid = [Security.Principal.SecurityIdentifier]::new('S-1-5-32-544')
|
||||
$localAdministrators = Get-LocalGroup -SID $localAdministratorsSid
|
||||
$group = [ADSI]("WinNT://$env:COMPUTERNAME/$($localAdministrators.Name),group")
|
||||
function Get-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 { }
|
||||
})
|
||||
}
|
||||
|
||||
$statePath = 'HKLM:\SOFTWARE\SGU\WorkstationAdministrators'
|
||||
$managedSids = if (Test-Path -LiteralPath $statePath) {
|
||||
@((Get-ItemProperty -LiteralPath $statePath -Name ManagedSids -ErrorAction SilentlyContinue).ManagedSids)
|
||||
} else { @() }
|
||||
$managedSids = @($managedSids | Where-Object { $_ -match '^S-1-5-21-' } | Sort-Object -Unique)
|
||||
$presentSids = @(Get-MemberSids)
|
||||
|
||||
foreach ($sid in $desiredSids) {
|
||||
if ($presentSids -notcontains $sid) {
|
||||
Add-LocalGroupMember -Group $localAdministrators.Name -Member $sid
|
||||
$presentSids += $sid
|
||||
$managedSids += $sid
|
||||
}
|
||||
}
|
||||
|
||||
# Remove only entries previously added by this policy. Other local or domain
|
||||
# administrators remain untouched if they have a separate authorization path.
|
||||
foreach ($sid in @($managedSids | Where-Object { $desiredSids -notcontains $_ })) {
|
||||
if ($presentSids -contains $sid) {
|
||||
Remove-LocalGroupMember -Group $localAdministrators.Name -Member $sid
|
||||
}
|
||||
}
|
||||
|
||||
New-Item -Path $statePath -Force | Out-Null
|
||||
$remainingManagedSids = @($managedSids |
|
||||
Where-Object { $desiredSids -contains $_ } | Sort-Object -Unique)
|
||||
if ($remainingManagedSids.Count -gt 0) {
|
||||
$state = @{
|
||||
Path = $statePath
|
||||
Name = 'ManagedSids'
|
||||
PropertyType = 'MultiString'
|
||||
Value = $remainingManagedSids
|
||||
Force = $true
|
||||
}
|
||||
New-ItemProperty @state | Out-Null
|
||||
}
|
||||
else {
|
||||
Remove-ItemProperty -Path $statePath -Name ManagedSids -ErrorAction SilentlyContinue
|
||||
}
|
||||
|
||||
$verifiedSids = @(Get-MemberSids)
|
||||
$missingSids = @($desiredSids | Where-Object { $verifiedSids -notcontains $_ })
|
||||
if ($missingSids.Count -gt 0) {
|
||||
throw "Domain Administrators were not added to local Administrators: $($missingSids -join ', ')"
|
||||
}
|
||||
@@ -520,6 +520,7 @@ foreach ($requiredPath in @(
|
||||
(Join-Path $scriptsRoot 'Set-LabBrokerDns.ps1'),
|
||||
(Join-Path $scriptsRoot 'Set-SguDomainComputerPolicies.ps1'),
|
||||
(Join-Path $scriptsRoot 'Enable-SguWorkstationRemoteAccess.ps1'),
|
||||
(Join-Path $scriptsRoot 'Enable-SguWorkstationAdministrators.ps1'),
|
||||
(Join-Path $scriptsRoot 'Install-SguWorkstationRemoteAccessPolicy.ps1'),
|
||||
(Join-Path $scriptsRoot 'Set-SguDomainUserPolicies.ps1'),
|
||||
(Join-Path $scriptsRoot 'Set-SguLaboratorioWallpaperPolicy.ps1'),
|
||||
|
||||
@@ -28,25 +28,48 @@ if (-not @($links | Where-Object { $_.DisplayName -eq $GpoName -and
|
||||
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
|
||||
# The existing machine GPO also manages the credential tile. These startup
|
||||
# scripts are a native Group Policy extension, not scheduled client tasks.
|
||||
$scriptNames = @(
|
||||
'Enable-SguWorkstationRemoteAccess.ps1',
|
||||
'Enable-SguWorkstationAdministrators.ps1'
|
||||
)
|
||||
$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
|
||||
$startupScripts = @(
|
||||
foreach ($scriptName in $scriptNames) {
|
||||
$sourcePath = Join-Path $PSScriptRoot $scriptName
|
||||
$startupPath = Join-Path $startupRoot "Startup\$scriptName"
|
||||
$source = [IO.File]::ReadAllText($sourcePath)
|
||||
[pscustomobject]@{
|
||||
Name = $scriptName
|
||||
Path = $startupPath
|
||||
Source = $source
|
||||
Changed = -not (Test-Path -LiteralPath $startupPath) -or
|
||||
[IO.File]::ReadAllText($startupPath) -cne $source
|
||||
}
|
||||
}
|
||||
)
|
||||
$iniLines = @('[Startup]')
|
||||
for ($index = 0; $index -lt $scriptNames.Count; $index++) {
|
||||
$unc = "\\$domainName\SYSVOL\$domainName\Policies\{$($gpo.Id)}\Machine\Scripts\Startup\$($scriptNames[$index])"
|
||||
$iniLines += "${index}CmdLine=powershell.exe"
|
||||
$iniLines += "${index}Parameters=-NoProfile -NonInteractive -ExecutionPolicy Bypass -File `"$unc`""
|
||||
}
|
||||
$ini = ($iniLines -join "`r`n") + "`r`n"
|
||||
$scriptChanged = @($startupScripts | Where-Object Changed).Count -gt 0
|
||||
$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))
|
||||
New-Item -ItemType Directory -Path (Join-Path $startupRoot 'Startup') -Force | Out-Null
|
||||
foreach ($startupScript in $startupScripts) {
|
||||
if ($startupScript.Changed) {
|
||||
[IO.File]::WriteAllText($startupScript.Path, $startupScript.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 `
|
||||
@@ -72,6 +95,6 @@ if (($scriptChanged -or $iniChanged) -and
|
||||
GpoName = $GpoName
|
||||
GpoId = $gpo.Id
|
||||
TargetOu = $TargetOuDn
|
||||
StartupScript = $startupPath
|
||||
StartupScripts = @($startupScripts.Path)
|
||||
Changed = $scriptChanged -or $iniChanged
|
||||
}
|
||||
@@ -211,6 +211,7 @@ $serverScripts = @(
|
||||
'Enable-SguAzureUserRoaming.ps1',
|
||||
'Enable-SguServerRemoteManagement.ps1',
|
||||
'Enable-SguWorkstationRemoteAccess.ps1',
|
||||
'Enable-SguWorkstationAdministrators.ps1',
|
||||
'Get-SguUsageReport.ps1',
|
||||
'Get-SguBrokerLog.ps1',
|
||||
'Install-SguDomainMonitoring.ps1',
|
||||
|
||||
Reference in new issue
Block a user