[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 = '', [string]$FallbackDnsServer = '', [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 $networkConfiguration=[IO.File]::ReadAllText((Join-Path $PSScriptRoot 'ced-network.json'))|ConvertFrom-Json if(-not $DnsServer){$DnsServer=[string]$networkConfiguration.DnsServers[0]} if(-not $FallbackDnsServer){$FallbackDnsServer=[string]$networkConfiguration.DnsServers[1]} $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) { Invoke-Command -Session $session { $task=Get-ScheduledTask -TaskName 'LCI-ConfigMgr2509-ClientRegistration' -ErrorAction SilentlyContinue if(-not $task -or -not $task.Settings.Enabled){throw 'Apply the global SYSTEMCENTER Configuration Manager GPO before requesting client installation.'} Start-ScheduledTask -InputObject $task } } if ($MigrateWifi) { foreach ($file in 'Install-SguCedWifiMaintenance.ps1','Repair-SguCedWifi.ps1','Set-SguCedClientNetwork.ps1','ced-network.json') { 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 } } }