From: Ruediger Roesler on 3 Apr 2010 10:41 M Shafaat <poppy2(a)comhem.se> typed: > I wish to disable and enable my Local Area Connection using scripts. > Can anyone suggest a very simple script or command line using netsh > etc for doing this? This script toggles all of your named network connections: '######################### SwitchNetConn.vbs ########################## Option Explicit Const AUTOR = " from h.r.roesler" ' Toggles the status of a named network connection. ' For exemple: ' SwitchNetConn LAN-Connection ' If the name contains spaces, surround it with double quotes: ' SwitchNetConn "Local Area Connection" ' Const TYP = 1, STATUS = 2, ADAPTER = 3 If WScript.Arguments.Count > 0 Then 'Switch GetCtrlFolder("Netzwerkverbindungen"), WScript.Arguments(0) Switch GetCtrlFolder("Network Connections"), WScript.Arguments(0) End If Function GetCtrlFolder(strFolder) Const ssfCONTROLS = 3 Dim xpShell, xpCtrl Set xpShell = CreateObject("Shell.Application") For Each xpCtrl In xpShell.NameSpace(ssfCONTROLS).Items If xpCtrl.IsFolder Then If StrComp(xpCtrl.Name, strFolder, vbTextCompare) = 0 Then Set GetCtrlFolder = xpCtrl.GetFolder Exit Function End If End If Next Err.Number = 53 Err.Description = "Virtual Folder '" & strFolder & _ "' was not found!" Err.Raise Err.Number End Function Function SelConnect(ByRef xpFolder, strConn) Dim xpItem, str For Each xpItem In xpFolder.Items If StrComp(xpItem.Name, strConn, vbTextCompare) = 0 Then Set SelConnect = xpItem Exit Function End If Next Err.Number = 448 Err.Description = "Network Connection '" & strConn & _ "' was not found!" Err.Raise Err.Number End Function Sub Switch(ByRef xpFolder, strConnName) 'Const ENABLE = "&Aktivieren", DISABLE = "&Deaktivieren" Const ENABLE = "En&able", DISABLE = "Disa&ble" Dim xpVerb, str, xpConnect Set xpConnect = SelConnect(xpFolder, strConnName) For Each xpVerb In xpConnect.Verbs If xpVerb.Name = ENABLE Xor xpVerb.Name = DISABLE Then str = xpFolder.GetDetailsOf(xpConnect, STATUS) xpVerb.DoIt Do WScript.Sleep 1000 Set xpConnect = SelConnect(xpFolder, xpConnect.Name) Loop Until str <> xpFolder.GetDetailsOf(xpConnect, STATUS) WScript.Echo GetDetailsOf(xpConnect, xpFolder) Exit Sub End If Next End Sub Function GetDetailsOf(ByRef xpItem, ByRef xpFolder) Dim str str = xpFolder.GetDetailsOf(xpItem, TYP) & " on " & vbCRLF str = str & xpFolder.GetDetailsOf(xpItem, ADAPTER) str = str & vbCRLF & String(79, "*") & vbCRLF str = WScript.ScriptName&AUTOR& vbCRLF & vbCRLF & str str = str & "Status of '" & xpItem.Name & "' at " str = str & FormatDateTime(Date, vbLongDate) & ":" & vbCRLF GetDetailsOf = str & vbCRLF & xpFolder.GetDetailsOf(xpItem, STATUS) End Function -- ЯR
From: Ruediger Roesler on 3 Apr 2010 13:03 Me typed: > Sub Switch(ByRef xpFolder, strConnName) > 'Const ENABLE = "&Aktivieren", DISABLE = "&Deaktivieren" > Const ENABLE = "En&able", DISABLE = "Disa&ble" > Dim xpVerb, str, xpConnect > > Set xpConnect = SelConnect(xpFolder, strConnName) > For Each xpVerb In xpConnect.Verbs > If xpVerb.Name = ENABLE Xor xpVerb.Name = DISABLE Then > str = xpFolder.GetDetailsOf(xpConnect, STATUS) > xpVerb.DoIt > Do > WScript.Sleep 1000 > Set xpConnect = SelConnect(xpFolder, xpConnect.Name) > Loop Until str <> xpFolder.GetDetailsOf(xpConnect, STATUS) > WScript.Echo GetDetailsOf(xpConnect, xpFolder) Const CONFIG = 6 WScript.Echo xpFolder.GetDetailsOf(xpConnect, CONFIG) > Exit Sub > End If > Next > End Sub If you insert the above two lines into the script, you will get the configuration for the network adapter if it will be activated, e.g. the IP-number, the subnet mask and how it was assigned. The output then looks alike to this: SwitchNetConn.vbs from h.r.roesler LAN or High Speed Internet on Atheros Gigabit Ethernet 10/100/1000Base-T Controller ******************************************************** Status of 'LAN-Verbindung' at Saturday, April 3, 2010: Connection established, with Firewall IP-Address: 192.168.100.250 Subnet mask: 255.255.255.0 Manually configured -- ЯR
From: Ruediger Roesler on 5 Apr 2010 12:36
M Shafaat <poppy2(a)comhem.se> typed: > I wish to disable and enable my Local Area Connection using scripts. > Can anyone suggest a very simple script or command line using netsh > etc for doing this? I have something worked about the script. The former script does not work in any case, therefore this is a new one which worked in any case, I guess: :-) '######################### SwitchNetConn.vbs ########################## Option Explicit Const AUTOR = " from h.r.roesler" ' Toggles the status of a named network connection. ' For exemple: ' SwitchNetConn LAN-Connection ' If the name contains spaces, surround it with double quotes: ' SwitchNetConn "Local Area Connection" ' This script can be executed only with CScript as host. ' Recommended OS is Windows XP SP3 Const TYP = 1, STATUS = 2, ADAPTER = 3, CONFIG = 6, CONNECTED = 2 Dim wmi WScript.StdOut.WriteLine vbCRLF & WScript.ScriptName & AUTOR & vbCRLF If WScript.Arguments.Count > 0 Then Set wmi = GetObject("winmgmts:!root/cimv2") 'Switch GetCtrlFolder("Netzwerkverbindungen"), WScript.Arguments(0) Switch GetCtrlFolder("Network Connections"), WScript.Arguments(0) End If Function GetCtrlFolder(strFolder) Const ssfCONTROLS = 3 Dim xpShell, xpCtrl Set xpShell = CreateObject("Shell.Application") For Each xpCtrl In xpShell.NameSpace(ssfCONTROLS).Items If xpCtrl.IsFolder Then If StrComp(xpCtrl.Name, strFolder, vbTextCompare) = 0 Then Set GetCtrlFolder = xpCtrl.GetFolder Exit Function End If End If Next Err.Number = 53 Err.Description = "Virtual Folder '" & strFolder & _ "' was not found!" Err.Raise Err.Number End Function Function SelConnect(ByRef xpFolder, strConn) Dim xpItem, str For Each xpItem In xpFolder.Items If StrComp(xpItem.Name, strConn, vbTextCompare) = 0 Then Set SelConnect = xpItem Exit Function End If Next Err.Number = 448 Err.Description = "Network Connection '" & strConn & _ "' was not found!" Err.Raise Err.Number End Function Sub Switch(ByRef xpFolder, strConnName) 'Const ENABLE = "&Aktivieren", DISABLE = "&Deaktivieren" 'Const ESTA = "Verbindung hergestellt", DEAK = "Deaktiviert" Const ENABLE = "En&able", DISABLE = "Disa&ble" Const ESTA = "Connected", DEAK = "Disabled" Dim xpVerb, str, xpConnect, i Set xpConnect = SelConnect(xpFolder, strConnName) For Each xpVerb In xpConnect.Verbs If xpVerb.Name = ENABLE Xor xpVerb.Name = DISABLE Then str = xpFolder.GetDetailsOf(xpConnect, STATUS) xpVerb.DoIt Do WScript.Sleep 500: i = i + 1 Set xpConnect = SelConnect(xpFolder, xpConnect.Name) If str <> xpFolder.GetDetailsOf(xpConnect, STATUS) Then str = xpFolder.GetDetailsOf(xpConnect, STATUS) WScript.Echo str End If If xpVerb.Name = ENABLE Then If InStr(1, str, ESTA, vbTextcompare) > 0 Then Exit Do End If ElseIf InStr(1, str, DEAK, vbTextcompare) > 0 Then Exit Do End If Loop While i <= 180 WScript.Echo GetDetailsOf(xpConnect, xpFolder) If xpVerb.Name = ENABLE Then If Not(CheckIP(xpConnect.Name)) Then Switch xpFolder, strConnName WScript.Sleep 20000 Switch xpFolder, strConnName Exit Sub End If End If Exit For End If Next End Sub Function GetDetailsOf(ByRef xpItem, ByRef xpFolder) Dim str str = vbCRLF & xpFolder.GetDetailsOf(xpItem, TYP) & " on " & vbCRLF str = str & xpFolder.GetDetailsOf(xpItem, ADAPTER) str = str & vbCRLF & String(79, "*") & vbCRLF str = str & "Status of '" & xpItem.Name & "' at " str = str & FormatDateTime(Date, vbLongDate) & ":" & vbCRLF str = str & vbCRLF & xpFolder.GetDetailsOf(xpItem, STATUS) GetDetailsOf = str & vbCRLF & xpFolder.GetDetailsOf(xpItem, CONFIG) End Function Function CheckIP(strConnection) Const NIC = "Win32_NetworkAdapter", SETT = "Setting" Dim wmiNIC, str, arr arr = Array("Disconnected", "Connecting", "Connected", "Discon" & _ "necting", "Hardware not present", "Hardware disab" & _ "led", "Hardware malfunction", "Media disconnected", _ "Authenticating", "Authentication succeeded", _ "Authentication failed", "Invalid address", _ "Credentials required") str = "select * from " & NIC & " where NetConnectionID = '" & _ strConnection & "'" For Each wmiNIC In wmi.ExecQuery(str) WScript.Echo vbCRLF & " " & wmiNIC.NetConnectionID & _ ": " & arr(wmiNIC.NetConnectionStatus) If wmiNIC.NetConnectionStatus <> CONNECTED Then Exit For str = "Associators of {" & NIC & ".DeviceID='" & _ wmiNIC.DeviceID & "'} where AssocClass = " & NIC & SETT CheckIP = CheckGateway(str) Next End Function Function CheckGateway(strConnection) Dim wmiNIC, i, x, blnSuccess Do For Each wmiNIC In wmi.ExecQuery(strConnection) blnSuccess = True If IsNull(wmiNIC.DefaultIPGateway) Then Exit For For i = 0 To UBound(wmiNIC.DefaultIPGateway) x = 0 WScript.StdOut.Write "Checking Gateway " Do Until Ping(wmiNIC.DefaultIPGateway(i)) If x >= 10 Then WScript.Echo "Gateway isn't reachable." blnSuccess = False Exit Do End If x = x + 1 WScript.Sleep 1000 Loop Exit Do Next Next WScript.Sleep 1000 x = x + 1 Loop While x < 60 CheckGateway = blnSuccess End Function Function Ping(ByRef strIPNum) Dim wmiPing, blnPong For Each wmiPing In wmi.ExecQuery("select * from " & _ "Win32_PingStatus where Address = '" & strIPNum & "'") If IsNull(wmiPing.StatusCode) Then Exit For blnPong = CBool(wmiPing.StatusCode = 0) WScript.Echo strIPNum & ": " & blnPong If Not(blnPong) Then WScript.Echo GetError(wmiPing.StatusCode) End If Next Ping = blnPong End Function Function GetError(intNumber) Dim arr arr = Array("Success", "Buffer Too Small", "Destination Net " & _ "Unreachable", "Destination Host Unreachable", _ "Destination Protocol Unreachable", "Destination Port " & _ "Unreachable", "No Resources", "Bad Option", "Hardware " & _ "Error", "Packet Too Big", "Request Timed Out", _ "Bad Request", "Bad Route", "TimeToLive Expired Transit", _ "TimeToLive Expired Reassembly", "Parameter Problem", _ "Source Quench", "Option Too Big", "Bad Destination") ReDim Preserve arr(50) arr(32) = "Negotiating IPSEC": arr(50) = "General Failure" GetError = vbCRLF & "ICMP-Error " & intNumber & ": " & _ arr(intNumber - 11000) & vbCRLF End Function '######################### SwitchNetConn.vbs ########################## -- ЯR |