Jump to content

Recommended Posts

Posted

After a bunch of searching around, I finally decided to write a script to add wireless profiles and run it as a computer startup script.

Sharing it here in case it's useful to anybody else;

 

First of all, you need to export your profiles into an xml file and then hardcode the key into that xml, you can follow the guidance for that here;

 

Kreslavsky IT blog » Deploy wireless settings with Key using netsh script and GPO

 

Once you have your files prepped, rename them to match the name of the wireless profile (important if you want my script to work), pop them into a network share (could store it in the GPO itself) and then change the NTFS or sharing permissions to remove everybody and add domain computers as read.

 

Now you can leave it there with the info in hand but I wanted a wee bit more. I wanted to add a bunch of profiles and then set a preferred order of profiles. Couldn't find it so I ended up writing the script below

 

To run it use it as so "Profile1" "Profile2"

Note make sure you read the notes and change the path to xml constant

 

If you're using GPO, add it to the computers startup script and add the profiles in the arguments field with each profile separated by quotes

 

 

'Name :WLAN Profile Configurator

'Description :A Script to Create Wireless LAN Profiles on a PC and Set a preferred Order

'Author :Himji

'Date Created :01/03/2012 (British Date Format)

'Revision History :

'01/03/2012 :Script Created Ready for Testing

'Notes :

'1. :You will need to export the wireless config xml files and store them securely (as they contain the network key).

'2. :Ensure the name of the XML file is the same as the wireless network profile name.

'3. :Enter the network path the the XML files in the section marked with stars ("***") below

'4. :Usage. To use this script, invoke the script with the profilesw you want added in quotes seperated by a space

'5. :Usage cont. For example WLAN_Profile_Configurator "Profile1" "Profile2" "Profile3"

 

 

Option Explicit

'*************************Change the following settings here to cusomise the script*************************

'Define Location of Profile XML Files Here

CONST PathtoXML = "\\server\sharename\path"

'****************************************************End****************************************************

 

Const HKEY_LOCAL_MACHINE = &H80000002

Dim strComputer

Dim StrWLANName

Dim CmdSetProfileOrder

Dim IntPriority

Dim WSHNetwork

Dim WShell

Dim oReg

Dim strKeyPath

Dim strValueName

Dim dwValue

Dim strValue

Dim arrSubKeys

Dim Subkey

Dim Profile

Dim Return

Dim CmdAddWLAN

Dim ArrProfileOrder

 

'sets the focus of the script to run against the local system

Set WSHNetwork = WScript.CreateObject("WScript.Network")

strComputer = WSHNetwork.Computername

 

'CONNECTION OBJECTS

'create a connection object to the windows Shell commands

Set WShell = Wscript.CreateObject("Wscript.Shell")

'create a connection object to connect to the winmgmts ojbect

Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")

 

'DEFINED VARIABIES

'this is the registry entry to look for wireless NICs on the system

strKeyPath = "SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}"

 

 

'Adds the WLAN Profile to the client

Set ArrProfileOrder = WScript.Arguments

For Each Profile in ArrProfileOrder

CmdAddWLAN = "netsh wlan add profile filename=" & CHR(34) & PathtoXML & Profile & ".xml" & CHR(34)

Return = WShell.run (CmdAddWLAN,1, true)

Next

 

 

'enumerates all the sub keys underneath strkeypath and puts them in an array so the data can be used to find values that match for a wireless card

oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys

 

'This will look for the subkey that

For Each subkey In arrSubKeys

If subkey <> "Descriptions" Then

'Wscript.Echo strKeyPath & "\" & subkey & "\" & "Connection" 'Error Checking

'this sets the variable to the name of the registry key MediaSubType

strValueName = "MediaSubType"

'this will get the value of the registry key 1 = built in NIC 2 = Wireless

oReg.GetDWORDValue HKEY_LOCAL_MACHINE,strKeyPath & "\" & subkey & "\" & "Connection" ,strValueName,dwvalue

'this will get the value of the registry key name which tells us the name of the network card (Wireless Naetowrk Connection)

strValueName = "Name"

oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath & "\" & subkey & "\" & "Connection" ,strValueName,strValue

'If the media sub type = 2 then it is wireless so this will be the focus of the IF statement

If dwvalue = 2 Then

IntPriority = 0

'Sets the profile Order as defined at the start of the script

For Each Profile in ArrProfileOrder

IntPriority = IntPriority + 1

CmdSetProfileOrder = "netsh wlan set profileorder name=" & CHR(34) & Profile & CHR(34) & " interface=" & CHR(34) & strValue & CHR(34) & " Priority=" & IntPriority

Return = WShell.run (CmdSetProfileOrder,1, true)

Next

End If

End If

Next

 

 

Probably not the best of scripts but it seems to do the job well and I hope it helps somebody looking for the same

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...