locked
Need simple VB script to determine if staff member is connected to domain or working remotely from home RRS feed

  • Question

  • Hi Technet community. This is my first post so please go easy. I'm not good with scripts and the one we have is not working which I have been asked to fix. I did not create this script. We are in a windows domain environment using server 2012 R2 or newer, all client machines are using Windows 10 1809 or newer.

    I will dump the actual script below so you can all take a look. It's purpose is simple, when a staff member logs into the laptop the script determines if they are connected to the domain or if they are simply working from home by detecting their IP address. If they are successfully on the domain a check is done on their account and if their domain password is expiring within 14 days (expires every 90 days) a message box will displayed after they log in each morning reminding them that their account will expire soon in ‘x’ days and to consider changing their password. If they are not on the domain, the script quietly expires at that point.

    I’ve looked at the script and found the problem on line 35,

    “If left (sIPAddress,5) = "10.0." Then”

    That line is being used to verify if the computer is connected to our corp network (on a 10.0.x.x subnet) which is assuming no one is getting a 10.0.x.x IP address from their home network.  The problem is that a 10.0.x.x address is a very common IP range for a home router. So because the computer gets a match on that address but its not actually on the corp network it displays an error message after the staff member logs in, “The specified domain either does not exist or could not be contacted” which is annoying staff every time they want to work remotely. I'm hoping someone from Technet can suggest what argument I can use to replace the checking of the IP address to determine if they are on the domain or not?

    NOTE: there may be some additional arguments in the text below to disable the internet proxy, please ignore them as this script was originally intended to disable the proxy server option in the LAN settings of internet explorer however we no longer use or need this part of the script.

    Here is the script....

    ''''''''''''''''''''''''''''''''''''''
    
    'Script author: Domain Admin
    
    'Script name: PasswordExpireReminder.vbs
    
    'Date: 02/05/2015
    
    'Version:01
    
    ''''''''''''''''''''''''''''''''''''''
    
    '"Wait until the user is logged on before we start
    
    ''''''''''''''''''''''''''''''''''''''
    
    Dim WSHNetwork, strUserName, noproxycfg, proxycfg, net
    
    Set WSHNetwork = WScript.CreateObject("WScript.Network")
    
    ' Wait until the user is really logged in...
    
    strUserName = ""
    
    While strUserName = ""
    
    WScript.Sleep 10000 ' 10 seconds
    
    strUserName = WSHNetwork.UserName
    
    Wend
    
    strUserDomain = WSHNetwork.UserDomain
    
    'get ip address
    
    Dim refWMI,sIPAddress,colNetworkAdapters,oNetworkAdapter, WshtNetwork
    
    Set refWMI = GetObject("WinMgmts:\\.\root\cimv2")
    
    Set colNetworkAdapters = refWMI.ExecQuery("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled=True")
    
    For Each oNetworkAdapter in colNetworkAdapters
    
         If oNetworkAdapter.IPAddress(0) <> "0.0.0.0" Then
    
              sIPAddress = oNetworkAdapter.IPAddress(0)
    
              Exit For
    
         End If
    
    Next
    
    'Wscript.Echo sIPAddress
    
    ''''''''''''''''''''''''''''''''''''''
    
    '"This is a Domain issued IP address"
    
    ''''''''''''''''''''''''''''''''''''''
    
    If left (sIPAddress,5) = "10.0." Then
    
    Set ShellObj = WScript.CreateObject("WScript.Shell")
    
        '========================================
    
         ' First, get the domain policy.
    
         '========================================
    
         Dim oDomain
    
         Dim oUser
    
         Dim maxPwdAge
    
         Dim numDays
    
         Dim warningDays
    
     
    
        warningDays = 14
    
       
    
        Set LoginInfo = CreateObject("ADSystemInfo") 
    
        Set objUser = GetObject("LDAP://" & LoginInfo.UserName & "") 
    
        strDomainDN = UCase(LoginInfo.DomainDNSName)
    
        strUserDN = LoginInfo.UserName
    
     
    
       
    
        Set oDomain = GetObject("LDAP://" & strDomainDN)
    
         Set maxPwdAge = oDomain.Get("maxPwdAge")
    
     
    
        '========================================
    
         ' Calculate the number of days that are
    
         ' held in this value.
    
         '========================================
    
         numDays = CCur((maxPwdAge.HighPart * 2 ^ 32) + _
    
                         maxPwdAge.LowPart) / CCur(-864000000000)
    
         'WScript.Echo "Maximum Password Age: " & numDays
    
        
    
        '========================================
    
         ' Determine the last time that the user
    
         ' changed his or her password.
    
         '========================================
    
         Set oUser = GetObject("LDAP://" & strUserDN)
    
     
    
        '========================================
    
         ' Add the number of days to the last time
    
         ' the password was set.
    
         '========================================
    
         whenPasswordExpires = DateAdd("d", numDays, oUser.PasswordLastChanged)
    
         fromDate = Date
    
         daysLeft = DateDiff("d",fromDate,whenPasswordExpires)
    
        
    
        'WScript.Echo "Password Last Changed: " & oUser.PasswordLastChanged
    
     
    
        if (daysLeft < warningDays) and (daysLeft > -1) then
    
             Msgbox "Password Expires in " & daysLeft & " day(s)" & " at " & whenPasswordExpires & chr(13) & chr(13) & "Once logged in, press CTRL-ALT-DEL and" & chr(13) & "select the 'Change a password' option. If you require IT assistance please call ext 6514", 0, "PASSWORD EXPIRATION WARNING!"
    
         End if
    
     
    
        '========================================
    
         ' Clean up.
    
         '========================================
    
         Set oUser = Nothing
    
         Set maxPwdAge = Nothing
    
         Set oDomain = Nothing
    
                    ''''''''''''''''''''''''''''''''''''''
    
                    '"Address not known"
    
                    ''''''''''''''''''''''''''''''''''''''
    
                    Else
    
                    Set ShellObj = WScript.CreateObject("WScript.Shell")
    
                    ShellObj.RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable", 0, "REG_DWORD"
    
                    End If





    • Edited by Camopop Wednesday, January 8, 2020 3:07 AM
    Tuesday, January 7, 2020 11:44 PM

All replies

  • Please edit your original post and fix the code according to the following link:

    As posted your code is unreadable in most browsers and cannot be copied. Post code that is correctly formatted and not double or triple spaced.


    \_(ツ)_/


    • Edited by jrv Wednesday, January 8, 2020 12:00 AM
    Tuesday, January 7, 2020 11:59 PM
  • no worries, have followed instructions and added code into code block.
    Wednesday, January 8, 2020 3:08 AM
  • ...  have followed instructions and added code into code block.
    You followed half of the instructions. ;-)  Why do you have an empty line after each and every single code line?

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Wednesday, January 8, 2020 7:30 AM
  • no worries, have followed instructions and added code into code block.

    Please format code correctly.

    Also note that we do not fix code that you have copied from the Internet.  Please contact the author of the code for help with your issue.


    \_(ツ)_/

    Wednesday, January 8, 2020 8:28 AM
  • A quick scan of the script tells me that the author of the script did not do what you say. The script does not detect anything useful and we cannot customize this or write a custom script for you.

    Your original question an be answered in one line of PowerShell code.

    if $env:USERDOMAIN -eq 'MYCORPORATEDOMAIN'){
          # connected to corporate domain
    }else{
         # not connected t corporate domain
    }


    \_(ツ)_/

    Wednesday, January 8, 2020 8:53 AM
  • I simply copied the code from the vb script from the server, there were empty lines but not between every line it must have copied that way. I also selected powershell as the language type but it's a vb script so maybe it was formatted that way I'm not sure. I haven't copied this code from the internet either mate, it's a local script that's located on the C: drive of all 125 staff machines in our network. As i said I'm not good with scripts that's why I'm reaching out for help. Thanks for your recommendation of using a PS command instead, I will put that on a test machine and see what happens when logging on via lan cable (on domain network) vs via tethered to a mobile phone (off the domain) 
    Thursday, January 9, 2020 11:15 PM
  • We don't fix code that you have found. If you didn't write the code then you will need to cont6act the author for help. If you don't know how to edit the code to make it readable then you should be contacting a consultant for help and not asking us to fix code you didn't write.

    The code is a mess and was written by someone who has little or no knowledge of scripting or with Windows technology.  It appears to be a bunch of unrelated pieces of code pasted together.


    \_(ツ)_/

    Thursday, January 9, 2020 11:51 PM