101 lines
4.7 KiB
PowerShell
101 lines
4.7 KiB
PowerShell
[CmdletBinding(SupportsShouldProcess)]
|
|
param(
|
|
[string]$TargetOuDn = 'OU=Laboratorio,DC=lci,DC=lasalle,DC=mx',
|
|
[string]$GpoName = 'SGU - Windows client experience',
|
|
[string]$DomainController = $env:COMPUTERNAME
|
|
)
|
|
|
|
$ErrorActionPreference = 'Stop'
|
|
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
|
|
if (-not ([Security.Principal.WindowsPrincipal]::new($identity)).IsInRole(
|
|
[Security.Principal.WindowsBuiltInRole]::Administrator)) {
|
|
throw 'Run this script elevated on the domain controller.'
|
|
}
|
|
Import-Module ActiveDirectory -ErrorAction Stop
|
|
Import-Module GroupPolicy -ErrorAction Stop
|
|
|
|
$targetOu = Get-ADOrganizationalUnit -Identity $TargetOuDn -Server $DomainController
|
|
$domain = Get-ADDomain -Server $DomainController
|
|
if (-not $targetOu.DistinguishedName.EndsWith($domain.DistinguishedName,
|
|
[StringComparison]::OrdinalIgnoreCase)) {
|
|
throw 'The target OU is outside the domain.'
|
|
}
|
|
$domainName = $domain.DNSRoot
|
|
$gpo = Get-GPO -Name $GpoName -Domain $domainName -Server $DomainController -ErrorAction Stop
|
|
$links = @(Get-GPInheritance -Target $TargetOuDn -Domain $domainName -Server $DomainController).GpoLinks
|
|
if (-not @($links | Where-Object { $_.DisplayName -eq $GpoName -and
|
|
[string]$_.Enabled -in @('True','Yes') }).Count) {
|
|
throw "The existing GPO '$GpoName' must be linked and enabled on $TargetOuDn."
|
|
}
|
|
|
|
# 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"
|
|
$scriptsIniPath = Join-Path $startupRoot 'scripts.ini'
|
|
$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 (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 `
|
|
-Properties gPCMachineExtensionNames,versionNumber
|
|
$extension = '[{42B5FAAE-6536-11D2-AE5A-0000F87571E3}{40B6664F-4972-11D1-A7CA-0000F87571E3}]'
|
|
$names = [string]$adGpo.gPCMachineExtensionNames
|
|
if ($names.IndexOf($extension, [StringComparison]::OrdinalIgnoreCase) -lt 0) {
|
|
Set-ADObject -Identity $gpoDn -Server $DomainController `
|
|
-Replace @{gPCMachineExtensionNames = "$names$extension"}
|
|
}
|
|
$gptPath = Join-Path (Split-Path (Split-Path $startupRoot -Parent) -Parent) 'GPT.ini'
|
|
$gpt = [IO.File]::ReadAllText($gptPath)
|
|
$gptVersion = [int]([regex]::Match($gpt, '(?m)^Version=(\d+)\r?$').Groups[1].Value)
|
|
$baseVersion = [Math]::Max($gptVersion, [int]$adGpo.versionNumber)
|
|
$nextVersion = ($baseVersion -band -65536) -bor (($baseVersion + 1) -band 65535)
|
|
[IO.File]::WriteAllText($gptPath,
|
|
[regex]::Replace($gpt, '(?m)^Version=\d+\r?$', "Version=$nextVersion"),
|
|
[Text.Encoding]::ASCII)
|
|
Set-ADObject -Identity $gpoDn -Server $DomainController -Replace @{versionNumber = $nextVersion}
|
|
}
|
|
|
|
[pscustomobject]@{
|
|
GpoName = $GpoName
|
|
GpoId = $gpo.Id
|
|
TargetOu = $TargetOuDn
|
|
StartupScripts = @($startupScripts.Path)
|
|
Changed = $scriptChanged -or $iniChanged
|
|
}
|