# Script Error: Error setting profile for interface Primary LAN Interface:

**URL:** <https://forums.powershell.org/t/script-error-error-setting-profile-for-interface-primary-lan-interface/26114>\
**Category:** PowerShell Help\
**Created:** [August 19, 2025, 11:01am UTC](https://forums.powershell.org/t/script-error-error-setting-profile-for-interface-primary-lan-interface/26114 "2025-08-19T11:01:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tomcat456](https://avatars.discourse-cdn.com/v4/letter/t/9e8a1a/32.png) [@Tomcat456](https://forums.powershell.org/u/Tomcat456)\
**Post date:** [August 19, 2025, 11:01am UTC](https://forums.powershell.org/t/script-error-error-setting-profile-for-interface-primary-lan-interface/26114/1 "2025-08-19T11:01:50Z")

</div>

Hi all

I’m getting this error running this script:

Error setting profile for interface Primary LAN Interface:

The network connection profile is corrupted.

Reseting the network connection or restoring the VM doesn’t help. The problem is in the script.  
Any advice?

Thanks very much!

# 

# -------------------- STEP 5: Configure 802.1X NAC with EAP (Certificate) --------------------

Write-Host “`n=== Configuring 802.1X NAC on interface: $interfaceName ===”

# Enable 802.1X Authentication

netsh lan set profileparameter name=“$interfaceName” authentication=enabled | Out-Null  
netsh lan set profileparameter name=“$interfaceName” authmode=machine | Out-Null

# ================================

# 802.1X PEAP (EAP-TLS) for Wired

# Interface: “Primary LAN Interface”

# Root CA can be selected by Thumbprint or Subject Match

# Run as Administrator

# ================================

$InterfaceName = “Primary LAN Interface” # Target NIC display name  
$ProfileName = “Network” # Wired profile name to manage  
$TempFolder = “D:\iltis\tools\Temp\LanProfile”

# Root CA selection (choose ONE approach)

$RootCAThumbprint = “96f2bf58b39db8b704a4dda8c5df456c725fce24” # e.g. “AB12CD34EF56…”; leave blank to use subject search  
$RootCASubjectLike = “Root” # Used only if $RootCAThumbprint is blank

# ---------- Helpers ----------

function Ensure-Folder($path) {  
if (-not (Test-Path $path)) { New-Item -ItemType Directory -Path $path | Out-Null }  
}

function Get-RootCAThumbprint {  
param(  
[string]$Thumbprint,  
[string]$SubjectLike  
)  
if ($Thumbprint -and $Thumbprint.Trim() -ne “”) {  
return ($Thumbprint -replace “\s”,“”).ToUpper()  
}

```auto
$match = Get-ChildItem Cert:\LocalMachine\Root |
    Where-Object { $_.Subject -like "*$SubjectLike*" } |
    Select-Object -First 1

if (-not $match) { throw "Root CA with subject like '$SubjectLike' not found in LocalMachine\Root." }
return ($match.Thumbprint -replace "\s","").ToUpper()

```

}

function Start-WiredAutoConfig {  
$svc = Get-Service -Name dot3svc -ErrorAction SilentlyContinue  
if (-not $svc) { throw “Wired AutoConfig (dot3svc) service not found.” }  
if ($svc.StartType -ne ‘Automatic’) { Set-Service dot3svc -StartupType Automatic }  
if ($svc.Status -ne ‘Running’) { Start-Service dot3svc }  
}

function Get-NicOrThrow {  
param([string]$Name)  
$nic = Get-NetAdapter -Name $Name -ErrorAction SilentlyContinue  
if (-not $nic) { throw “Network adapter ‘$Name’ not found. Use Get-NetAdapter to confirm the exact name.” }  
if ($nic.Status -ne ‘Up’) { Write-Warning “Adapter ‘$Name’ is not Up (status: $($nic.Status)). Continuing anyway.” }  
return $nic  
}

# Minimal valid LAN profile XML with EAPHostConfig placeholder (we’ll inject the Root CA thumbprint).

function New-LanProfileXml {  
param([string]$ProfName)  
@"

\<?xml version="1.0"?\> $ProfName userOrComputer 25 0 13 true true \_\_ROOT\_CA\_THUMBPRINT\_\_ false "@ }
# ---------- Main ----------

try {  
Write-Host “Preparing environment…” -ForegroundColor Cyan  
Ensure-Folder $TempFolder  
Start-WiredAutoConfig  
$null = Get-NicOrThrow -Name $InterfaceName

```auto
$thumb = Get-RootCAThumbprint -Thumbprint $RootCAThumbprint -SubjectLike $RootCASubjectLike
Write-Host "Using Root CA Thumbprint: $thumb" -ForegroundColor Green

# Try to export existing profile for this interface+name; if missing, build a fresh one.
$exported = $false
Write-Host "Exporting existing wired profile (if present)..." -ForegroundColor Cyan
$null = netsh lan export profile folder="$TempFolder" interface="$InterfaceName" name="$ProfileName" 2>$null

$ProfilePath = Join-Path $TempFolder "$ProfileName.xml"
if (Test-Path $ProfilePath) { $exported = $true }

if (-not $exported) {
    Write-Host "No existing profile named '$ProfileName' found. Creating a new one..." -ForegroundColor Yellow
    $xmlText = New-LanProfileXml -ProfName $ProfileName
    $xmlText = $xmlText -replace " __ROOT_CA_THUMBPRINT__", $thumb
    $xmlText | Set-Content -Path $ProfilePath -Encoding UTF8
} else {
    # Load existing, replace EAP host config with our desired PEAP->EAP-TLS block
    [xml]$xml = Get-Content $ProfilePath
    # Find any EapHostConfig node and replace its InnerXml with our config (PEAP 25 -> inner EAP-TLS 13)
    $ns = New-Object System.Xml.XmlNamespaceManager($xml.NameTable)
    $ns.AddNamespace("lp", "http://www.microsoft.com/networking/LAN/profile/v1")
    $ns.AddNamespace("ox", "http://www.microsoft.com/networking/OneX/v1")
    $eapConfigNode = $xml.SelectSingleNode("//lp:LANProfile/lp:MSM/lp:security/ox:OneX/ox:EAPConfig", $ns)
    if (-not $eapConfigNode) { throw "EAPConfig node not found in exported profile. Cannot proceed safely." }

    $newEap = New-LanProfileXml -ProfName $ProfileName
    # extract just the EAPHostConfig from the template
    [xml]$tmp = $newEap
    $tmpNs = New-Object System.Xml.XmlNamespaceManager($tmp.NameTable)
    $tmpNs.AddNamespace("lp", "http://www.microsoft.com/networking/LAN/profile/v1")
    $tmpNs.AddNamespace("ox", "http://www.microsoft.com/networking/OneX/v1")
    $eapHostConfig = $tmp.SelectSingleNode("//lp:LANProfile/lp:MSM/lp:security/ox:OneX/ox:EAPConfig/*", $tmpNs)

    # Replace placeholder with real thumbprint
    $eapHostConfigOuterXml = $eapHostConfig.OuterXml.Replace(" __ROOT_CA_THUMBPRINT__", $thumb)

    # Replace EAPConfig contents
    $eapConfigNode.InnerXml = $eapHostConfigOuterXml

    # Save back
    $xml.Save($ProfilePath)
}

Write-Host "Importing wired 802.1X profile to '$InterfaceName'..." -ForegroundColor Cyan
# Adds the profile and associates it with the specified interface
netsh lan add profile filename="$ProfilePath" interface="$InterfaceName"

# Optional: force 802.1X reauth/reconnect
try { netsh lan reconnect interface="$InterfaceName" | Out-Null } catch {}

Write-Host ""
Write-Host "✅ Done." -ForegroundColor Green
Write-Host "The adapter '$InterfaceName' is configured for PEAP with inner 'Smart Card or other certificate' (EAP-TLS) and trusts the specified Root CA." -ForegroundColor Green
Write-Host "Profile name: $ProfileName"
Write-Host ""
Write-Host "Tip: If your NAC expects machine auth, make sure a valid machine certificate is present in LocalMachine\My." -ForegroundColor DarkGray

```

}  
catch {  
Write-Error $\_.Exception.Message  
exit 1  
}

---

<div class="post-metadata">

**Author:** ![Tomcat456](https://avatars.discourse-cdn.com/v4/letter/t/9e8a1a/32.png) [@Tomcat456](https://forums.powershell.org/u/Tomcat456)\
**Post date:** [August 19, 2025, 11:05am UTC](https://forums.powershell.org/t/script-error-error-setting-profile-for-interface-primary-lan-interface/26114/2 "2025-08-19T11:05:58Z")

</div>

The fault in the Script is from “Step 5” and below.

---

<div class="post-metadata">

**Author:** ![Tomcat456](https://avatars.discourse-cdn.com/v4/letter/t/9e8a1a/32.png) [@Tomcat456](https://forums.powershell.org/u/Tomcat456)\
**Post date:** [August 19, 2025, 12:10pm UTC](https://forums.powershell.org/t/script-error-error-setting-profile-for-interface-primary-lan-interface/26114/3 "2025-08-19T12:10:49Z")

</div>

The computers are in no Domain, so GPO is no option

---

<div class="post-metadata">

**Author:** ![psdarwin](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/psdarwin/32/2063_2.png) [@psdarwin](https://forums.powershell.org/u/psdarwin)\
**Post date:** [August 19, 2025, 1:28pm UTC](https://forums.powershell.org/t/script-error-error-setting-profile-for-interface-primary-lan-interface/26114/4 "2025-08-19T13:28:56Z")

</div>

That is a long block of code - could you edit your original post to insert as preformatted text? That option exists under the + icon in the message editor. It really helps us more easily read your code and thus assist you

---

<div class="post-metadata">

**Author:** ![psdarwin](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/psdarwin/32/2063_2.png) [@psdarwin](https://forums.powershell.org/u/psdarwin)\
**Post date:** [August 19, 2025, 2:16pm UTC](https://forums.powershell.org/t/script-error-error-setting-profile-for-interface-primary-lan-interface/26114/5 "2025-08-19T14:16:51Z")

</div>

You might consider looking into using native PowerShell commands instead of netsh commands - that could give you better feedback on what’s happening.

> **[Basic Networking PowerShell cmdlets cheatsheet to replace netsh, ipconfig,...](https://www.thomasmaurer.ch/2016/02/basic-networking-powershell-cmdlets-cheatsheet-to-replace-netsh-ipconfig-nslookup-and-more/)**
>
> Around 4 years ago I wrote a blog post about how to Replace netsh with Windows PowerShell which includes basic powershell networking cmdlets. After working with Microsoft Azure, Nano Server and Containers, PowerShell together with networking becomes...

---

<div class="post-metadata">

**Author:** ![Tomcat456](https://avatars.discourse-cdn.com/v4/letter/t/9e8a1a/32.png) [@Tomcat456](https://forums.powershell.org/u/Tomcat456)\
**Post date:** [August 19, 2025, 2:19pm UTC](https://forums.powershell.org/t/script-error-error-setting-profile-for-interface-primary-lan-interface/26114/6 "2025-08-19T14:19:26Z")

</div>

According Chatgpt there are no native Powershell Commands for these settings

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [September 18, 2025, 2:20pm UTC](https://forums.powershell.org/t/script-error-error-setting-profile-for-interface-primary-lan-interface/26114/7 "2025-09-18T14:20:21Z")

</div>

Closed on September 18, 2025, 2:20pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
