76 lines
6.6 KiB
PowerShell
76 lines
6.6 KiB
PowerShell
[CmdletBinding()]
|
|
param()
|
|
$ErrorActionPreference = 'Stop'
|
|
# Native WLAN AutoConfig holds the machine profile before interactive logon.
|
|
$computer=Get-CimInstance Win32_ComputerSystem
|
|
if(-not $computer.PartOfDomain -or $computer.DomainRole -ne 1 -or $env:COMPUTERNAME -notmatch '^(AF|SD|SI)-'){return}
|
|
$destination = 'C:\ProgramData\LCI-Admin\CED-WiFi'
|
|
New-Item $destination -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'))
|
|
}
|
|
if((Get-Acl -LiteralPath $destination).GetSecurityDescriptorSddlForm([Security.AccessControl.AccessControlSections]::Access) -ne $acl.GetSecurityDescriptorSddlForm([Security.AccessControl.AccessControlSections]::Access)){Set-Acl -LiteralPath $destination -AclObject $acl}
|
|
foreach($name in 'ced-network.json','Set-SguCedClientNetwork.ps1'){
|
|
$source=Join-Path $PSScriptRoot $name
|
|
$target=Join-Path $destination $name
|
|
if($source -ine $target -and (-not(Test-Path -LiteralPath $target) -or (Get-FileHash -LiteralPath $source).Hash -ne (Get-FileHash -LiteralPath $target).Hash)){Copy-Item -LiteralPath $source -Destination $target -Force}
|
|
}
|
|
$networkConfiguration=[IO.File]::ReadAllText((Join-Path $destination 'ced-network.json'))|ConvertFrom-Json
|
|
$primaryDomainController=[string]$networkConfiguration.PrimaryController
|
|
& (Join-Path $destination 'Set-SguCedClientNetwork.ps1')|Out-Null
|
|
$source=Join-Path $PSScriptRoot 'Repair-SguCedWifi.ps1'
|
|
$helper=Join-Path $destination 'Repair-SguCedWifi.ps1'
|
|
if($source -ine $helper -and (-not(Test-Path -LiteralPath $helper) -or (Get-FileHash -LiteralPath $source).Hash -ne (Get-FileHash -LiteralPath $helper).Hash)){Copy-Item -LiteralPath $source -Destination $helper -Force}
|
|
$service=Get-Service WlanSvc
|
|
if($service.StartType -ne 'Automatic'){Set-Service WlanSvc -StartupType Automatic}
|
|
if($service.Status -ne 'Running'){Start-Service WlanSvc}
|
|
$subscription='<QueryList><Query Id="0" Path="Microsoft-Windows-GroupPolicy/Operational"><Select Path="Microsoft-Windows-GroupPolicy/Operational">*[System[(EventID=8000 or EventID=8002 or EventID=8004 or EventID=8006)]]</Select></Query><Query Id="1" Path="Microsoft-Windows-TerminalServices-LocalSessionManager/Operational"><Select Path="Microsoft-Windows-TerminalServices-LocalSessionManager/Operational">*[System[EventID=23]]</Select></Query><Query Id="2" Path="Microsoft-Windows-WLAN-AutoConfig/Operational"><Select Path="Microsoft-Windows-WLAN-AutoConfig/Operational">*[System[EventID=8001]]</Select></Query></QueryList>'
|
|
$subscription=[Security.SecurityElement]::Escape($subscription)
|
|
$command=[Security.SecurityElement]::Escape("$env:WINDIR\System32\WindowsPowerShell\v1.0\powershell.exe")
|
|
$arguments=[Security.SecurityElement]::Escape('-NoProfile -NonInteractive -ExecutionPolicy Bypass -File "'+$helper+'"')
|
|
$taskXml=@"
|
|
<?xml version="1.0" encoding="UTF-16"?>
|
|
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
|
|
<RegistrationInfo><Author>La Salle</Author><Description>Computer Wi-Fi recovery after boot, computer policy, logoff or network change. No periodic polling.</Description></RegistrationInfo>
|
|
<Triggers><BootTrigger><Enabled>true</Enabled><Delay>PT10S</Delay></BootTrigger><EventTrigger><Enabled>true</Enabled><Subscription>$subscription</Subscription><Delay>PT3S</Delay></EventTrigger></Triggers>
|
|
<Principals><Principal id="System"><UserId>S-1-5-18</UserId><RunLevel>HighestAvailable</RunLevel></Principal></Principals>
|
|
<Settings><MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy><DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries><StopIfGoingOnBatteries>false</StopIfGoingOnBatteries><StartWhenAvailable>true</StartWhenAvailable><RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable><Enabled>true</Enabled><ExecutionTimeLimit>PT1M</ExecutionTimeLimit></Settings>
|
|
<Actions Context="System"><Exec><Command>$command</Command><Arguments>$arguments</Arguments></Exec></Actions>
|
|
</Task>
|
|
"@
|
|
$taskName='CED WiFi policy connection'
|
|
$existing=Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue
|
|
$versionFile=Join-Path $destination 'maintenance-v2.sha256'
|
|
$sha=[Security.Cryptography.SHA256]::Create()
|
|
try{$hash=[BitConverter]::ToString($sha.ComputeHash([Text.Encoding]::UTF8.GetBytes($taskXml))).Replace('-','')}finally{$sha.Dispose()}
|
|
$valid=$existing -and $existing.Triggers.Count -eq 2 -and @($existing.Triggers|Where-Object{$_.Repetition.Interval}).Count -eq 0 -and $existing.Principal.UserId -in 'SYSTEM','S-1-5-18' -and (Test-Path -LiteralPath $versionFile) -and (Get-Content -LiteralPath $versionFile -Raw).Trim() -eq $hash
|
|
if(-not $valid){Register-ScheduledTask -TaskName $taskName -Xml $taskXml -Force|Out-Null;$hash|Set-Content -LiteralPath $versionFile -Encoding ASCII}
|
|
$profileState=& $helper
|
|
if(-not $profileState.MachineProfileReady){
|
|
# Computer authentication reads the existing restricted AD WLAN object.
|
|
# The PSK stays in memory and Windows encrypts it into its machine store.
|
|
$searchRoot=$null;$searcher=$null;$found=$null
|
|
try{
|
|
# The managed hosts entry directs this FQDN to the Azure public endpoint of
|
|
# the primary DC; the signed and sealed machine binding protects the read.
|
|
$domainDn=($computer.Domain.Split('.')|ForEach-Object{'DC='+$_}) -join ','
|
|
$authentication=[DirectoryServices.AuthenticationTypes]::Secure -bor [DirectoryServices.AuthenticationTypes]::Signing -bor [DirectoryServices.AuthenticationTypes]::Sealing
|
|
$searchRoot=[DirectoryServices.DirectoryEntry]::new("LDAP://$primaryDomainController/CN=Policies,CN=System,"+$domainDn,$null,$null,$authentication)
|
|
$searcher=[DirectoryServices.DirectorySearcher]::new($searchRoot)
|
|
$searcher.ClientTimeout=[TimeSpan]::FromSeconds(10)
|
|
$searcher.ServerTimeLimit=[TimeSpan]::FromSeconds(10)
|
|
$searcher.Filter='(&(objectClass=ms-net-ieee-80211-GroupPolicy)(cn=CED Wi-Fi))'
|
|
[void]$searcher.PropertiesToLoad.Add('ms-net-ieee-80211-GP-PolicyData')
|
|
$found=$searcher.FindAll()
|
|
if($found.Count -ne 1){throw 'Expected exactly one authorized CED WLAN policy.'}
|
|
[xml]$policy=[string]$found[0].Properties['ms-net-ieee-80211-gp-policydata'][0]
|
|
$profile=$policy.SelectSingleNode('//*[local-name()="WLANProfile"]')
|
|
$profileState=& $helper -ProvisionXml $profile.OuterXml
|
|
}finally{if($found){$found.Dispose()};if($searcher){$searcher.Dispose()};if($searchRoot){$searchRoot.psbase.Dispose()};$profile=$null;$policy=$null}
|
|
}
|
|
if(-not $profileState.MachineProfileReady){throw 'Machine Wi-Fi profile is not ready.'}
|
|
$profileState
|