![]() |
|
|
|||||||
| Petri.co.il is happy to award auglan the title of Most Valuable Member !!! |
| Register | Calendar |
Search |
Today's Posts |
Mark Forums Read |
| Notices |
|
|
PowerShell v2 script for disabling and enabling LAN connections when no ping reply.this thread has 4 replies and has been viewed 4994 times
|
![]() |
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||||||||
|
|||||||||
|
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
|
|||||||||
|
|||||||||
|
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
}
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 : Can anyone help me please ? |
|
#3
|
|||||||||
|
|||||||||
|
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()
}
|
|
#4
|
|||||||||
|
|||||||||
|
|
|
#5
|
|||||||||
|
|||||||||
|
Quote:
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
}
hope this helps. |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
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 |