Results 1 to 3 of 3

Thread: VBScript to join domain

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2009
    Posts
    1

    VBScript to join domain

    I have a vbscript to join a domain. The issue is I have no error checking to see if correct user name/password was user. As of right now, any user name/password will "join" the domain but in actually, you aren't because a bad user name and/or password were user. I have a input box where you enter your user name and password, that info is passed on to the following vbscript and then "joins" the domain. I am looking for a way to error check it so if a wrong user name/password is entered, it runs the input box again (the input box is "JoinDomain.exe"). The only think I came up with was to try to map a network drive as my error checker. If a wrong user name/password is supplied, you receive an error. The issue is, it stops there. I heed help to make to loop back to the JoinDomain.exe or if someone has any other way to error check, that would be great. vbscript posted below:
    Code:
    Option Explicit
    Dim objShell, WshShell
    Dim ReturnValue
    Dim RestartMsg
    Dim Network
    Dim u_name, p_word
    Dim dtStart
    Dim strUser, strPassword, strDomain, strComputer, strDriveLetter, strRemotePath, strProfile
    Dim objNetwork, objComputer, objWMIService, objConnection, objCommand, objRecordSet
    Dim objOperatingSystem, colOperatingSystems
    Set objShell = CreateObject("Shell.Application")
    Set WshShell = CreateObject("WScript.Shell")
    Set objShell = CreateObject("Wscript.Shell")
    Set WshShell = WScript.CreateObject("WScript.Shell")
    
    '=== Join Domain ===
    strUser = WScript.Arguments(0)
    strPassword = WScript.Arguments(1)
    strDomain = "bud.bpa.gov"
    
    Const JOIN_DOMAIN = 1
    Const ACCT_CREATE = 2
    Const ACCT_DELETE = 4
    Const WIN9X_UPGRADE = 16
    Const DOMAIN_JOIN_IF_JOINED = 32
    Const JOIN_UNSECURE = 64
    Const MACHINE_PASSWORD_PASSED = 128
    Const DEFERRED_SPN_SET = 256
    Const INSTALL_INVOCATION = 262144
    
    '=== Start Account Check ===
    Set network = Wscript.CreateObject("WScript.Network") 
    strDriveLetter = "Z:" 
    strRemotePath = "\\bud.bpa.gov\ent"
    strProfile = "false"
    
    Set objNetwork = WScript.CreateObject("WScript.Network") 
    objNetwork.MapNetworkDrive strDriveLetter, strRemotePath, strProfile, "BUD\" & strUser, strPassword
    
    On Error Resume Next
    'NEED HELP HERE!!! 
    
    '=== End Account Check ===
    
    'Set objNetwork = CreateObject("WScript.Network")
    'strComputer = objNetwork.ComputerName
    'Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & _
    '   strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & strComputer & "'")
    'ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, _
    '   strPassword, strDomain & "\" & strUser, NULL, JOIN_DOMAIN + ACCT_CREATE)
    
    '==== Restart Computer ====
    'RestartMsg=MsgBox("Welcome to the BUD Domain." & Chr(13) & _
    '		  ""  & Chr(13) & _
    '                  "Click OK to restart your computer to finish the uDisc installation process.", _
    '                  0, "Restart") 
    'strComputer = "."
    'Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate,(Shutdown)}!\\" _
    '    & strComputer & "\root\cimv2")
    'Set colOperatingSystems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
    'For Each objOperatingSystem in colOperatingSystems
    '    ObjOperatingSystem.Reboot()
    'Next

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: VBScript to join domain

    Moved From The FAQ Section

    Welcome to the forms.

    A bad user name or password will result in a specific error with a specific error number which you can trap for. This will never occur in your code, however, because you are using "On Error Resume Next"

    You don't want to do that. You want the error to happen so you can deal with it.

  3. #3
    New Member
    Join Date
    Feb 2009
    Posts
    4

    Re: VBScript to join domain

    You can skip the drive mapping instead, use the WBEM locater & connect server objects to connect with alternate credentials and set remote boot privilege. I didn’t test this but I think its’ good. - Scott


    Const JOIN_DOMAIN = 1
    Const ACCT_CREATE = 2
    Const ACCT_DELETE = 4
    Const WIN9X_UPGRADE = 16
    Const DOMAIN_JOIN_IF_JOINED = 32
    Const JOIN_UNSECURE = 64
    Const MACHINE_PASSWORD_PASSED = 128
    Const DEFERRED_SPN_SET = 256
    Const INSTALL_INVOCATION = 262144
    Const WBEM_IMPERSONATE = 3
    Const wbemPrivilegeRemoteShutdown = 23 ' Required to shut down a system using a network request.
    Const wbemPrivilegeShutdown = 18 ' Required to shut down a local system

    Set objNetwork = CreateObject("WScript.Network")
    strComputer = "RemoteServerName"
    strLocalUser = "LocalServerUserName"
    strLocalUserPassword = "LocalServerPassword"
    strNetAdminUser = "NetworkUserAdmin"
    strNetAdminPassword = "NetworkAdminPassword"

    On Error Resume Next
    Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
    Set objSWbemService = objSWbemLocator.ConnectServer(strComputer , "root\cimv2", strUser, strPassword)
    objSWbemService.Security_.ImpersonationLevel=WBEM_IMPERSONATE
    If Err <> 0 Then
    'Do something here
    End if
    On Error GoTo 0

    On Error Resume next
    Set objComputer = objSWbemService.get("\root\cimv2:Win32_ComputerSystem.Name='" & strComputer & "'")
    ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain,strNetAdminPassword, strDomain & "\" & strNetAdminUser, NULL, JOIN_DOMAIN + ACCT_CREATE)
    If Err <> 0 Then
    'Do something here
    End if
    On Error GoTo 0

    On Error Resume next
    objSWbemService.Security_.Privileges=wbemPrivilegeRemoteShutdown
    Set colOperatingSystems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
    For Each objOperatingSystem in colOperatingSystems
    ObjOperatingSystem.Reboot()
    Next
    If Err <> 0 Then
    'Do something here
    End if
    On Error GoTo 0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width