changing status virtual network adapter change service on disabled

gogi100 51 Reputation points
2020-12-17T06:45:16.957+00:00

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

my code is

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

this code does not works, i recevie

> Id Name PSJobTypeName State HasMoreData Location Command
— —- ————- —– ———– ——– ——-
1 d16f5266-065… NotStarted False UpNetwork
2 e51af08a-528… NotStarted False DownNetwork

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2020-12-17T08:40:49.517+00:00

    Hi,

    Please see if this works

    $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  
            }  
        }  
    }  
      
    Register-WMIEvent -Namespace root\wmi -Class MSNdis_StatusMediaConnect -Action $UpNetwork  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.