locked
VB script for rejoining to domain? RRS feed

  • Question

  • Hi,

    when the LAN connection is switched to Wi-Fi or vice versa the domain has to be reconnected via this command "\\domain.com". I´ve found the script for prioritizing LAN over Wifi and I need to add to this script also reconnecting to domain when LAN is swiched to Wi-Fi or vice versa. Then I will distribute this script to our GPO. Thx a lot for your help!

    On Error Resume Next
     Dim strComputer
     Dim objWMIService
     Dim colLAN
     Dim objWifi,objLAN
     Dim state
     Dim wireStatus
     Dim wifiStatus
    
     state=""
     wireStatus=""
     wifiStatus=""
    
     Do While True
      
     strComputer = "." 
     Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
     Set colLAN = objWMIService.ExecQuery("Select * From Win32_NetworkAdapter Where NetConnectionID like 'Local Area Connection' and PhysicalAdapter='True'" )
     Set colWiFi=objWMIService.ExecQuery ("Select * From Win32_NetworkAdapter Where NetConnectionID =" & "'" &GetWirlessName & "'" & "and PhysicalAdapter='True' ")
     
     For Each objWifi In colWiFi
    	 If objWifi.Netconnectionstatus=2 Then
    	 wifiStatus=True
    	 Else
    	 wifiStatus=False
    	 End If
     Next
     
     For Each objLAN in colLAN
     
     If objLAN.Netconnectionstatus=2 Then
    	 wireStatus=True
    	 state=False ' this is very importnat variable to determine when to enable or disbale wireless connection   
    	 Else
    	 wireStatus=False
    	 End If
     Next
     
    
    If True Then
    	If state <>  False Then
    		 If wifiStatus = False Then
    		 EnableWireless GetWirlessName
    		 End If
    	Else 
    		If wifiStatus = True Then
    		DisableWireless GetWirlessName
    		End If
    	End If
    	 
     End If 
     
    
    state=""
    wireStatus=""
    wifiStatus=""
    
    WScript.Sleep  60000
    
    Loop
    
    
    ' Function to get wireless adapter name from the registery
    Function GetWirlessName 
    
    Dim strKeyPath
    Dim strComputer
    Dim objReg
    Dim arrSubKeys
    Dim SubKey
    Dim strValueName
    Dim dwValue
    Dim strValue
    Const HKLM=&H80000002
    
    
    strKeyPath="SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}"
    strComputer="."
    
    Set objReg=GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
    objReg.Enumkey HKLM ,strKeyPath,arrSubKeys
    
    For Each SubKey In arrSubKeys
    	strValueName="MediaSubType"
    	objReg.GetDWORDValue HKLM,strKeyPath & "\" & subkey & "\" & "Connection" ,strValueName,dwValue
    	If dwValue=2 Then
    		strValueName = "Name"
    		objReg.GetStringValue HKLM,strKeyPath & "\" & subkey & "\" & "Connection" ,strValueName,strValue
    		Exit For
    	End If
    Next
    
    GetWirlessName=strValue
    
    End Function
    
    
    
    ' Subroutine to disable wireless connection 
    Sub DisableWireless (strNetConn)
    
    Dim oConnections
    dim objShell
    Dim objConnections,objConn
    Dim strDisable
    Dim objNetwork
    Dim objDisable
    Dim objVerb
    
    Const NETWORK_CONNECTIONS = &H31&
    
    strDisable = "Disa&ble"
    
    Set objShell = CreateObject("Shell.Application")
    Set objConnections = objShell.Namespace(NETWORK_CONNECTIONS)
    
    For Each objConn In objConnections.Items
    	If objConn.Name = strNetConn Then
    		Set objNetwork = objConn
    		Exit For
    	End If
    Next
    Set objDisable = Nothing
    
    For Each objVerb in objNetwork.verbs
    	If objVerb.name = strDisable Then 
    		Set objDisable = objVerb 
    		Exit For
    	End If
    
    Next
    objDisable.DoIt
    WScript.Sleep 1000 
    End Sub
    
    'Function to enable wireless connection , you can combone these two subtoutines into one
    ' but I prefer to seperate them just for simplicity
    Sub EnableWireless (strNetConn)
    Dim oConnections
    dim objShell
    Dim objConnections,objConn
    Dim strEnable
    Dim objNetwork
    Dim objEnable
    Dim objVerb
    
    Const NETWORK_CONNECTIONS = &H31&
    
    strEnable = "En&able"
    
    Set objShell = CreateObject("Shell.Application")
    Set objConnections = objShell.Namespace(NETWORK_CONNECTIONS)
    
    
    For Each objConn In objConnections.Items
    If objConn.Name = strNetConn Then
    	Set objNetwork = objConn
    	Exit For
    End If
    Next
    Set objEnable = Nothing
    
    ' Enable NIC 
    For Each objVerb in objNetwork.verbs
    	If objVerb.name = strEnable Then 
    		Set objEnable = objVerb 
    		Exit For
    	End If
    
    Next
    
    objEnable.DoIt
    WScript.Sleep 1000 
    End Sub
    

    Monday, June 12, 2017 10:28 AM

Answers

  • This should work:

    CreateObject("Shell.Application").Explore("\\domain.com")


    \_(ツ)_/

    • Marked as answer by kovino Tuesday, June 13, 2017 2:53 PM
    Monday, June 12, 2017 12:34 PM

All replies

  • Is there a question or are you just sharing this script?


    \_(ツ)_/

    Monday, June 12, 2017 10:34 AM
  • Yes, the question is that how to type the VB command for "\\domain.com" and implement it to this script above. This script above is OK but I need to just add this command "\\domain.com" in VB language to it.
    Monday, June 12, 2017 10:44 AM
  • Why?  That is not a command that joins anything to a domain.  If you connect a machine to a network that is a domain the machine is either joined or not joined.  Typing that command will not cause a machine to join.

    I also don't understand what you mean by typing it.  If you just want to run that line then you need to execute this command:

    cmd /c \\domain.com


    \_(ツ)_/

    Monday, June 12, 2017 10:55 AM
  • In Windows I click Start-Run and put this \\domain.com and hit enter. (domain.com is just example, name of our domain is different)

    This I´d like to know how to write in VB language.

    Then I´d like to add this to the sript above so that whenever the ethernet or wi-fi adapter is disabled this command is executed.

    Monday, June 12, 2017 11:18 AM
  • Other than what I showed you above there is no way to write that in VBScript.  It is also unnecessary.  The computer will connect itself to the domain as soon as it is plugged in.


    \_(ツ)_/

    Monday, June 12, 2017 11:39 AM
  • I understand and appreciate your help. The thing is that we live in big corporate network where two different domains cooporate. Once the machine is switched from LAN to Wi-Fi or vice versa (dock-in and dock-out laptop) the internet access is filtered via FortiGuard and the user must manually execute this command \\domain.com to be able browse the internet again. Nobody knows at the headquarters how to solve this so that I´d like to do this at least locally in automatic way. 
    Monday, June 12, 2017 12:03 PM
  • shell.Run("\\domain.com")

    Running that on any machine seems to always produce an error.

    Running it from explorer causes Explorer to connect tot he root of the domain.

    You can try:

    shell.Run ("explorer \\domain.com")


    \_(ツ)_/


    • Edited by jrv Monday, June 12, 2017 12:09 PM
    Monday, June 12, 2017 12:06 PM
  • Instead of domain.com there will be name of our domain.

    Have you tested that command shell.Run("\\domain.com") and it produces an error?

    Monday, June 12, 2017 12:12 PM
  • Instead of domain.com there will be name of our domain.

    Have you tested that command shell.Run("\\domain.com") and it produces an error?

    As I noted above, \\domain.com is not a command and only work in explorer.  [Win-R] is an explorer command and not a shell command or, rather, it is a System shell executable string which is processed by Explorer.


    \_(ツ)_/

    Monday, June 12, 2017 12:28 PM
  • This should work:

    CreateObject("Shell.Application").Explore("\\domain.com")


    \_(ツ)_/

    • Marked as answer by kovino Tuesday, June 13, 2017 2:53 PM
    Monday, June 12, 2017 12:34 PM
  • OK, thank you very much. I will test it tomorrow and let you know.
    Monday, June 12, 2017 12:46 PM
  • jrv THANK YOU VERY MUCH!!!!

    You made my day! It´s working as I wished. The internet access is preserved all the time after ethernet/wifi adapater is activated/deactivated and also the priority is set for the ethernet when both connections are available (wifi is disabled back then).

    CreateObject("Shell.Application").Explorer("\\domain.com") - this is the right answer for my question!

    Tuesday, June 13, 2017 2:53 PM