[CmdletBinding()] param( [Parameter(Mandatory)][PSCredential]$Credential, [string]$TargetOuDn = 'OU=Centro de Experiencia Digital,OU=Laboratorio,DC=lci,DC=lasalle,DC=mx', [string]$Ssid = 'Centro de Experiencia Digital', [ValidatePattern('^[A-Za-z0-9]{3}$')][string]$SiteCode = 'LCI', [string]$WacThumbprint = 'CF2B7C6899885BF9EB0E59054F8E8AB2D4390513' ) $ErrorActionPreference = 'Stop' Import-Module ActiveDirectory Clear-DnsClientCache $computers = @(Get-ADComputer -SearchBase $TargetOuDn -Filter 'Enabled -eq $true' -Properties DNSHostName) $notification = @{} $notificationError = $null try { foreach ($resource in (Get-CimInstance -Namespace ("root\SMS\site_"+$SiteCode) -ClassName SMS_CombinedDeviceResources)) { $notification[$resource.Name] = $resource } } catch { $notificationError = $_.Exception.Message } function Add-NotificationStatus($result) { $resource = $notification[$result.Name] # Null means no provider data, not a confirmed offline client. $result | Add-Member -NotePropertyMembers @{ NotificationOnline = $(if ($resource) { $resource.CNIsOnline } else { $null }) NotificationMP = $(if ($resource) { $resource.CNAccessMP } else { $null }) NotificationLastOnlineTime = $(if ($resource) { $resource.CNLastOnlineTime } else { $null }) NotificationQueryError = $notificationError } -PassThru } $online = @() foreach ($computer in $computers) { $client = [Net.Sockets.TcpClient]::new() try { $connect = $client.BeginConnect($computer.DNSHostName,5985,$null,$null) $available = $connect.AsyncWaitHandle.WaitOne(1000) -and $client.Connected } catch { $available = $false } finally { $client.Dispose() } if ($available) { $online += $computer.DNSHostName } else { Add-NotificationStatus ([pscustomobject]@{Name=$computer.Name;Reachable=$false;WifiConnected=$false;ClientRunning=$false;Error='No WinRM TCP connection at current DNS address.'}) } } $options = New-PSSessionOption -OpenTimeout 10000 -OperationTimeout 60000 if ($online.Count) { $reported = @(Invoke-Command -ComputerName $online -Credential $Credential -Authentication Kerberos -SessionOption $options -ErrorAction Continue -ArgumentList $Ssid,$WacThumbprint -ScriptBlock { param($ssid,$thumbprint) $liveSsid = @([Windows.Networking.Connectivity.NetworkInformation,Windows.Networking.Connectivity,ContentType=WindowsRuntime]::GetConnectionProfiles() | Where-Object IsWlanConnectionProfile | ForEach-Object { $_.WlanConnectionProfileDetails.GetConnectedSsid() }) $event = Get-WinEvent -FilterHashtable @{LogName='Microsoft-Windows-WLAN-AutoConfig/Operational';Id=8001} -MaxEvents 1 -ErrorAction SilentlyContinue $profile = $null; $authentication = $null if ($event) { [xml]$xml = $event.ToXml() $profile = ($xml.Event.EventData.Data | Where-Object Name -eq ProfileName).'#text' $authentication = ($xml.Event.EventData.Data | Where-Object Name -eq AuthenticationAlgorithm).'#text' } $service = Get-Service CcmExec -ErrorAction SilentlyContinue $version = $null; $site = $null if ($service.Status -eq 'Running') { $sms = Get-CimInstance -Namespace root\ccm -ClassName SMS_Client -ErrorAction SilentlyContinue $version = $sms.ClientVersion $site = (Invoke-CimMethod -Namespace root\ccm -ClassName SMS_Client -MethodName GetAssignedSite -ErrorAction SilentlyContinue).sSiteCode } [pscustomobject]@{ Name=$env:COMPUTERNAME;Reachable=$true;SSID=$liveSsid -join ',';Profile=$profile WifiConnected=($liveSsid -contains $ssid -and $profile -eq $ssid) IP=@(Get-NetIPAddress -AddressFamily IPv4 | Where-Object IPAddress -like '192.168.*' | ForEach-Object IPAddress) -join ',' Authentication=$authentication;ClientRunning=($service.Status -eq 'Running');ClientVersion=$version;Site=$site WacRootTrusted=(Test-Path ("Cert:\LocalMachine\Root\"+$thumbprint));Error=$null } }) foreach ($result in $reported) { Add-NotificationStatus $result } foreach ($hostname in $online) { if ($reported.PSComputerName -inotcontains $hostname) { Add-NotificationStatus ([pscustomobject]@{Name=$hostname.Split('.')[0];Reachable=$false;WifiConnected=$false;ClientRunning=$false;Error='WinRM session or client verification failed after TCP probe.'}) } } }