Files
SGU-CredentialProvider/scripts/Invoke-SguCedClientEnrollment.ps1
T

46 lines
2.9 KiB
PowerShell

[CmdletBinding()]
param([switch]$InstallTask)
$ErrorActionPreference = 'Stop'
$directory = 'C:\ProgramData\LCI-Admin\CED-Client'
if ($InstallTask) {
New-Item $directory -ItemType Directory -Force | Out-Null
$acl = [Security.AccessControl.DirectorySecurity]::new()
$acl.SetAccessRuleProtection($true,$false)
foreach ($sid in 'S-1-5-18','S-1-5-32-544') {
$acl.AddAccessRule([Security.AccessControl.FileSystemAccessRule]::new(
[Security.Principal.SecurityIdentifier]::new($sid),'FullControl','ContainerInherit,ObjectInherit','None','Allow'))
}
Set-Acl $directory $acl
$localScript = Join-Path $directory 'Invoke-SguCedClientEnrollment.ps1'
if ($PSCommandPath -ine $localScript) { Copy-Item $PSCommandPath $localScript -Force }
$action = New-ScheduledTaskAction -Execute powershell.exe -Argument ('-NoProfile -NonInteractive -ExecutionPolicy Bypass -File "{0}"' -f $localScript)
$triggers = @((New-ScheduledTaskTrigger -AtStartup),(New-ScheduledTaskTrigger -Once -At (Get-Date).AddMinutes(1) -RepetitionInterval (New-TimeSpan -Minutes 30)))
$settings = New-ScheduledTaskSettingsSet -StartWhenAvailable -MultipleInstances IgnoreNew -ExecutionTimeLimit (New-TimeSpan -Minutes 5)
Register-ScheduledTask 'CED Configuration Manager policy enrollment' -Action $action -Trigger $triggers -Settings $settings -User SYSTEM -RunLevel Highest -Force | Out-Null
Start-ScheduledTask 'CED Configuration Manager policy enrollment'
return
}
if (Get-Service CcmExec -ErrorAction SilentlyContinue) { return }
if (Get-Process ccmsetup -ErrorAction SilentlyContinue) { return }
# Check the machine's current OU with its own domain identity. An offline DC
# defers enrollment; an existing Wi-Fi connection continues using its cached PSK.
$root = [ADSI]'LDAP://RootDSE'
$search = [DirectoryServices.DirectorySearcher]::new([ADSI]("LDAP://"+[string]$root.defaultNamingContext))
try {
$search.Filter = '(&(objectClass=computer)(sAMAccountName='+$env:COMPUTERNAME+'$))'
[void]$search.PropertiesToLoad.Add('distinguishedName')
$result = $search.FindOne()
$dn = [string]$result.Properties['distinguishedname'][0]
$ou = 'OU=Centro de Experiencia Digital,OU=Laboratorio,DC=lci,DC=lasalle,DC=mx'
if (-not $dn.EndsWith(','+$ou,[StringComparison]::OrdinalIgnoreCase)) { return }
} finally { $search.Dispose() }
$source = '\\sgu-dc02.lci.lasalle.mx\CEDClient$'
$bootstrap = Join-Path $source 'ccmsetup.exe'
$signature = Get-AuthenticodeSignature $bootstrap
if ($signature.Status -ne 'Valid' -or $signature.SignerCertificate.Subject -notlike '*Microsoft Corporation*') {
throw 'Client bootstrapper is not a valid Microsoft-signed binary.'
}
Copy-Item $bootstrap (Join-Path $directory 'ccmsetup.exe') -Force
& (Join-Path $directory 'ccmsetup.exe') /service /source:$source SMSSITECODE=LCI SMSMP=sgu-dc02.lci.lasalle.mx
# ccmsetup/service runs asynchronously. Its own log and CcmExec verify completion.