Announcement

Collapse
No announcement yet.

PowerShell v2 script for disabling and enabling LAN connections when no ping reply.

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • PowerShell v2 script for disabling and enabling LAN connections when no ping reply.

    Hi All,

    I'd like to create a powershell script to disable and enable the current LAN connections when there is no network connection (ping reply) to gateway IP inside my WIndows Server 2003 VM, it should go like this:

    Ping the gateway IP address every 1 hour and when there is no ping reply then disable and enable the current active NIC connection ("Local Area Network Connection").

    but it has an error in it and i still don't know the logic or command to check the ping reply.

    Note: this is running on Windows Server 2003 x64 with Powershell V2

    can anyone help me to fix this please ?

    Thanks
    Code:
    ##################################################################################################################
    # This is how I execute
    ##################################################################################################################
    PS C:\> Set-ExecutionPolicy RemoteSigned
    PS C:\> C:\test.ps1
    Get-WmiObject : Invalid query
    At C:\test.ps1:7 char:26
    + $colItems = get-wmiobject  <<<< -class $class -namespace $namespace -computername $strComputer -filter $filter
    
    You cannot call a method on a null-valued expression.
    At C:\test.ps1:12 char:21
    +        $objItem.Disable( <<<< )
    
    You cannot call a method on a null-valued expression.
    At C:\test.ps1:20 char:20
    +        $objItem.Enable( <<<< )
    
    ##################################################################################################################
    # THE SCRIPT
    ##################################################################################################################
    
    $filter = "AdapterType = 'Ethernet 802.3' and NetEnabled = 'True'"
    $class = "Win32_NetworkAdapter"
    $namespace = "root\cimv2"
    $strComputer = "."
    
    # Find all Active ethernet 802.3 network cards
    $colItems = get-wmiobject -class $class -namespace $namespace -computername $strComputer -filter $filter
    
    # Disable  active network cards
    foreach ($objItem in $colItems) {
          write-host $objItem.Description
               $objItem.Disable()
    }
    
    Start-sleep 5
    
    # Enable active network cards
    foreach ($objItem in $colItems) {
          write-host $objItem.Description
               $objItem.Enable()
    }
    PS C:\>
    ##################################################################################################################

  • #2
    Re: PowerShell v2 script for disabling and enabling LAN connections when no ping repl

    OK,

    I've modified my script to be simpler like the following:
    Code:
    $colItems = Get-wmiobject win32_NetworkAdapter | where {$_.AdapterType -like "*Ethernet*" }
    
    foreach ( $objItems in $colItems ) {
        Write-Host $objItem.Description
        $objItems.Disable
    }
    
    Start-sleep 5
    
    # Enable active network cards
    foreach ($objItems in $colItems) {
        write-host $objItem.Description
        $objItems.Enable
    }
    and here's the results:
    Code:
    ServiceName      : b57nd60x
    MACAddress       : 00:22:64:4D:28:6F
    AdapterType      : Ethernet 802.3
    DeviceID         : 7
    Name             : Broadcom NetLink (TM) Gigabit Ethernet
    NetworkAddresses : 
    Speed            : 1000000000
    
    ServiceName      : NETw5s32
    MACAddress       : 00:16:EA:BC:4F:7A
    AdapterType      : Ethernet 802.3
    DeviceID         : 11
    Name             : Intel(R) WiFi Link 5100 AGN
    NetworkAddresses : 
    Speed            : 54000000
    
    ServiceName      : BthPan
    MACAddress       : 00:21:86:70:8F:41
    AdapterType      : Ethernet 802.3
    DeviceID         : 13
    Name             : Bluetooth Device (Personal Area Network)
    NetworkAddresses : 
    Speed            : 0
    
    ServiceName      : DNE
    MACAddress       : 00:16:EA:BC:4F:7A
    AdapterType      : Ethernet 802.3
    DeviceID         : 17
    Name             : Deterministic Network Enhancer Miniport
    NetworkAddresses : 
    Speed            : 
    
    ServiceName      : DNE
    MACAddress       : 00:22:64:4D:28:6F
    AdapterType      : Ethernet 802.3
    DeviceID         : 18
    Name             : Deterministic Network Enhancer Miniport
    NetworkAddresses : 
    Speed            : 
    
    ServiceName      : DNE
    MACAddress       : 4C:4C:20:52:41:53
    AdapterType      : Ethernet 802.3
    DeviceID         : 19
    Name             : Deterministic Network Enhancer Miniport
    NetworkAddresses : 
    Speed            : 
    
    ServiceName      : DNE
    MACAddress       : 4C:4C:20:52:41:53
    AdapterType      : Ethernet 802.3
    DeviceID         : 20
    Name             : Deterministic Network Enhancer Miniport
    NetworkAddresses : 
    Speed            : 
    
    ServiceName      : DNE
    MACAddress       : 4C:4C:20:52:41:53
    AdapterType      : Ethernet 802.3
    DeviceID         : 21
    Name             : Deterministic Network Enhancer Miniport
    NetworkAddresses : 
    Speed            : 
    
    ServiceName      : Teefer2
    MACAddress       : 00:16:EA:BC:4F:7A
    AdapterType      : Ethernet 802.3
    DeviceID         : 26
    Name             : Teefer2 Miniport
    NetworkAddresses : 
    Speed            : 
    
    ServiceName      : Teefer2
    MACAddress       : 00:22:64:4D:28:6F
    AdapterType      : Ethernet 802.3
    DeviceID         : 27
    Name             : Teefer2 Miniport
    NetworkAddresses : 
    Speed            : 
    
    ServiceName      : Teefer2
    MACAddress       : 4C:4C:20:52:41:53
    AdapterType      : Ethernet 802.3
    DeviceID         : 28
    Name             : Teefer2 Miniport
    NetworkAddresses : 
    Speed            : 
    
    ServiceName      : Teefer2
    MACAddress       : 4C:4C:20:52:41:53
    AdapterType      : Ethernet 802.3
    DeviceID         : 29
    Name             : Teefer2 Miniport
    NetworkAddresses : 
    Speed            : 
    
    ServiceName      : Teefer2
    MACAddress       : 4C:4C:20:52:41:53
    AdapterType      : Ethernet 802.3
    DeviceID         : 30
    Name             : Teefer2 Miniport
    NetworkAddresses : 
    Speed            :
    but when i try to ping mygatewayIP -t the connection is still up ? not gets dropped ?

    Can anyone help me please ?

    Comment


    • #3
      Re: PowerShell v2 script for disabling and enabling LAN connections when no ping repl

      even after I make the adapter selection to be more sophisticated (details)

      it is still failed too with the error:

      Code:
      Method invocation failed because [Microsoft.PowerShell.Commands.Internal.Format.FormatStartData] doesn't contain a method named 'Disable'.
      At :line:7 char:20
      +    $objItems.Disable <<<< ()
      Code:
      $colItems = Get-WmiObject Win32_NetworkAdapter -Filter "AdapterType='Ethernet 802.3'" | foreach {
              Get-WmiObject -Query "ASSOCIATORS OF {Win32_NetworkAdapter.DeviceId='$($_.DeviceID)'} WHERE ResultClass=Win32_NetworkAdapterConfiguration" | Format-List *
      }
      
      foreach ( $objItems in $colItems ) {
         Write-Host $objItem.Description
         $objItems.Disable()
      }
      
      Start-sleep 5
      
      # Enable active network cards
      foreach ($objItems in $colItems) {
         write-host $objItem.Description
         $objItems.Enable()
      }
      I even try to putthe brackets in to no avail see the screenshot
      Attached Files

      Comment


      • #4
        Re: PowerShell v2 script for disabling and enabling LAN connections when no ping repl

        Take a look at earlier thread. There is an example about how to get the ping results.

        -vP

        Comment


        • #5
          Re: PowerShell v2 script for disabling and enabling LAN connections when no ping repl

          Originally posted by vonPryz View Post
          Take a look at earlier thread. There is an example about how to get the ping results.

          -vP
          To all here's my final script that works:

          Code:
          while (!(Test-Connection gateway -Quiet -Count 1)) {
          	Get-WMIObject Win32_NetworkAdapter -Filter "NetConnectionID LIKE '%'" | %{
          		netsh interface set interface "$($_.NetConnectionID)" DISABLED
          		netsh interface set interface "$($_.NetConnectionID)" ENABLED
          	}
          	Send-MailMessage -to "[email protected]" -from "[email protected]" -subject "TS NIC problem recovery" -body "vNIC reset by powershell v2.0" -smtpServer mail.domain.com
          }
          this is using Powershell 2.0

          hope this helps.

          Comment

          Working...
          X