Monday, 13 November 2017 19:12

Using MDT to Import Wireless Network Profile

Written by
Rate this item
(4 votes)

image

The other day, as I was working with a customer on improving and optimizing his Windows 10 image, one of IT technicians asked if it would be possible to import a wireless network profile to devices during the OS deployment without resorting to Group Policies (I am sure they had good reasons). By deploying these settings, the customer hoped to minimize the effort that end users require to connect to the corporate wireless network.

From my past experiences, I knew that you can configure 802.11 wireless connectivity and security settings using the Netsh commands for wireless local area network (netsh wlan).

The first step was to configure information about the Wi-Fi network that users will connect to and then run the following command from an elevated command prompt to export specified WLAN profile to an XML file:

netsh export profile folder=%PathAndFileName% name=%ProfileName% key=clear

Note: since the corporate network was using WPA2-PSK authentication, I added the optional parameter key=clear to export WPA2 key in plain text.

The next (and final) step was to add a WLAN profile to a computer during OSD using the add profile command. I knew that configuring client computers to connect automatically to the corporate wireless network can be accomplished by running following command:

netsh wlan add profile filename=%PathAndFileName%

I wanted to wrap the configure functionality into an easy to use PowerShell script. As per usual, I am not a fan of reinventing the wheel - there is literally no reason to waste time and develop a solution that might already be available on TechNet or an IT pro's blog. However, while a quick Google search yielded a few results, none of the scripts did what I wanted them to do: import one (or multiple) network profiles and evaluate netsh return code, so I quickly cobbled together a script using the Invoke-EXE function from Mikael Nystrom. Below (and on GitHub) is the end result:

# Determine where to do the logging 
$tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment 
$logPath = $tsenv.Value("LogPath") 
$logFile = "$logPath\$($myInvocation.MyCommand).log"

# Start the logging 
Start-Transcript $logFile
Write-Host "Logging to $logFile"

# https://deploymentbunny.com/2015/09/29/powershell-is-kinginvoke-exe-could-help-you-run-exe-using-powershell/
Function Invoke-Exe {
    param(
        [parameter(mandatory=$true,position=0)]
        [ValidateNotNullOrEmpty()]
        [string]
        $Executable,

        [parameter(mandatory=$false,position=1)]
        [string]
        $Arguments
    )

    if($Arguments -eq "")
    {
        Write-Verbose "Running Start-Process -FilePath $Executable -ArgumentList $Arguments -NoNewWindow -Wait -Passthru"
        $ReturnFromEXE = Start-Process -FilePath $Executable -NoNewWindow -Wait -Passthru
    }else{
        Write-Verbose "Running Start-Process -FilePath $Executable -ArgumentList $Arguments -NoNewWindow -Wait -Passthru"
        $ReturnFromEXE = Start-Process -FilePath $Executable -ArgumentList $Arguments -NoNewWindow -Wait -Passthru
    }
    Write-Verbose "Returncode is $($ReturnFromEXE.ExitCode)"
    Return $ReturnFromEXE.ExitCode
}

#Custom Code Starts--------------------------------------

# Get all Wifi profiles 
$xml = Get-ChildItem $PSScriptRoot | Where-Object {$_.extension -eq ".xml"} 

# Apply wifi profiles
If ($xml) {
   ForEach ($profile in $xml) {
        $cmdline = 'wlan add profile filename="' + $profile.FullName +'" user=all'
        Write-Host "Command line set to" $cmdline
        
        $ExitCode = Invoke-Exe -Executable "C:\Windows\system32\netsh.exe" -Arguments $cmdline
        Write-Host "Exit code from command:" $ExitCode
        If ($ExitCode -ne 0) {
            Write-Warning "An error occured. Exiting.."
            Exit $ExitCode
        }
   }
}
Else {
    Write-Host "No Wifi profiles found. Exiting..."
    Exit 0
}

#Custom Code Ends--------------------------------------

Stop-Transcript
Exit $ExitCode

Setting this up is dead simple. Simply put this script and your XML profile(s) into the same folder and add a Run PowerShell script step to your TS. That's it!

Read 516 times Last modified on Monday, 13 November 2017 19:22

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.

Recent Posts