[CmdletBinding()] param( [string]$ProfileName = 'Centro de Experiencia Digital', [string]$Ssid = '', [ValidateRange(1,30)][int]$TimeoutMinutes = 10, [string]$FallbackProfile = '', [string]$StatusPath = '', [switch]$Force, [switch]$RequireGroupPolicy ) $ErrorActionPreference = 'Stop' if (-not $Ssid) { $Ssid = $ProfileName } # Connect to the already provisioned profile. Do not scan BSSIDs or change # Windows location consent: those operations are unnecessary for this task. if (-not ('Sgu.CedWlan' -as [type])) { Add-Type -TypeDefinition @' using System; using System.Runtime.InteropServices; namespace Sgu { public static class CedWlan { [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)] public struct Parameters { public int Mode; [MarshalAs(UnmanagedType.LPWStr)] public string Profile; public IntPtr Ssid; public IntPtr Bssids; public int BssType; public uint Flags; } [DllImport("wlanapi.dll")] static extern uint WlanOpenHandle(uint version,IntPtr reserved,out uint negotiated,out IntPtr handle); [DllImport("wlanapi.dll")] static extern uint WlanEnumInterfaces(IntPtr handle,IntPtr reserved,out IntPtr list); [DllImport("wlanapi.dll",CharSet=CharSet.Unicode)] static extern uint WlanConnect(IntPtr handle,ref Guid id,ref Parameters parameters,IntPtr reserved); [DllImport("wlanapi.dll",CharSet=CharSet.Unicode)] static extern uint WlanGetProfile(IntPtr handle,ref Guid id,string profile,IntPtr reserved,out IntPtr xml,ref uint flags,out uint access); [DllImport("wlanapi.dll",CharSet=CharSet.Unicode)] static extern uint WlanSetProfile(IntPtr handle,ref Guid id,uint flags,string xml,string security,[MarshalAs(UnmanagedType.Bool)] bool overwrite,IntPtr reserved,out uint reason); [DllImport("wlanapi.dll")] static extern uint WlanCloseHandle(IntPtr handle,IntPtr reserved); [DllImport("wlanapi.dll")] static extern void WlanFreeMemory(IntPtr memory); static void Check(uint result) { if(result!=0) throw new System.ComponentModel.Win32Exception((int)result); } public static void Connect(string profile, bool requireGroupPolicy) { IntPtr handle=IntPtr.Zero, list=IntPtr.Zero; uint version; Check(WlanOpenHandle(2,IntPtr.Zero,out version,out handle)); try { Check(WlanEnumInterfaces(handle,IntPtr.Zero,out list)); int count=Marshal.ReadInt32(list); if(count==0) throw new InvalidOperationException("No WLAN interfaces."); for(int i=0;ifalse")) { uint reason; Check(WlanSetProfile(handle,ref id,1,xml,null,true,IntPtr.Zero,out reason)); } } finally { if(xmlPointer!=IntPtr.Zero) WlanFreeMemory(xmlPointer); } Parameters p=new Parameters { Mode=0,Profile=profile,BssType=1,Flags=0 }; Check(WlanConnect(handle,ref id,ref p,IntPtr.Zero)); } } finally { if(list!=IntPtr.Zero) WlanFreeMemory(list); WlanCloseHandle(handle,IntPtr.Zero); } } } } '@ } $start = Get-Date $deadline = $start.AddMinutes($TimeoutMinutes) function Write-Status([string]$Status, $Details) { if ($StatusPath) { [pscustomobject]@{Time=Get-Date;Profile=$ProfileName;Status=$Status;Details=$Details} | ConvertTo-Json -Depth 4 | Set-Content -LiteralPath $StatusPath -Encoding UTF8 } } if (-not $Force) { $liveSsid = @() try { $connections = [Windows.Networking.Connectivity.NetworkInformation,Windows.Networking.Connectivity,ContentType=WindowsRuntime]::GetConnectionProfiles() $liveSsid = @($connections | Where-Object IsWlanConnectionProfile | ForEach-Object { $_.WlanConnectionProfileDetails.GetConnectedSsid() }) } catch { # An unavailable SSID API means we must verify a fresh connection event. } $latest = Get-WinEvent -FilterHashtable @{ LogName='Microsoft-Windows-WLAN-AutoConfig/Operational';Id=8001,8003 } -MaxEvents 1 -ErrorAction SilentlyContinue if ($liveSsid -contains $Ssid -and $latest -and $latest.Id -eq 8001) { [xml]$latestXml = $latest.ToXml() $data = $latestXml.Event.EventData.Data if (@($data | Where-Object { $_.Name -eq 'SSID' -and $_.'#text' -eq $Ssid }).Count -and @($data | Where-Object { $_.Name -eq 'ProfileName' -and $_.'#text' -eq $ProfileName }).Count) { Write-Status 'AlreadyConnected' $null return } } } try { do { [Sgu.CedWlan]::Connect($ProfileName,[bool]$RequireGroupPolicy) Start-Sleep -Seconds 10 $connected = Get-WinEvent -FilterHashtable @{ LogName='Microsoft-Windows-WLAN-AutoConfig/Operational';Id=8001;StartTime=$start } -MaxEvents 20 -ErrorAction SilentlyContinue | Where-Object { [xml]$eventXml = $_.ToXml() $eventData = $eventXml.Event.EventData.Data @($eventData | Where-Object { $_.Name -eq 'SSID' -and $_.'#text' -eq $Ssid }).Count -gt 0 -and @($eventData | Where-Object { $_.Name -eq 'ProfileName' -and $_.'#text' -eq $ProfileName }).Count -gt 0 } if ($connected) { Register-DnsClient -ErrorAction SilentlyContinue Write-Status 'Connected' $null return } } while ((Get-Date) -lt $deadline) $failures = @(Get-WinEvent -FilterHashtable @{ LogName='Microsoft-Windows-WLAN-AutoConfig/Operational';Id=8002;StartTime=$start } -MaxEvents 3 -ErrorAction SilentlyContinue | ForEach-Object Message) Write-Status 'Failed' $failures throw "No successful WLAN connection event for '$ProfileName' within $TimeoutMinutes minutes." } catch { Write-Status 'Failed' $_.Exception.Message if ($FallbackProfile) { [Sgu.CedWlan]::Connect($FallbackProfile,$false) } throw }