Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 484
  • Last Modified:

IE to get user creds in VBS

I have figured out how to get multiple inputs but not sure how to extract the data for use in a variable for the rest of my VB script.

PasswordBox("Please enter your domain admin credentials.") 
Wscript.Echo ("Domain: ") & domain
Wscript.Echo ("User: ") & admuser
Wscript.Echo ("Password: ") & pass

Function PasswordBox(sTitle) 
  set oIE = CreateObject("InternetExplorer.Application") 
  With oIE 
    .FullScreen = False 
    .ToolBar   = False : .RegisterAsDropTarget = False 
    .StatusBar = False : .Navigate("about:blank") 
    While .Busy : WScript.Sleep 200 : Wend 
    With .document 
      With .ParentWindow 
        .resizeto 350,210 
        .moveto .screen.width/2-200, .screen.height/2-50 
      End With 
      .Write("<html><body bgColor=Silver><center>") 
      .Write( sTitle ) 
      .Write("<br />") 
	  .Write("<input type=radio name=domain value=domain1 checked=checked>Domain1")
	  .Write("<input type=radio name=domain value=domain2>Domain2<br />")
      .Write("Username: <input type=test id=admuser><br />")
      .Write("Password: <input type=password id=pass><br />" & _ 
               "<button id=but3>Submit</button>")
      With .ParentWindow.document.body 
        .style.borderStyle = "outset" 
        .style.borderWidth = "3px" 
      End With 
      .all.but3.onclick = getref("PasswordBox_Submit") 
      oIE.Visible = True 
      bPasswordBoxOkay = False : bPasswordBoxWait = True 
      On Error Resume Next 
      While bPasswordBoxWait 
        WScript.Sleep 100 
        if oIE.Visible Then bPasswordBoxWait = bPasswordBoxWait 
        if Err Then bPasswordBoxWait = False 
      PasswordBox = .all.pass.value 
    End With ' document 
    .Visible = False 
  End With   ' IE 
End Function 

Sub PasswordBox_Submit() 
  bPasswordBoxWait = False 
End Sub

Open in new window

  • 5
  • 4
1 Solution
Hi, try changing
      PasswordBox = .all.pass.value

      PasswordBox = .all.admuser.value & "|" & .all.pass.value

and then use
arrCreds = Split(PasswordBox("Please enter your domain admin credentials."), "|")
Wscript.Echo ("User: " & arrCreds(0))
Wscript.Echo ("Password: " & arrCreds(1))

pphreadrAuthor Commented:

Again you find the answer. Now although I can add a text box for the domain, and use:
arrCreds = Split(PasswordBox("Please enter your domain admin credentials."), "|")
admDomain = arrCreds(0)
admUser = arrCreds(1)
admPW = arrCreds(2)
Wscript.Echo "Domain: " & admDomain
Wscript.Echo "User: " & admUser
Wscript.Echo "Password: " & admPW

Open in new window

     .Write("Username: <input type=test id=admuser><br />")

Open in new window

I would rather use either a dropdown or radio buttons, because I only have 2 domains to work with.

How do I determine which radio button is checked?
pphreadrAuthor Commented:
Oops the correct domain code is:
.Write("Domain: <input type=test id=domain><br />")

Open in new window

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Hi, with your radio option in your original code (the one with name=domain), does this work:

      PasswordBox = all.domain.value & "|" & .all.pass.value & "|" & .all.admuser.value

You may need to add id=domain to the radio options, or id='domain'

On the other hand, instead of launching an IE window to get this job done, and then, I assume, continuing the VBS to perform some other task, have you considered using a HTA, where you can design a UI, and run code directly in that HTA?


pphreadrAuthor Commented:

I have tried your suggestion for id-domain, but since there are 2 radio buttons it does not like that. I have decided to go with the same text box that is used for admUser. Thanks for the help.

As for an HTA, I have never really done that. I am assuming that what I am doing is using VB to create the interface and logic and calling IE (html) when needed, whereas an HTA would use html as the main interface and calling vb when needed.

If that is the case it sounds like the HTA would have a cleaner interface (less clunky than what I have), but I would basically have to start over. I like the idea, but I wanted to try and get this working by Tuesday.

That said this post has been in prep for #2 in the menu below, as I will need the domain creds to reset a computer account.
I am trying to create a stand alone file that will do the following:
1. Join a computer to a Workgroup
2. Reset AD Computer Account for the computer
3. Reset the local Administrator's password
4. Release the IP and shutdown computer
5. Do 1 - 4 in sequence
6. Join a Workgroup computer to a Domain

1. I have found several scripts that will remove a computer from a domain, but I want to have the local computer joined to a workgroup without removing the account from the domain. I want the computer detached from the domain, so I can reset the computer account later.

2. I am not sure if I have to specify the complete path to the OU the computer resides in, or can I simply code 'reset computer XXX' and it will find it for me.

5. easy
6. Once a computer is part of a workgroup and the AD account has been reset this should be fairly easy.

I am sure I will have more posts to follow....hopefully today.
OK, so it's getting bigger ;-)

For step 1, try something like this:
Const NETSETUP_ACCT_DELETE = 2 'Disables computer account in domain.
strUser = "administrator"
strPassword = "password"

Set objNetwork = CreateObject("WScript.Network")
strComputer = objNetwork.ComputerName

Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\.\root\cimv2:Win32_ComputerSystem.Name='" & strComputer & "'")
strDomain = objComputer.Domain

intReturn = objComputer.UnjoinDomainOrWorkgroup(strPassword, strDomain & "\" & strUser, NETSETUP_ACCT_DELETE)

Open in new window

Which should remove the computer from the domain, and disable the computer account.

For step 2, try code like this, to search AD for the computer name, and then reset the password:
Set objNetwork = CreateObject("WScript.Network")
strComputer = objNetwork.ComputerName

strADsPath = Get_LDAP_User_Properties("computer", "name", strComputer, "adsPath")
If strADsPath <> "" Then
	Set objComputer = GetObject(strADsPath)
	objComputer.SetPassword strComputer & "$"
End If

Private Function Get_LDAP_User_Properties(strObjectType, strSearchField, strObjectToGet, strCommaDelimProps)
    ' This is a custom function that connects to the Active Directory, and returns the specific
    ' Active Directory attribute value, of a specific Object.
    ' strObjectType: usually "User" or "Computer"
    ' strSearchField: the field by which to seach the AD by. This acts like an SQL Query's WHERE clause.
    '             It filters the results by the value of strObjectToGet
    ' strObjectToGet: the value by which the results are filtered by, according the strSearchField.
    '             For example, if you are searching based on the user account name, strSearchField
    '             would be "samAccountName", and strObjectToGet would be that speicific account name,
    '             such as "jsmith".  This equates to "WHERE 'samAccountName' = 'jsmith'"
    ' strCommaDelimProps: the field from the object to actually return.  For example, if you wanted
    '             the home folder path, as defined by the AD, for a specific user, this would be
    '             "homeDirectory".  If you want to return the ADsPath so that you can bind to that
    '             user and get your own parameters from them, then use "ADsPath" as a return string,
    '             then bind to the user: Set objUser = GetObject("LDAP://" & strReturnADsPath)
    ' Now we're checking if the user account passed may have a domain already specified,
    ' in which case we connect to that domain in AD, instead of the default one.
    If InStr(strObjectToGet, "\") > 0 Then
          arrGroupBits = Split(strObjectToGet, "\")
          strDC = arrGroupBits(0)
          strDNSDomain = strDC & "/" & "DC=" & Replace(Mid(strDC, InStr(strDC, ".") + 1), ".", ",DC=")
          strObjectToGet = arrGroupBits(1)
    ' Otherwise we just connect to the default domain
          Set objRootDSE = GetObject("LDAP://RootDSE")
          strDNSDomain = objRootDSE.Get("defaultNamingContext")
    End If

    strBase = "<LDAP://" & strDNSDomain & ">"
    ' Setup ADO objects.
    Set adoCommand = CreateObject("ADODB.Command")
    Set ADOConnection = CreateObject("ADODB.Connection")
    ADOConnection.Provider = "ADsDSOObject"
    ADOConnection.Open "Active Directory Provider"
    adoCommand.ActiveConnection = ADOConnection

    ' Filter on user objects.
    'strFilter = "(&(objectCategory=person)(objectClass=user))"
    strFilter = "(&(objectClass=" & strObjectType & ")(" & strSearchField & "=" & strObjectToGet & "))"

    ' Comma delimited list of attribute values to retrieve.
    strAttributes = strCommaDelimProps
    arrProperties = Split(strCommaDelimProps, ",")

    ' Construct the LDAP syntax query.
    strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
    adoCommand.CommandText = strQuery
    ' Define the maximum records to return
    adoCommand.Properties("Page Size") = 100
    adoCommand.Properties("Timeout") = 30
    adoCommand.Properties("Cache Results") = False

    ' Run the query.
    Set adoRecordset = adoCommand.Execute
    ' Enumerate the resulting recordset.
    strReturnVal = ""
    Do Until adoRecordset.EOF
        ' Retrieve values and display.
        For intCount = LBound(arrProperties) To UBound(arrProperties)
            If strReturnVal = "" Then
                If IsArray(adoRecordset.Fields(intCount).Value) Then
                    For Each strValue In adoRecordset.Fields(intCount).Value
                        If strReturnVal = "" Then
                            strReturnVal = strValue
                            strReturnVal = strReturnVal & ", " & strValue
                        End If
                    strReturnVal = adoRecordset.Fields(intCount).Value
                End If
                If IsArray(adoRecordset.Fields(intCount).Value) Then
                    For Each strValue In adoRecordset.Fields(intCount).Value
                        strReturnVal = strReturnVal & ", " & strValue
                    strReturnVal = strReturnVal & ", " & adoRecordset.Fields(intCount).Value
                End If
            End If
        ' Move to the next record in the recordset.
    ' Clean up.
    Get_LDAP_User_Properties = strReturnVal
End Function

Open in new window

Finally, to join it to the domain, use the JoinDomainOrWorkgroup method (as opposed to the UnjoinDomainOrWorkgroup in step 1).

One of the most annoying things with a process like this is having to reboot in between the domain changes.

Hope that helps.


pphreadrAuthor Commented:
Once again rob comes thru. 'Split' is a wonderful thing.
pphreadrAuthor Commented:

My timing is bad. I just figured out that I had to unjoin first. I had another question about that, so I posted my fix and completed that.
VB script to join computer to workgroup

Just after that I checked my email and  found you gave me that solution on this question and  what appears to be my next project of resetting the computer account.

Please post your reset solution here, so I can give you the credit you deserve.
How do I reset an AD computer account using VB

No problem.  Glad to help.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now