197 lines
10 KiB
PowerShell
197 lines
10 KiB
PowerShell
#Requires -Version 5.1
|
|
[CmdletBinding(SupportsShouldProcess)]
|
|
param(
|
|
[string]$TargetOuDn = 'OU=Laboratorio,DC=lci,DC=lasalle,DC=mx',
|
|
[string]$GpoName = 'SGU - Laboratorio wallpaper protection',
|
|
[string]$DomainController = $env:COMPUTERNAME,
|
|
[string]$WallpaperPath = '%LOCALAPPDATA%\SGU\Wallpapers\welcome-%COMPUTERNAME%.jpg',
|
|
[string]$LocalProfilePolicyScriptPath = (Join-Path $PSScriptRoot 'Set-SguLocalProfileSecurityPolicies.ps1')
|
|
)
|
|
|
|
$ErrorActionPreference = 'Stop'
|
|
Import-Module ActiveDirectory -ErrorAction Stop
|
|
Import-Module GroupPolicy -ErrorAction Stop
|
|
$domainName = (Get-ADDomain -Server $DomainController).DNSRoot
|
|
Get-ADOrganizationalUnit -Identity $TargetOuDn -Server $DomainController -ErrorAction Stop | Out-Null
|
|
|
|
function Set-SguLocalProfilePolicyStartupScript {
|
|
param(
|
|
[Parameter(Mandatory)]$Gpo,
|
|
[Parameter(Mandatory)][string]$DomainName,
|
|
[Parameter(Mandatory)][string]$DomainController,
|
|
[Parameter(Mandatory)][string]$SourceScript
|
|
)
|
|
|
|
if (-not (Test-Path -LiteralPath $SourceScript -PathType Leaf)) {
|
|
throw 'Set-SguLocalProfileSecurityPolicies.ps1 must be beside Set-SguLaboratorioWallpaperPolicy.ps1.'
|
|
}
|
|
|
|
$domainDn = (Get-ADDomain -Server $DomainController).DistinguishedName
|
|
$gpoAdPath = "CN={$($Gpo.Id)},CN=Policies,CN=System,$domainDn"
|
|
$gpoAd = Get-ADObject -Identity $gpoAdPath -Properties gPCFileSysPath,gPCMachineExtensionNames,versionNumber `
|
|
-Server $DomainController -ErrorAction Stop
|
|
$scriptsRoot = Join-Path $gpoAd.gPCFileSysPath 'Machine\Scripts'
|
|
$startupRoot = Join-Path $scriptsRoot 'Startup'
|
|
$destinationScript = Join-Path $startupRoot 'Set-SguLocalProfileSecurityPolicies.ps1'
|
|
$startupLauncher = Join-Path $startupRoot 'Start-SguLocalProfileSecurityPolicies.cmd'
|
|
$scriptsIniPath = Join-Path $scriptsRoot 'scripts.ini'
|
|
$legacyPowerShellScriptsIniPath = Join-Path $scriptsRoot 'psscripts.ini'
|
|
$scriptsIni = @"
|
|
[Startup]
|
|
0CmdLine=Start-SguLocalProfileSecurityPolicies.cmd
|
|
0Parameters=
|
|
"@
|
|
$normalizedScriptsIni = $scriptsIni.TrimEnd("`r", "`n")
|
|
$launcherContents = @"
|
|
@echo off
|
|
"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass -File "%~dp0Set-SguLocalProfileSecurityPolicies.ps1" -RunAsSystem
|
|
exit /b %ERRORLEVEL%
|
|
"@
|
|
$normalizedLauncherContents = $launcherContents.TrimEnd("`r", "`n")
|
|
$legacyManagedPowerShellScriptsIni = @"
|
|
[Startup]
|
|
0CmdLine=Set-SguLocalProfileSecurityPolicies.ps1
|
|
0Parameters=-RunAsSystem
|
|
"@.TrimEnd("`r", "`n")
|
|
# These are the Script CSE and its Group Policy editor snap-in. Domain
|
|
# clients ignore scripts.ini unless this CSE pair is on the GPO object.
|
|
$startupScriptExtension = '[{42B5FAAE-6536-11D2-AE5A-0000F87571E3}{40B66650-4972-11D1-A7CA-0000F87571E3}]'
|
|
# gPCMachineExtensionNames is a single concatenated AD attribute, even
|
|
# though it holds several CSE/snap-in GUID pairs.
|
|
$extensionValue = [string]($gpoAd.gPCMachineExtensionNames -join '')
|
|
$needsExtension = $extensionValue -notmatch [regex]::Escape($startupScriptExtension)
|
|
$needsScript = -not (Test-Path -LiteralPath $destinationScript -PathType Leaf)
|
|
if (-not $needsScript) {
|
|
$needsScript = (Get-FileHash -LiteralPath $SourceScript -Algorithm SHA256).Hash -ne
|
|
(Get-FileHash -LiteralPath $destinationScript -Algorithm SHA256).Hash
|
|
}
|
|
$needsIni = -not (Test-Path -LiteralPath $scriptsIniPath -PathType Leaf) -or
|
|
(Get-Content -LiteralPath $scriptsIniPath -Raw).TrimEnd("`r", "`n") -ne $normalizedScriptsIni
|
|
$needsLauncher = -not (Test-Path -LiteralPath $startupLauncher -PathType Leaf) -or
|
|
(Get-Content -LiteralPath $startupLauncher -Raw).TrimEnd("`r", "`n") -ne $normalizedLauncherContents
|
|
$removeLegacyPowerShellScriptsIni = (Test-Path -LiteralPath $legacyPowerShellScriptsIniPath -PathType Leaf) -and
|
|
(Get-Content -LiteralPath $legacyPowerShellScriptsIniPath -Raw).TrimEnd("`r", "`n") -eq $legacyManagedPowerShellScriptsIni
|
|
|
|
if ($needsScript -or $needsIni -or $needsLauncher -or $needsExtension -or $removeLegacyPowerShellScriptsIni) {
|
|
New-Item -ItemType Directory -Path $startupRoot -Force | Out-Null
|
|
Copy-Item -LiteralPath $SourceScript -Destination $destinationScript -Force
|
|
Set-Content -LiteralPath $scriptsIniPath -Value $normalizedScriptsIni -Encoding ASCII -NoNewline
|
|
Set-Content -LiteralPath $startupLauncher -Value $normalizedLauncherContents -Encoding ASCII -NoNewline
|
|
if ($removeLegacyPowerShellScriptsIni) {
|
|
Remove-Item -LiteralPath $legacyPowerShellScriptsIniPath -Force
|
|
}
|
|
if ($needsExtension) {
|
|
$extensionValue += $startupScriptExtension
|
|
}
|
|
|
|
# A GPO is replicated as both its SYSVOL file tree and its AD object.
|
|
# Bump the computer-side version only after the files are ready, then
|
|
# publish the same version in AD so clients process this new startup
|
|
# script on their next boot.
|
|
$currentVersion = [int64]$gpoAd.versionNumber
|
|
$currentComputerVersion = $currentVersion -band 0xFFFF
|
|
if ($currentComputerVersion -ge 65535) {
|
|
throw 'The laboratory GPO computer version cannot be incremented further.'
|
|
}
|
|
$nextVersion = ($currentVersion -band 0xFFFF0000) -bor ($currentComputerVersion + 1)
|
|
$gptIniPath = Join-Path $gpoAd.gPCFileSysPath 'gpt.ini'
|
|
$gptIni = Get-Content -LiteralPath $gptIniPath -Raw
|
|
$gptIni = [regex]::Replace($gptIni, '(?m)^Version=\d+\s*$', "Version=$nextVersion")
|
|
Set-Content -LiteralPath $gptIniPath -Value $gptIni -Encoding ASCII
|
|
# Set-ADObject validates a -Replace hashtable as one value type, so
|
|
# update the extension string and integer version in separate writes.
|
|
Set-ADObject -Identity $gpoAd.DistinguishedName -Server $DomainController -Replace @{
|
|
gPCMachineExtensionNames = $extensionValue
|
|
}
|
|
Set-ADObject -Identity $gpoAd.DistinguishedName -Server $DomainController -Replace @{
|
|
versionNumber = $nextVersion
|
|
}
|
|
}
|
|
|
|
$verifiedGpoAd = Get-ADObject -Identity $gpoAdPath -Properties gPCMachineExtensionNames `
|
|
-Server $DomainController -ErrorAction Stop
|
|
if ([string]($verifiedGpoAd.gPCMachineExtensionNames -join '') -notmatch [regex]::Escape($startupScriptExtension) -or
|
|
-not (Test-Path -LiteralPath $destinationScript -PathType Leaf) -or
|
|
-not (Test-Path -LiteralPath $startupLauncher -PathType Leaf) -or
|
|
(Get-Content -LiteralPath $scriptsIniPath -Raw).TrimEnd("`r", "`n") -ne $normalizedScriptsIni -or
|
|
(Get-Content -LiteralPath $startupLauncher -Raw).TrimEnd("`r", "`n") -ne $normalizedLauncherContents) {
|
|
throw 'The laboratory local-profile startup policy was not configured correctly.'
|
|
}
|
|
|
|
return [pscustomobject]@{
|
|
Configured = $true
|
|
Script = $destinationScript
|
|
RunsAs = 'LocalSystem'
|
|
}
|
|
}
|
|
|
|
if (-not $PSCmdlet.ShouldProcess($TargetOuDn, "Apply '$GpoName' with enforced loopback Merge")) {
|
|
return
|
|
}
|
|
|
|
$backupPath = $null
|
|
$gpo = Get-GPO -Name $GpoName -Domain $domainName -Server $DomainController -ErrorAction SilentlyContinue
|
|
if ($gpo) {
|
|
$backupPath = Join-Path $env:ProgramData ('SGU\PolicyBackups\Wallpaper-' + [guid]::NewGuid().ToString('N'))
|
|
New-Item -ItemType Directory -Path $backupPath -Force | Out-Null
|
|
Backup-GPO -Guid $gpo.Id -Path $backupPath -Domain $domainName -Server $DomainController | Out-Null
|
|
}
|
|
else {
|
|
$gpo = New-GPO -Name $GpoName -Domain $domainName -Server $DomainController `
|
|
-Comment 'Protects the SGU desktop wallpaper and prevents workstation locking for every user on Laboratorio computers and child OUs; loopback Merge preserves existing user policies.'
|
|
}
|
|
# These are user policies scoped by the computer OU, not by the user OU. The
|
|
# enforced loopback GPO therefore applies to local accounts such as .\ing as
|
|
# well as domain accounts that log on to a managed laboratory machine.
|
|
# Match the per-user/per-computer output of Set-SguWelcomeWallpaper.ps1.
|
|
$settings = @(
|
|
@{ Key = 'HKLM\Software\Policies\Microsoft\Windows\System'; Name = 'UserPolicyMode'; Type = 'DWord'; Value = 1 },
|
|
@{ Key = 'HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\ActiveDesktop'; Name = 'NoChangingWallPaper'; Type = 'DWord'; Value = 1 },
|
|
@{ Key = 'HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\System'; Name = 'DisableLockWorkstation'; Type = 'DWord'; Value = 1 },
|
|
@{ Key = 'HKCU\Software\Policies\Microsoft\Windows\Control Panel\Desktop'; Name = 'ScreenSaveActive'; Type = 'String'; Value = '0' },
|
|
@{ Key = 'HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\System'; Name = 'Wallpaper'; Type = 'ExpandString'; Value = $WallpaperPath },
|
|
@{ Key = 'HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\System'; Name = 'WallpaperStyle'; Type = 'String'; Value = '10' }
|
|
)
|
|
foreach ($setting in $settings) {
|
|
Set-GPRegistryValue -Guid $gpo.Id -Domain $domainName -Server $DomainController `
|
|
-Key $setting.Key -ValueName $setting.Name -Type $setting.Type -Value $setting.Value | Out-Null
|
|
}
|
|
$linkParameters = @{
|
|
Guid = $gpo.Id
|
|
Target = $TargetOuDn
|
|
Domain = $domainName
|
|
Server = $DomainController
|
|
LinkEnabled = 'Yes'
|
|
Enforced = 'Yes'
|
|
Order = 1
|
|
}
|
|
$existingLink = (Get-GPInheritance -Target $TargetOuDn -Domain $domainName -Server $DomainController).GpoLinks |
|
|
Where-Object GpoId -eq $gpo.Id
|
|
if ($existingLink) {
|
|
Set-GPLink @linkParameters | Out-Null
|
|
}
|
|
else {
|
|
New-GPLink @linkParameters | Out-Null
|
|
}
|
|
foreach ($setting in $settings) {
|
|
$actual = Get-GPRegistryValue -Guid $gpo.Id -Domain $domainName -Server $DomainController `
|
|
-Key $setting.Key -ValueName $setting.Name
|
|
if ([string]$actual.Value -ne [string]$setting.Value -or [string]$actual.Type -ne $setting.Type) {
|
|
throw "Wallpaper policy verification failed for $($setting.Name)."
|
|
}
|
|
}
|
|
$startupPolicy = Set-SguLocalProfilePolicyStartupScript -Gpo $gpo -DomainName $domainName `
|
|
-DomainController $DomainController -SourceScript $LocalProfilePolicyScriptPath
|
|
[pscustomobject]@{
|
|
Name = $gpo.DisplayName
|
|
Id = $gpo.Id
|
|
TargetOuDn = $TargetOuDn
|
|
WallpaperPath = $WallpaperPath
|
|
Loopback = 'Merge'
|
|
DisableLockWorkstationForAllUsers = $true
|
|
LocalProfileStartupPolicy = $startupPolicy.Configured
|
|
LocalProfileStartupScript = $startupPolicy.Script
|
|
Enforced = $true
|
|
BackupPath = $backupPath
|
|
}
|