Monday, 13 November 2017 19:12

Using MDT to Import Wireless Network Profile

Written by
Rate this item
(5 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 2226 times Last modified on Monday, 13 November 2017 19:22
  1. Comments (0)

  2. Add yours
There are no comments posted here yet

Leave your comments

Posting comment as a guest.
0 Characters
Attachments (0 / 3)
Share Your Location

Recent Posts

  • Yet Another Windows 10 Optimization Script
    As a reminder, Microsoft will be ending support for Windows 7 SP1 on January 14, 2020. I've had multiple enterprise…
    Written on Monday, 25 June 2018 16:09
  • Automating Dell BIOS Configuration Using MDT
    It’s been a busy couple of weeks for me, so I’m slowly going through a backlog of things to cover.…
    Written on Thursday, 21 June 2018 08:11
  • Configuring HP BIOS Using MDT
    This is the second post in my series that explores one of the most common questions I’ve been asked from…
    Written on Tuesday, 19 June 2018 09:54
  • BIOS to UEFI - The Easy Way: MBR2GPT
    This article is the first blog post in a series I'll write over the coming days that will provide a…
    Written on Monday, 11 June 2018 17:08
  • Force LAPS Password Reset during MDT OSD
    My customers often send me exciting cases. This particular one is especially interesting because it is common in infrastructures that…
    Written on Friday, 08 June 2018 10:10
  • Localizing Inbox Apps during OSD
    As a reader of this blog, I suspect that most of you, like me, are frequenting Twitter. And I bet…
    Written on Monday, 04 June 2018 18:18