[CmdletBinding()] param( [string]$DomainController = $env:COMPUTERNAME, [string]$TargetOuDn = 'OU=Centro de Experiencia Digital,OU=Laboratorio,DC=lci,DC=lasalle,DC=mx', [string]$GpoName = 'CED - Confianza de administracion', [string[]]$DnsNames = @('sgu-dc02.lci.lasalle.mx','sgu-dc02'), [string]$ExportPath = 'C:\ProgramData\LCI-Admin\CED-WAC.cer' ) $ErrorActionPreference = 'Stop' Import-Module ActiveDirectory Import-Module GroupPolicy Import-Module 'C:\Program Files\WindowsAdminCenter\PowerShellModules\Microsoft.WindowsAdminCenter.Configuration\Microsoft.WindowsAdminCenter.Configuration.psm1' $domain = Get-ADDomain -Server $DomainController Get-ADOrganizationalUnit -Identity $TargetOuDn -Server $DomainController | Out-Null $subject = 'CN=CED-WAC-SGU-DC02' $cert = Get-ChildItem Cert:\LocalMachine\My | Where-Object { $_.Subject -eq $subject -and $_.HasPrivateKey -and $_.NotAfter -gt (Get-Date).AddMonths(3) } | Sort-Object NotAfter -Descending | Select-Object -First 1 if (-not $cert) { $cert = New-SelfSignedCertificate -Type SSLServerAuthentication -Subject $subject -DnsName $DnsNames -CertStoreLocation Cert:\LocalMachine\My -Provider 'Microsoft RSA SChannel Cryptographic Provider' -KeyAlgorithm RSA -KeyLength 3072 -KeySpec KeyExchange -KeyExportPolicy NonExportable -HashAlgorithm SHA256 -NotAfter (Get-Date).AddYears(2) -FriendlyName 'CED Windows Admin Center HTTPS' } New-Item -ItemType Directory -Path (Split-Path $ExportPath -Parent) -Force | Out-Null Export-Certificate -Cert $cert -FilePath $ExportPath -Force | Out-Null Import-Certificate -FilePath $ExportPath -CertStoreLocation Cert:\LocalMachine\Root | Out-Null $blob = Get-ItemPropertyValue -LiteralPath "HKLM:\SOFTWARE\Microsoft\SystemCertificates\Root\Certificates\$($cert.Thumbprint)" -Name Blob $gpo = Get-GPO -Name $GpoName -Domain $domain.DNSRoot -Server $DomainController -ErrorAction SilentlyContinue if (-not $gpo) { $gpo = New-GPO -Name $GpoName -Domain $domain.DNSRoot -Server $DomainController } $gpo.GpoStatus = [Microsoft.GroupPolicy.GpoStatus]::UserSettingsDisabled Set-GPRegistryValue -Guid $gpo.Id -Domain $domain.DNSRoot -Server $DomainController -Key "HKLM\Software\Policies\Microsoft\SystemCertificates\Root\Certificates\$($cert.Thumbprint)" -ValueName Blob -Type Binary -Value $blob | Out-Null $links = (Get-GPInheritance -Target $TargetOuDn -Domain $domain.DNSRoot -Server $DomainController).GpoLinks if ($gpo.Id -notin $links.GpoId) { New-GPLink -Guid $gpo.Id -Target $TargetOuDn -Domain $domain.DNSRoot -Server $DomainController | Out-Null } $keyPath = Join-Path "$env:ProgramData\Microsoft\Crypto\RSA\MachineKeys" $cert.PrivateKey.CspKeyContainerInfo.UniqueKeyContainerName $acl = Get-Acl -LiteralPath $keyPath $sid = [Security.Principal.SecurityIdentifier]::new('S-1-5-20') $acl.AddAccessRule([Security.AccessControl.FileSystemAccessRule]::new($sid,'Read','Allow')) Set-Acl -LiteralPath $keyPath -AclObject $acl $settings = 'C:\Program Files\WindowsAdminCenter\Service\appsettings.json' $old = (Get-Content -LiteralPath $settings -Raw | ConvertFrom-Json).Kestrel.Endpoints.WindowsAdminCenter.Certificate.Subject if ($old -ne 'CED-WAC-SGU-DC02') { Copy-Item -LiteralPath $settings -Destination (Join-Path (Split-Path $ExportPath -Parent) "WAC-before-CED-$((Get-Date).ToString('yyyyMMddHHmmss')).json") Set-WACCertificateSubjectName -Thumbprint $cert.Thumbprint -Target FrontEnd Restart-WACService } [pscustomobject]@{Gpo=$GpoName;TargetOu=$TargetOuDn;Thumbprint=$cert.Thumbprint;DnsNames=@($cert.DnsNameList.Unicode);Expires=$cert.NotAfter;PublicCertificate=$ExportPath;PreviousSubject=$old}