[CmdletBinding()] param([switch]$PreferCedNow,[string]$ProvisionXml='') $ErrorActionPreference='Stop' $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' if(-not ('Sgu.CedMachineWifi' -as [type])){ Add-Type -ReferencedAssemblies System.dll,System.Xml.dll -TypeDefinition @' using System; using System.Collections.Generic; using System.Collections; using System.Runtime.InteropServices; using System.Text; using System.Xml; namespace Sgu { public class CedWifiResult { public string Interface; public string Ssid; public string Profile; public bool MachineProfileReady; public bool GroupProfileRepaired; public bool MachineProfileCreated; public int OtherProfilesMadeManual; public bool ConnectionRequested; public uint QueryError; } public static class CedMachineWifi { [StructLayout(LayoutKind.Sequential,CharSet=CharSet.Unicode)] struct Connection { public int Mode; [MarshalAs(UnmanagedType.LPWStr)] public string Profile; public IntPtr Ssid; public IntPtr Bssids; public int BssType; public uint Flags; } [StructLayout(LayoutKind.Sequential)] struct Session { public int Id; public IntPtr Name; public int State; } [DllImport("wlanapi.dll")] static extern uint WlanOpenHandle(uint v,IntPtr r,out uint n,out IntPtr h); [DllImport("wlanapi.dll")] static extern uint WlanEnumInterfaces(IntPtr h,IntPtr r,out IntPtr p); [DllImport("wlanapi.dll")] static extern uint WlanGetProfileList(IntPtr h,ref Guid i,IntPtr r,out IntPtr p); [DllImport("wlanapi.dll",CharSet=CharSet.Unicode)] static extern uint WlanGetProfile(IntPtr h,ref Guid i,string name,IntPtr r,out IntPtr xml,ref uint flags,out uint access); [DllImport("wlanapi.dll",CharSet=CharSet.Unicode)] static extern uint WlanSetProfile(IntPtr h,ref Guid i,uint flags,string xml,string security,[MarshalAs(UnmanagedType.Bool)]bool overwrite,IntPtr r,out uint reason); [DllImport("wlanapi.dll",CharSet=CharSet.Unicode)] static extern uint WlanSetProfilePosition(IntPtr h,ref Guid i,string name,uint position,IntPtr r); [DllImport("wlanapi.dll")] static extern uint WlanQueryInterface(IntPtr h,ref Guid i,int opcode,IntPtr r,out uint size,out IntPtr data,out int type); [DllImport("wlanapi.dll")] static extern uint WlanSetInterface(IntPtr h,ref Guid i,int opcode,uint size,ref int data,IntPtr r); [DllImport("wlanapi.dll",CharSet=CharSet.Unicode)] static extern uint WlanConnect(IntPtr h,ref Guid i,ref Connection c,IntPtr r); [DllImport("wlanapi.dll")] static extern uint WlanCloseHandle(IntPtr h,IntPtr r); [DllImport("wlanapi.dll")] static extern void WlanFreeMemory(IntPtr p); [DllImport("wtsapi32.dll",CharSet=CharSet.Unicode,SetLastError=true)] static extern bool WTSEnumerateSessions(IntPtr h,int reserved,int version,out IntPtr list,out int count); [DllImport("wtsapi32.dll",CharSet=CharSet.Unicode)] static extern bool WTSQuerySessionInformation(IntPtr h,int id,int type,out IntPtr data,out int bytes); [DllImport("wtsapi32.dll")] static extern void WTSFreeMemory(IntPtr p); static void Check(uint e){if(e!=0)throw new System.ComponentModel.Win32Exception((int)e);} static XmlDocument Read(IntPtr h,ref Guid id,string name,out uint flags){IntPtr p=IntPtr.Zero;uint access;flags=0;uint e=WlanGetProfile(h,ref id,name,IntPtr.Zero,out p,ref flags,out access);if(e!=0)return null;try{var x=new XmlDocument();x.LoadXml(Marshal.PtrToStringUni(p));return x;}finally{WlanFreeMemory(p);}} static string Text(XmlDocument x,string name){XmlNode n=x.SelectSingleNode("//*[local-name()='"+name+"']");return n==null?"":n.InnerText;} static bool Auto(XmlDocument x){bool changed=false;XmlElement root=x.DocumentElement;XmlNode mode=root.SelectSingleNode("*[local-name()='connectionMode']");if(mode==null)throw new InvalidOperationException("Missing connection mode.");if(mode.InnerText!="auto"){mode.InnerText="auto";changed=true;}XmlNode sw=root.SelectSingleNode("*[local-name()='autoSwitch']");if(sw==null){sw=x.CreateElement("autoSwitch",root.NamespaceURI);root.InsertAfter(sw,mode);changed=true;}if(sw.InnerText!="false"){sw.InnerText="false";changed=true;}return changed;} static void Save(IntPtr h,ref Guid id,uint flags,XmlDocument x){uint reason;uint e=WlanSetProfile(h,ref id,flags,x.OuterXml,null,true,IntPtr.Zero,out reason);if(e!=0)throw new InvalidOperationException("Cannot save WLAN profile: "+e+", reason="+reason);} static void Connected(IntPtr h,ref Guid id,CedWifiResult result){IntPtr p=IntPtr.Zero;uint size;int type;uint e=WlanQueryInterface(h,ref id,7,IntPtr.Zero,out size,out p,out type);result.QueryError=e;try{if(e==0&&size>=556){result.Profile=Marshal.PtrToStringUni(IntPtr.Add(p,8));int len=Marshal.ReadInt32(p,520);if(len>=0&&len<=32){byte[] ssid=new byte[len];Marshal.Copy(IntPtr.Add(p,524),ssid,0,len);result.Ssid=Encoding.UTF8.GetString(ssid);}}}finally{if(p!=IntPtr.Zero)WlanFreeMemory(p);}} static void Connect(IntPtr h,ref Guid id,string name){Connection c=new Connection{Mode=0,Profile=name,BssType=1};Check(WlanConnect(h,ref id,ref c,IntPtr.Zero));} public static int UserSessions(){IntPtr list=IntPtr.Zero;int count,result=0;if(!WTSEnumerateSessions(IntPtr.Zero,0,1,out list,out count))throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());try{int size=Marshal.SizeOf(typeof(Session));for(int i=0;i2&&!String.IsNullOrEmpty(Marshal.PtrToStringUni(p)))result++;}finally{if(p!=IntPtr.Zero)WTSFreeMemory(p);}}}finally{WTSFreeMemory(list);}return result;} public static CedWifiResult[] Ensure(string provision,bool prefer,bool rollback,string prior,IDictionary facts){ const string ssid="Centro de Experiencia Digital", backup="CED - Equipo";IntPtr h=IntPtr.Zero,list=IntPtr.Zero;uint v;var results=new List();Check(WlanOpenHandle(2,IntPtr.Zero,out v,out h)); try{Check(WlanEnumInterfaces(h,IntPtr.Zero,out list));int count=Marshal.ReadInt32(list);for(int i=0;i