changing status virtual network adapter change service on disabled

Welcome Forums General PowerShell Q&A changing status virtual network adapter change service on disabled

Viewing 2 reply threads
  • Author
    Posts
    • #278658
      Participant
      Topics: 4
      Replies: 12
      Points: 18
      Rank: Member

      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?

    • #278796
      Participant
      Topics: 17
      Replies: 1951
      Points: 3,986
      Helping Hand
      Rank: Community Hero

      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:

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

      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:

      https://www.winhelponline.com/blog/trigger-launch-program-script-connecting-to-specific-network/
      Step-by-Step: How to Trigger an Email Alert from a Windows Event that Includes the Event Details using Windows Server 2016
      Trigger a PowerShell Script from a Windows Event

    • #279411
      Participant
      Topics: 4
      Replies: 12
      Points: 18
      Rank: Member

      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

Viewing 2 reply threads
  • You must be logged in to reply to this topic.