101 lines
6.4 KiB
PowerShell
101 lines
6.4 KiB
PowerShell
[CmdletBinding()]
|
|
param(
|
|
[Parameter(Mandatory)][string]$InventoryPath,
|
|
[Parameter(Mandatory)][PSCredential]$Credential,
|
|
[string]$TargetOuDn = 'OU=Centro de Experiencia Digital,OU=Laboratorio,DC=lci,DC=lasalle,DC=mx',
|
|
[string]$DomainController = $env:COMPUTERNAME,
|
|
[string]$DnsServer = '192.168.54.211',
|
|
[string]$FallbackDnsServer = '192.168.54.1',
|
|
[switch]$InstallClient,
|
|
[switch]$MigrateWifi,
|
|
[switch]$RestartPendingInstall,
|
|
[string]$ClientSource = '\\sgu-dc02.lci.lasalle.mx\CEDClient$'
|
|
)
|
|
$ErrorActionPreference = 'Stop'
|
|
Import-Module ActiveDirectory
|
|
$domain = Get-ADDomain -Server $DomainController
|
|
Get-ADOrganizationalUnit -Identity $TargetOuDn -Server $DomainController | Out-Null
|
|
$allowed = @(Get-ADComputer -SearchBase $TargetOuDn -Filter 'Enabled -eq $true' -Server $DomainController)
|
|
$inventory = Get-Content -LiteralPath $InventoryPath -Raw | ConvertFrom-Json
|
|
$source = 'C:\Program Files\Microsoft Configuration Manager\Client\ccmsetup.exe'
|
|
if ($InstallClient) {
|
|
$signature = Get-AuthenticodeSignature -LiteralPath $source
|
|
if ($signature.Status -ne 'Valid' -or $signature.SignerCertificate.Subject -notlike '*Microsoft Corporation*') {
|
|
throw 'The Configuration Manager installer is not a valid Microsoft signed binary.'
|
|
}
|
|
}
|
|
$options = New-PSSessionOption -OpenTimeout 10000 -OperationTimeout 180000
|
|
foreach ($item in $inventory) {
|
|
if ($item.WinRM -eq $false) { continue }
|
|
if ($item.Name -notin $allowed.Name) { throw "Inventory host outside approved OU: $($item.Name)" }
|
|
[Net.IPAddress]::Parse($item.IP) | Out-Null
|
|
$session = $null
|
|
try {
|
|
$address = $item.IP
|
|
# After migration the old DHCP address is stale. Prefer a reachable
|
|
# current AD/DNS hostname, then validate the actual remote identity.
|
|
$hostname = ($allowed | Where-Object Name -eq $item.Name).DNSHostName
|
|
if (-not $hostname) { $hostname = $item.Name+'.'+$domain.DNSRoot }
|
|
$probe = [Net.Sockets.TcpClient]::new()
|
|
try {
|
|
$pending = $probe.BeginConnect($hostname,5985,$null,$null)
|
|
if ($pending.AsyncWaitHandle.WaitOne(1000) -and $probe.Connected) { $address = $hostname }
|
|
} catch { } finally { $probe.Dispose() }
|
|
$session = New-PSSession -ComputerName $address -Credential $Credential -Authentication Negotiate -SessionOption $options
|
|
$identity = Invoke-Command -Session $session {
|
|
[pscustomobject]@{Name=$env:COMPUTERNAME;Domain=(Get-CimInstance Win32_ComputerSystem).Domain;Trusted=Test-ComputerSecureChannel}
|
|
}
|
|
if ($identity.Name -ine $item.Name -or $identity.Domain -ine $domain.DNSRoot -or -not $identity.Trusted) {
|
|
throw 'Remote identity/domain/secure-channel validation failed.'
|
|
}
|
|
$state = Invoke-Command -Session $session -ArgumentList $DnsServer,$domain.DNSRoot,$FallbackDnsServer {
|
|
param($dns,$suffix,$fallbackDns)
|
|
New-Item C:\ProgramData\LCI-Admin -ItemType Directory -Force | Out-Null
|
|
$backup = 'C:\ProgramData\LCI-Admin\CED-dns-before.json'
|
|
if (-not (Test-Path $backup)) {
|
|
Get-DnsClientServerAddress -AddressFamily IPv4 | Select-Object InterfaceAlias,InterfaceIndex,ServerAddresses |
|
|
ConvertTo-Json -Depth 3 | Set-Content -LiteralPath $backup
|
|
}
|
|
$wifi = @(Get-NetAdapter | Where-Object { $_.NdisPhysicalMedium -eq 9 -and $_.Status -eq 'Up' })
|
|
if ($wifi.Count -ne 1) { throw 'Expected one active Wi-Fi interface.' }
|
|
Set-DnsClientServerAddress -InterfaceIndex $wifi[0].InterfaceIndex -ServerAddresses @($dns,$fallbackDns)
|
|
Set-DnsClient -InterfaceIndex $wifi[0].InterfaceIndex -ConnectionSpecificSuffix $suffix -RegisterThisConnectionsAddress $true
|
|
Register-DnsClient
|
|
& gpupdate.exe /target:computer /force /wait:90 | Out-Null
|
|
if ($LASTEXITCODE) { throw "gpupdate failed: $LASTEXITCODE" }
|
|
[pscustomobject]@{Name=$env:COMPUTERNAME;WifiPolicyPresent=(netsh.exe wlan show profiles | Select-String 'Centro de Experiencia Digital').Count -gt 0;ClientPresent=[bool](Get-Service CcmExec -ErrorAction SilentlyContinue)}
|
|
}
|
|
if (-not $state.WifiPolicyPresent) { throw 'Native CED Wi-Fi policy was not applied.' }
|
|
if ($InstallClient -and -not $state.ClientPresent) {
|
|
Copy-Item -LiteralPath $source -Destination C:\ProgramData\LCI-Admin\ccmsetup.exe -ToSession $session
|
|
Invoke-Command -Session $session -ArgumentList $ClientSource,[bool]$RestartPendingInstall {
|
|
param($clientSource,$restartPending)
|
|
if (-not $restartPending -and (Get-Process ccmsetup -ErrorAction SilentlyContinue)) { return }
|
|
# Replace a pending installer that was waiting for an inaccessible source.
|
|
if (Get-Service ccmsetup -ErrorAction SilentlyContinue) {
|
|
Stop-Service ccmsetup -Force -ErrorAction SilentlyContinue
|
|
}
|
|
# /source alone forces the complete SMB media. Combining it with
|
|
# /mp gives MP/DP discovery priority and can leave setup waiting.
|
|
$action = New-ScheduledTaskAction -Execute C:\ProgramData\LCI-Admin\ccmsetup.exe -Argument ('/source:"{0}" SMSSITECODE=LCI SMSMP=sgu-dc02.lci.lasalle.mx' -f $clientSource)
|
|
Register-ScheduledTask -TaskName 'CED Configuration Manager enrollment' -Action $action -User SYSTEM -RunLevel Highest -Force | Out-Null
|
|
Start-ScheduledTask -TaskName 'CED Configuration Manager enrollment'
|
|
}
|
|
}
|
|
if ($MigrateWifi) {
|
|
foreach ($file in 'Connect-SguCedWifi.ps1','Install-SguCedWifiMaintenance.ps1') {
|
|
Copy-Item (Join-Path $PSScriptRoot $file) (Join-Path C:\ProgramData\LCI-Admin $file) -ToSession $session -Force
|
|
}
|
|
Invoke-Command -Session $session {
|
|
& powershell.exe -NoProfile -ExecutionPolicy Bypass -File C:\ProgramData\LCI-Admin\Install-SguCedWifiMaintenance.ps1
|
|
if ($LASTEXITCODE) { throw 'Wi-Fi maintenance task installation failed.' }
|
|
}
|
|
}
|
|
[pscustomobject]@{Name=$item.Name;IP=$item.IP;Prepared=$true;ClientPresent=$state.ClientPresent;InstallationRequested=[bool]$InstallClient;Error=$null}
|
|
} catch {
|
|
[pscustomobject]@{Name=$item.Name;IP=$item.IP;Prepared=$false;ClientPresent=$false;InstallationRequested=$false;Error=$_.Exception.Message}
|
|
} finally {
|
|
if ($session) { Remove-PSSession $session }
|
|
}
|
|
}
|