175 lines
8.6 KiB
PowerShell
175 lines
8.6 KiB
PowerShell
[CmdletBinding(SupportsShouldProcess)]
|
|
param(
|
|
[string]$Version = '0.6.15',
|
|
|
|
[ValidatePattern('^https://')]
|
|
[string]$ClientPackageUri = 'https://gitea.lci.ulsa.mx/alexrg/SGU-CredentialProvider/releases/download/v0.6.15/sgu-windows-client-bootstrap-0.6.15.zip',
|
|
|
|
[ValidatePattern('^[0-9A-Fa-f]{64}$')]
|
|
[string]$ClientPackageSha256 = 'A5684FD1C196672F005517EB8E21D345BB0707091B59E203627995FF88001A11',
|
|
|
|
[string]$GpoName = 'SGU - Credential Provider update',
|
|
|
|
[string]$DomainController = $env:COMPUTERNAME
|
|
)
|
|
|
|
$ErrorActionPreference = 'Stop'
|
|
|
|
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
|
|
$principal = [Security.Principal.WindowsPrincipal]::new($identity)
|
|
if (-not $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
|
|
throw 'Run this script from an elevated Windows PowerShell session on a domain controller or management host.'
|
|
}
|
|
|
|
Import-Module ActiveDirectory -ErrorAction Stop
|
|
Import-Module GroupPolicy -ErrorAction Stop
|
|
|
|
$domain = Get-ADDomain -Server $DomainController -ErrorAction Stop
|
|
$domainDnsRoot = $domain.DNSRoot
|
|
$domainDn = $domain.DistinguishedName
|
|
$gpo = Get-GPO -Name $GpoName -Domain $domainDnsRoot -Server $DomainController -ErrorAction SilentlyContinue
|
|
if (-not $gpo -and $PSCmdlet.ShouldProcess($GpoName, 'Create computer startup update policy')) {
|
|
$gpo = New-GPO -Name $GpoName -Domain $domainDnsRoot -Server $DomainController
|
|
}
|
|
if (-not $gpo) {
|
|
throw "The GPO '$GpoName' does not exist and was not created."
|
|
}
|
|
|
|
$existingLink = @(Get-GPInheritance -Target $domainDn -Domain $domainDnsRoot -Server $DomainController).GpoLinks |
|
|
Where-Object DisplayName -eq $GpoName |
|
|
Select-Object -First 1
|
|
if (-not $existingLink -and
|
|
$PSCmdlet.ShouldProcess($domainDn, "Link and enable '$GpoName'")) {
|
|
New-GPLink -Name $GpoName -Target $domainDn -Domain $domainDnsRoot -Server $DomainController -LinkEnabled Yes | Out-Null
|
|
}
|
|
elseif ($existingLink -and
|
|
$existingLink.Enabled -notin @($true, 'Yes') -and
|
|
$PSCmdlet.ShouldProcess($domainDn, "Enable '$GpoName'")) {
|
|
Set-GPLink -Name $GpoName -Target $domainDn -Domain $domainDnsRoot -Server $DomainController -LinkEnabled Yes | Out-Null
|
|
}
|
|
|
|
$systemPolicyKey = 'HKLM\Software\Policies\Microsoft\Windows\System'
|
|
$winlogonPolicyKey = 'HKLM\Software\Policies\Microsoft\Windows NT\CurrentVersion\Winlogon'
|
|
if ($PSCmdlet.ShouldProcess($GpoName, 'Make the computer startup script synchronous')) {
|
|
Set-GPRegistryValue -Name $GpoName -Domain $domainDnsRoot -Server $DomainController `
|
|
-Key $systemPolicyKey -ValueName 'RunStartupScriptSync' -Type DWord -Value 1
|
|
Set-GPRegistryValue -Name $GpoName -Domain $domainDnsRoot -Server $DomainController `
|
|
-Key $winlogonPolicyKey -ValueName 'SyncForegroundPolicy' -Type DWord -Value 1
|
|
}
|
|
|
|
$scriptName = "Update-SguCredentialProvider-$Version.ps1"
|
|
$gpoScriptsPath = Join-Path $env:WINDIR "SYSVOL\domain\Policies\{$($gpo.Id)}\Machine\Scripts"
|
|
$startupPath = Join-Path $gpoScriptsPath 'Startup'
|
|
$powershellScriptsIniPath = Join-Path $gpoScriptsPath 'psscripts.ini'
|
|
$scriptPath = Join-Path $startupPath $scriptName
|
|
|
|
$startupScript = @"
|
|
`$ErrorActionPreference = 'Stop'
|
|
`$version = '$Version'
|
|
`$packageUri = '$ClientPackageUri'
|
|
`$expectedSha256 = '$($ClientPackageSha256.ToUpperInvariant())'
|
|
|
|
`$computerSystem = Get-CimInstance Win32_ComputerSystem
|
|
if (-not `$computerSystem.PartOfDomain -or `$computerSystem.DomainRole -ge 4) {
|
|
exit 0
|
|
}
|
|
|
|
`$updateRoot = Join-Path `$env:ProgramData "SGU\CredentialProviderUpdates\`$version"
|
|
`$markerPath = Join-Path `$updateRoot 'complete.marker'
|
|
if (Test-Path -LiteralPath `$markerPath -PathType Leaf) {
|
|
exit 0
|
|
}
|
|
|
|
New-Item -ItemType Directory -Path `$updateRoot -Force | Out-Null
|
|
`$archivePath = Join-Path `$updateRoot 'client-bootstrap.zip'
|
|
`$stagePath = Join-Path `$updateRoot 'package'
|
|
if ((Test-Path -LiteralPath `$archivePath -PathType Leaf) -and
|
|
(Get-FileHash -LiteralPath `$archivePath -Algorithm SHA256).Hash -ne `$expectedSha256) {
|
|
Remove-Item -LiteralPath `$archivePath -Force
|
|
}
|
|
if (-not (Test-Path -LiteralPath `$archivePath -PathType Leaf)) {
|
|
Invoke-WebRequest -Uri `$packageUri -OutFile `$archivePath -UseBasicParsing -TimeoutSec 600
|
|
}
|
|
if ((Get-FileHash -LiteralPath `$archivePath -Algorithm SHA256).Hash -ne `$expectedSha256) {
|
|
throw 'The SGU Credential Provider update package did not match its expected SHA-256.'
|
|
}
|
|
|
|
`$installScript = Join-Path `$stagePath 'payload\scripts\Install-CredentialProvider.ps1'
|
|
if (-not (Test-Path -LiteralPath `$installScript -PathType Leaf)) {
|
|
if (Test-Path -LiteralPath `$stagePath -PathType Container) {
|
|
Remove-Item -LiteralPath `$stagePath -Recurse -Force
|
|
}
|
|
Expand-Archive -LiteralPath `$archivePath -DestinationPath `$stagePath -Force
|
|
}
|
|
if (-not (Test-Path -LiteralPath `$installScript -PathType Leaf)) {
|
|
throw 'The verified update package did not contain Install-CredentialProvider.ps1.'
|
|
}
|
|
|
|
`$settingsPath = Join-Path `$env:ProgramData 'SGU\CredentialProvider\settings.json'
|
|
`$settings = Get-Content -LiteralPath `$settingsPath -Raw | ConvertFrom-Json
|
|
`$installParameters = @{
|
|
PublishPath = (Join-Path `$stagePath 'payload\credential-provider')
|
|
BrokerEndpoint = [string]`$settings.BrokerEndpoint
|
|
ClientCertificateThumbprint = [string]`$settings.ClientCertificateThumbprint
|
|
ServerCertificateThumbprint = [string]`$settings.ServerCertificateThumbprint
|
|
DomainNetbios = [string]`$settings.DomainNetbios
|
|
TimeoutSeconds = [int]`$settings.TimeoutSeconds
|
|
}
|
|
`$dotnet = Get-Command dotnet -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Source -ErrorAction SilentlyContinue
|
|
`$runtimeInstalled = `$dotnet -and (& `$dotnet --list-runtimes | Select-String '^Microsoft\.NETCore\.App 10\.')
|
|
if (-not `$runtimeInstalled) {
|
|
`$runtimeInstaller = Get-ChildItem -LiteralPath (Join-Path `$stagePath 'payload\prerequisites') -Filter '*x64*.exe' -File |
|
|
Select-Object -First 1
|
|
if (-not `$runtimeInstaller) {
|
|
throw 'The update package does not contain the required .NET x64 runtime installer.'
|
|
}
|
|
`$installParameters.InstallDotNetRuntime = `$true
|
|
`$installParameters.DotNetRuntimeInstallerPath = `$runtimeInstaller.FullName
|
|
}
|
|
|
|
& `$installScript @installParameters | Out-Null
|
|
[IO.File]::WriteAllText(`$markerPath, (Get-Date).ToUniversalTime().ToString('o'), [Text.UTF8Encoding]::new(`$false))
|
|
"@
|
|
|
|
if ($PSCmdlet.ShouldProcess($startupPath, "Install '$scriptName' as a computer startup script")) {
|
|
New-Item -ItemType Directory -Path $startupPath -Force | Out-Null
|
|
[IO.File]::WriteAllText($scriptPath, $startupScript, [Text.UTF8Encoding]::new($false))
|
|
$scriptsIni = "[Startup]`r`n0CmdLine=$scriptName`r`n0Parameters=`r`n"
|
|
# Windows' Scripts CSE requires UTF-16LE with a BOM for psscripts.ini.
|
|
[IO.File]::WriteAllText($powershellScriptsIniPath, $scriptsIni, [Text.Encoding]::Unicode)
|
|
|
|
# Writing SYSVOL directly does not register the Scripts CSE in the GPO's
|
|
# Active Directory object. Without it, gpresult lists the GPO but the
|
|
# client never executes its startup script.
|
|
$gpoDn = "CN={$($gpo.Id)},CN=Policies,CN=System,$domainDn"
|
|
$gpoObject = Get-ADObject -Identity $gpoDn -Server $DomainController `
|
|
-Properties gPCMachineExtensionNames,versionNumber
|
|
$scriptExtension = '[{42B5FAAE-6536-11D2-AE5A-0000F87571E3}{40B6664F-4972-11D1-A7CA-0000F87571E3}]'
|
|
$extensionNames = [string]$gpoObject.gPCMachineExtensionNames
|
|
if ($extensionNames.IndexOf($scriptExtension, [StringComparison]::OrdinalIgnoreCase) -lt 0) {
|
|
Set-ADObject -Identity $gpoDn -Server $DomainController `
|
|
-Replace @{gPCMachineExtensionNames = "$extensionNames$scriptExtension"}
|
|
}
|
|
|
|
# Keep the AD and SYSVOL computer versions synchronized and force clients
|
|
# to process this corrected script registration on their next refresh.
|
|
$gptIniPath = Join-Path (Split-Path (Split-Path $gpoScriptsPath -Parent) -Parent) 'GPT.ini'
|
|
$gptIni = Get-Content -LiteralPath $gptIniPath -Raw
|
|
$gptVersion = [int]([regex]::Match($gptIni, '(?m)^Version=(\d+)\r?$').Groups[1].Value)
|
|
$adVersion = [int]$gpoObject.versionNumber
|
|
$baseVersion = [Math]::Max($gptVersion, $adVersion)
|
|
$nextVersion = ($baseVersion -band -65536) -bor (($baseVersion + 1) -band 65535)
|
|
$updatedGptIni = [regex]::Replace($gptIni, '(?m)^Version=\d+\r?$', "Version=$nextVersion")
|
|
[IO.File]::WriteAllText($gptIniPath, $updatedGptIni, [Text.Encoding]::ASCII)
|
|
Set-ADObject -Identity $gpoDn -Server $DomainController -Replace @{versionNumber = $nextVersion}
|
|
}
|
|
|
|
[pscustomobject]@{
|
|
GpoName = $gpo.DisplayName
|
|
GpoId = $gpo.Id
|
|
LinkedTarget = $domainDn
|
|
StartupScript = $scriptPath
|
|
Version = $Version
|
|
PackageSha256 = $ClientPackageSha256.ToUpperInvariant()
|
|
}
|