Petri IT Knowledgebase Forums
 

Petri.co.il forums Home Forums Start Page Forums Frequently Asked Questions FAQ Member List Members List
Go Back   Petri IT Knowledgebase Forums > Windows Scripting > Powershell
Petri.co.il is happy to award auglan the title of Most Valuable Member !!!
Register Calendar Calendar Search Petri IT Knowledgebase Forums Search Todays Posts Today's Posts Mark Forums Read

Notices

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

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

this thread has 4 replies and has been viewed 4994 times

Closed Thread
 
Thread Tools Search this Thread Display Modes
  #1  
Old 28th February 2010, 15:50
Albertwt Albertwt is offline
Member
Someone to look up to
 
 Join Date: Feb 2008
  6 month star 12 month star
 Location: Sydney
 Posts: 644
 Reputation: Albertwt is on a distinguished road (18)
Question 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  
Old 1st March 2010, 15:36
Albertwt Albertwt is offline
Member
Someone to look up to
 
 Join Date: Feb 2008
  6 month star 12 month star
 Location: Sydney
 Posts: 644
 Reputation: Albertwt is on a distinguished road (18)
Question 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 ?
  #3  
Old 2nd March 2010, 07:06
Albertwt Albertwt is offline
Member
Someone to look up to
 
 Join Date: Feb 2008
  6 month star 12 month star
 Location: Sydney
 Posts: 644
 Reputation: Albertwt is on a distinguished road (18)
Question 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 Thumbnails
Click image for larger version

Name:	WMI.jpg
Views:	121
Size:	136.4 KB
ID:	3760  
  #4  
Old 7th March 2010, 09:30
vonPryz vonPryz is offline
Member
Here to help
 
 Join Date: Oct 2008
  6 month star 12 month star
 Location: Finland
 Posts: 354
 Reputation: vonPryz will become famous soon enough (80)
Default 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
  #5  
Old 12th March 2010, 01:44
Albertwt Albertwt is offline
Member
Someone to look up to
 
 Join Date: Feb 2008
  6 month star 12 month star
 Location: Sydney
 Posts: 644
 Reputation: Albertwt is on a distinguished road (18)
Wink Re: PowerShell v2 script for disabling and enabling LAN connections when no ping repl

Quote:
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 "admin@domain.com" -from "administrator@server.domain.com" -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.
Closed Thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Error while executing powershell 2.0 script Cute.Devil Exchange 2007 / 2010 / 2013 0 11th December 2009 16:51
Enabling and Disabling a few items via GPO jordanwpg GPO 8 25th August 2009 20:08
powershell script and CSV formatting s_anr Powershell 3 14th August 2009 10:58
Little powershell script that needs help :) XtaZee Powershell 4 25th June 2008 16:13
Wrong reply for ping request adham512 Windows Server 2000 / 2003 11 20th January 2007 20:37


All times are GMT +3. The time now is 05:53.

Steel Blue 3.5.4 vBulletin Style ©2006 vBEnhanced
Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
 

Valid XHTML 1.0!   Valid CSS!

Copyright 2005 Daniel Petri