Monday, 13 November 2017 19:12

Using MDT to Import Wireless Network Profile

Written by
Rate this item
(6 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 14874 times Last modified on Monday, 13 November 2017 19:22

Recent Posts

  • Windows 10 21H2 Built-In Apps: What to Keep
    The development of the Windows 10, version 21H2 is finished and the update will soon be available for download from…
    Written on Wednesday, 20 October 2021 11:41
  • Group Policy Changes in Windows 10 21H2
    As Windows 10, version 21H2 update development winds down, Microsoft is now preparing for the final release of the Windows…
    Written on Wednesday, 20 October 2021 07:20
  • Group Policy Changes in Windows 10 20H1 Preview
    As Windows 10 Vibranium Update (20H1) development winds down, Microsoft is now beginning the phase of checking in the final…
    Written on Tuesday, 14 January 2020 04:51
  • An alternative ESU MAK Activation Solution
    This blog post was shared with me by a colleague of mine, Daniel Dorner, a Microsoft Premier Field Engineer. It’s…
    Written on Wednesday, 04 December 2019 21:04
  • The Case of Missing UE-V Templates
    My customers often deal with unexpected Windows behavior and this case is no different. This particular one is especially interesting…
    Written on Tuesday, 03 September 2019 12:20
  • The Case of Changing Default Printer
    While I sometimes long for the day when I no longer have to deal with unexpected Windows 10 behavior, there’s…
    Written on Wednesday, 14 August 2019 20:36