changing status virtual network adapter change service on disabled

i made script that check status cisco virtual adapter and if status ‘up’ service network list service is stopped and disabled, if status ‘disabled’ start network list service.

$up = "Up" $disabled = "Disabled" $lan = "Cisco Systems VPN Adapter for 64-bit Windows"

$landown = Get-NetAdapter | select interfacedescription,Status | where { $.Status -match $disabled -and $.interfacedescription -match $lan }
$lanUp = Get-NetAdapter | select interfacedescription,Status | where { $.Status -match $up -and $.interfacedescription -match $lan }

if ($lanUp)
{
Get-Service netprofm | Stop-Service -PassThru | Set-Service -StartupType disabled
}
elseif ($landown)
{
Get-Service netprofm | Start-Service -PassThru | Set-Service -StartupType Automatic
}

Exit


this script works ok when i start them manualy, but i want that the script is working automatically, when status virtual adapter changes from disabled to up automatic stops th network list service and disabling?

Two basic methods to trigger based on event, WMI and Windows Event. Get-NetAdapter uses WMI to get the underlying information, it’s basically a wrapper function. First you would have to translate the Get-NetAdapter to a WMI query:

Get-CimInstance -ClassName Win32_NetworkAdapter -Property Name, NetConnectionStatus | Select Name, NetConnectionStatus

If it’s in WMI, you should be able to create an event:

https://trevorsullivan.net/2009/11/16/powershell-getting-started-with-wmi-events/
https://stackoverflow.com/questions/45484159/powershell-how-to-register-an-event-on-network-connection-lost

Another choice would be a Windows Event Viewer event, which not sure what you are querying would cause and event, but here is some information if you want to take a look:

i used next code for registering wmievent

Function UpNetwork

{
$up = “Up”
$lan = “Cisco Systems VPN Adapter for 64-bit Windows”
$lanUp = Get-NetAdapter | select interfacedescription,Status | where { $.Status -match $up -and $.interfacedescription -match $lan }
$servicerunning = Get-Service netprofm | select status,starttype | where { $.Status -match $running -and $.starttype -match $manual }
if ($lanUp)
{
If($servicerunning)
{
Get-Service netprofm | Stop-Service -PassThru | Set-Service -StartupType disabled
}
}
}
Function DownNetwork

{
$disabled = “Disabled”
$lan = “Cisco Systems VPN Adapter for 64-bit Windows”
$landown = Get-NetAdapter | select interfacedescription,Status | where { $.Status -match $disabled -and $.interfacedescription -match $lan }
$servicestopped = Get-Service netprofm | select status,starttype | where { $_.starttype -match $disabled }

if ($landown)
{
if($servicestopped)
{
Get-Service netprofm | Set-Service -StartupType Manual | Start-Service
}
}
}

Register-WMIEvent -Namespace root\wmi -Class MSNdis_StatusMediaConnect -Action {UpNetwork}

Register-WMIEvent -Namespace root\wmi -Class MSNdis_StatusMediaDisconnect -Action {DownNetwork}
Exit


but i receive

Id Name PSJobTypeName State HasMoreData Location Command -- ---- ------------- ----- ----------- -------- ------- 1 d16f5266-065... NotStarted False UpNetwork 2 e51af08a-528... NotStarted False DownNetwork