Verify Ethernet Connectivity in Task Sequence

In some scenarios you might want to verify that the computer running the TS in question is connected to cabled network and not Wi-Fi. The script I have written for this purpose sets a TS variable called “ContinueTS” to True or False based on what the end-user choose to do when confronted with a messagebox.

So lets take a look at the script. First we set the variable to True and check the status of the ethernet connection using the Get-NetAdapter cmdlet.

$ContinueTS = "True"
$EthernetConnection = Get-NetAdapter -Physical | Where-Object {$_.Name -eq "Ethernet" -and $_.Status -eq "Up"}

If the cable is not connected, this query will give nothing back. In other words the $EthernetConnection variable will have a $null value. In this case the script continues from the next line:

if ($EthernetConnection -eq $null)
{
$TSProgressUI = New-Object -COMObject Microsoft.SMS.TSProgressUI
$TSProgressUI.CloseProgressDialog()
[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms') | Out-Null
$OutPut = "OK"

First we hide the TS progress to make way for the messagebox to be created later. Then we load the [System.Windows.Forms] .Net class to allow us to work with messageboxes and pipe this to Out-Null as we do not care about the output of the command.  Lastly we set the $OutPut variable to be used later.

Next we create a loop:

do {
$OutPut = [System.Windows.Forms.MessageBox]::Show("Please Connect Ethernet Cable" ,"Network Status" ,1)
$EthernetConnection = Get-NetAdapter -Physical | Where-Object {$_.Name -eq "Ethernet" -and $_.Status -eq "Up"}
 
} while ($EthernetConnection -eq $null -and $OutPut -eq "OK")

This do-loop will loop as long as the $EthernetConnection Variable equals $null and the end-user presses “OK” in the messagebox created at the first line. The number 1 at the end of the first line in the loop, creates a “OK/Cancel” messagebox. If a cable is connected and the OK button is pressed, the script skips to the final portion, but if the cancel button is pressed then this section will run:

if ($OutPut -eq "Cancel")
{
$ContinueTS = "False"
}

The $ContinueTS variable which will later be set as the value for the TS variable, is set to false. Finally the TS variable is created:

$TSVariable = New-Object -ComObject Microsoft.SMS.TSEnvironment
$TSVariable.Value("ContinueTS") = $ContinueTS

Now to the sequence:

We’ll leverage ServiceUI.exe from MDT to break out of the Task Sequence environment. The file can be aquired at the following locations:

X64: %ProgramFiles%\Microsoft Deployment Toolkit\Templates\Distribution\Tools\x64

X86: %ProgramFiles%\Microsoft Deployment Toolkit\Templates\Distribution\Tools\x86

Place the executable and the PowerShell script in a package and add run command line:

ServiceUI.exe -process:TSProgressUI.exe %SYSTEMROOT%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -WindowStyle Hidden -ExecutionPolicy Bypass -File Verify-Ethernet.ps1

It will then break out of the TS environment and pop the messagebox if an ethernetconnection is not active:

As mentioned earlier, the box will loop until a cabled connection is detected or the user chooses to cancel.

The second task sequence step called “Break Sequence” is simple a package with a program trying to launch a non-existent file and it has the following condition:

You could of course use the condition on all the following steps or on a top folder/group instead.

And thats it! The full script can be downloaded here:  and please leave your comments below.
Verify-Connectivity

Comments

  1. This was posted in the future!! What kind of sorcery is this!

    I’m kidding, but thank you for the script, this is exactly what I was looking for.

    1. @Kristo: Change the two lines in the script from

      $EthernetConnection = Get-NetAdapter -Physical | Where-Object {$_.Name -eq “Ethernet” -and $_.Status -eq “Up”}

      to

      $EthernetConnection = Get-NetAdapter -Physical | Where-Object {$_.Name -like “Ethernet*” -and $_.Status -eq “Up”}

      and I believe it should work better.

Leave a Reply

Your email address will not be published. Required fields are marked *