Skip to main content

Manage - Network Adapter Protocols

Summary

This task allows you to manage network protocols on Windows machines, including enabling, disabling, and configuring them to use DHCP.

Caution : Use this script with caution. Disabling IPv4 on the machine may result in a network disconnection.

Sample Run

Serach for Manage - Network Adapter Protocols task and click on it SampleRun

Selec Run Now and click Run Task button
SampleRun

Dependencies

Task Creation

Step 1

Navigate to AutomationTasks
step1

Step 2

Create a new Script Editor style task by choosing the Script Editor option from the Add dropdown menu
step2

The New Script page will appear on clicking the Script Editor button:
step3

Step 3

Fill in the following details in the Description section:

  • Name: Manage - Network Adapter Protocols
  • Description: It allows you to manage network protocols on Windows machines, including enabling, disabling, and configuring them to use DHCP.
    Caution : Use this script with caution. Disabling IPv4 on the machine may result in a network disconnection.
  • Category: Custom

step3

Script Editor

Click the Add Row button in the Script Editor section to start creating the script
AddRow

A blank function will appear:
BlankFunction

Row 1 Function: Set Pre-defined Variable

  • Select Set Pre-Defined Variable Function

Image

  • Select Custom Field
  • Input IsClientEnabled as Variable name
  • Select Enable Network Adapter Solution custom field from the dropdown
  • Click Save

Image

Row 2 Function: Set Pre-defined Variable

  • Select Set Pre-Defined Variable Function

Image

  • Select Custom Field
  • Input ClientAction as Variable name
  • Select Action(ENUM - CLIENT) custom field from the dropdown for Client
  • Click Save

Image

Row 3 Function: Set Pre-defined Variable

  • Select Set Pre-Defined Variable Function

Image

  • Select Custom Field
  • Input SiteAction as Variable name
  • Select Action(ENUM - SITE) custom field from the dropdown for Site
  • Click Save

Image

Row 4 Function: Set Pre-defined Variable

  • Select Set Pre-Defined Variable Function

Image

  • Select Custom Field
  • Input EndpointAction as Variable name
  • Select Action(ENUM - ENDPOINT) custom field from the dropdown for Endpoint
  • Click Save

Image

Row 5 Function: Set Pre-defined Variable

  • Select Set Pre-Defined Variable Function

Image

  • Select Custom Field
  • Input ClientProtocol as Variable name
  • Select Protocol(ENUM - CLIENT) custom field from the dropdown for Client
  • Click Save

Image

Row 6 Function: Set Pre-defined Variable

  • Select Set Pre-Defined Variable Function

Image

  • Select Custom Field
  • Input SiteProtocol as Variable name
  • Select Protocol(ENUM - SITE) custom field from the dropdown for Site
  • Click Save

Image

Row 7 Function: Set Pre-defined Variable

  • Select Set Pre-Defined Variable Function

Image

  • Select Custom Field
  • Input EndpointProtocol as Variable name
  • Select Protocol(ENUM - ENDPOINT) custom field from the dropdown for Endpoint
  • Click Save

Image

Row 8 Function: PowerShell Script

Image

Paste in the following PowerShell script and set the expected script execution time to 900 seconds.

if ('@IsClientEnabled@' -notmatch '1|Yes|True|Y') {
return 'Client is not opted for Network Adapter Solution'
}

# Resolve action
$ResolvedAction = 'Not Set'
if ('@EndpointAction@' -match 'ENABLE|DISABLE|Enable DHCP|Exclude from Solution') {
$ResolvedAction = '@EndpointAction@'
}
elseif ('@SiteAction@' -match 'ENABLE|DISABLE|Enable DHCP|Exclude from Solution') {
$ResolvedAction = '@SiteAction@'
}
elseif ('@ClientAction@' -match 'ENABLE|DISABLE|Enable DHCP') {
$ResolvedAction = '@ClientAction@'
}

if ($ResolvedAction -match 'Not Set|Exclude from Solution') {
return 'Please verify that the Action is defined properly or solution is enabled for the machine, then reattempt executing the Task'
}

# Resolve protocol
$ResolvedProtocol = 'Not Set'
if ('@EndpointProtocol@' -match 'IPv4|IPv6|Both') {
$ResolvedProtocol = '@EndpointProtocol@'
}
elseif ('@SiteProtocol@' -match 'IPv4|IPv6|Both') {
$ResolvedProtocol = '@SiteProtocol@'
}
elseif ('@ClientProtocol@' -match 'IPv4|IPv6|Both') {
$ResolvedProtocol = '@ClientProtocol@'
}

if ($ResolvedProtocol -match 'Not Set') {
return 'Please verify that the Protocol is properly defined for the machine, then reattempt executing the Task'
}

$Action = $ResolvedAction
$Protocol = $ResolvedProtocol

function Set-NetworkAdapterIP {
param (
[string]$InterfaceAlias,
[string]$Protocol,
[string]$Action
)

$ComponentID = if ($Protocol -eq "IPv6") { "ms_tcpip6" } else { "ms_tcpip" }

if ($Action -eq "DISABLE") {
Disable-NetAdapterBinding -Name $InterfaceAlias -ComponentID $ComponentID
$binding = Get-NetAdapterBinding -Name $InterfaceAlias | Where-Object { $_.ComponentID -eq $ComponentID }
if ($binding.Enabled -eq $false) {
Write-Output "$Protocol successfully disabled on $InterfaceAlias."
} else {
Write-Error "Failed to disable $Protocol on $InterfaceAlias."
}
}
elseif ($Action -eq "ENABLE") {
Enable-NetAdapterBinding -Name $InterfaceAlias -ComponentID $ComponentID
$binding = Get-NetAdapterBinding -Name $InterfaceAlias | Where-Object { $_.ComponentID -eq $ComponentID }
if ($binding.Enabled -eq $true) {
Write-Output "$Protocol successfully enabled on $InterfaceAlias."
} else {
Write-Error "Failed to enable $Protocol on $InterfaceAlias."
}
}
elseif ($Action -eq "Enable DHCP") {
Enable-NetAdapterBinding -Name $InterfaceAlias -ComponentID $ComponentID

if ($Protocol -eq "IPv4") {
Set-NetIPInterface -InterfaceAlias $InterfaceAlias -Dhcp Enabled
Remove-NetIPAddress -InterfaceAlias $InterfaceAlias -AddressFamily IPv4 -Confirm:$false -ErrorAction SilentlyContinue
}
elseif ($Protocol -eq "IPv6") {
Set-NetIPInterface -InterfaceAlias $InterfaceAlias -AddressFamily IPv6 -Dhcp Enabled
Remove-NetIPAddress -InterfaceAlias $InterfaceAlias -AddressFamily IPv6 -Confirm:$false -ErrorAction SilentlyContinue
}

$dhcpStatus = Get-NetIPInterface -InterfaceAlias $InterfaceAlias -AddressFamily $Protocol
if ($dhcpStatus.Dhcp -eq "Enabled") {
Write-Output "$Protocol DHCP successfully enabled on $InterfaceAlias."
} else {
Write-Error "Failed to enable DHCP for $Protocol on $InterfaceAlias."
}
}
}

$adapters = Get-NetAdapter

foreach ($adapter in $adapters) {
if ($Protocol -eq "Both") {
if ($Action -eq "DISABLE") {
Write-Error "Invalid selection: Cannot disable both IPv4 and IPv6."
exit 1
}
Set-NetworkAdapterIP -InterfaceAlias $adapter.Name -Protocol "IPv4" -Action $Action
Set-NetworkAdapterIP -InterfaceAlias $adapter.Name -Protocol "IPv6" -Action $Action
}
elseif ($Protocol -eq "IPv4") {
Set-NetworkAdapterIP -InterfaceAlias $adapter.Name -Protocol "IPv4" -Action $Action
if ($Action -eq "DISABLE") {
Set-NetworkAdapterIP -InterfaceAlias $adapter.Name -Protocol "IPv6" -Action "ENABLE"
}
}
elseif ($Protocol -eq "IPv6") {
Set-NetworkAdapterIP -InterfaceAlias $adapter.Name -Protocol "IPv6" -Action $Action
if ($Action -eq "DISABLE") {
Set-NetworkAdapterIP -InterfaceAlias $adapter.Name -Protocol "IPv4" -Action "ENABLE"
}
}
}

Image

Row 9 Function: Script Log

Add a new row by clicking the Add Row button.

Add Row

A blank function will appear.

Blank Function

Search and select the Script Log function.

Script Log Selection

Script Log Selected

The following function will pop up on the screen:

Script Log Function

In the script log message, simply type %output% and click the Save button.

Script Log Save

Click the Save button at the top-right corner of the screen to save the script.

Save Script

Save Task

Click the Save button at the top-right corner of the screen to save the script.
SaveButton

Completed Task

Image

Deployment

This task has to be scheduled on Devices Opted for Network Adapter Solution group for auto deployment. The script can also be run manually if required.

  • Go to Automations > Tasks.
  • Search for Manage - Network Adapter Protocols.
  • Then click on Schedule from right side and provide the parameters detail as necessary for the script completion.
  • Select Targeted Resources as device group Devices Opted for Network Adapter Solution. Image

Output

  • Script Logs