Sign in to follow this  
Followers 0
ACalcutt

Conversting joindomain.htm script

3 posts in this topic

I am trying to covert a hta( i think thats vbscript) to autoit. I thought i had it right but it does not join the domain. it gives an incorrect parameter error. I am new to wmi, so any help is appreciated

Here is my current version of the script

;***********************************************************************
;* 07/02/2006 - Modified by Andrew Calcutt
;*      -Converted script to AutoIT
;*
;* 07/07/2005 - Modified by Pedro J Toro 
;*      - added ability to choose OU to put computer into.
;*      - will record who added the computer and at what time
;*        into a SQL DB.
;*      - removed unnecessary code.
;* This will allow a user to add a computer to the domain (worcester.local)
;* and into a specific Organizational Unit (OU).
;*
;********************************************************************-->
#include <GuiConstants.au3>
GuiCreate("Tech Computer Domain Join", 392, 323,-1, -1 , BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS))
GuiCtrlCreateLabel("Machine Name:", 10, 10, 80, 20)
$machine_name = GuiCtrlCreateInput(@ComputerName, 100, 10, 280, 20, $ES_READONLY)
GuiCtrlCreateLabel("Join To:", 10, 40, 90, 20)
$join_to = GuiCtrlCreateCombo("", 100, 40, 280, 20)
GUICtrlSetData($join_to,"Lab|Employee","Lab")
GuiCtrlCreateLabel("Username:", 10, 70, 90, 20)
$username = GuiCtrlCreateInput("", 100, 70, 280, 20)
GuiCtrlCreateLabel("Password:", 10, 100, 90, 20)
$password = GuiCtrlCreateInput("", 100, 100, 280, 20, $ES_PASSWORD)
GuiCtrlCreateLabel("Domain", 10, 130, 80, 20)
$domain = GuiCtrlCreateCombo("", 100, 130, 280, 20)
GUICtrlSetData($domain,"ACL|WSC_DOMAIN","WSC_DOMAIN")
$join_button = GuiCtrlCreateButton("Join Domain", 10, 150, 110, 20)
$join_edit = GuiCtrlCreateEdit("", 10, 170, 370, 140)
GuiSetState()
While 1
    $msg = GuiGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        ExitLoop
    Case $msg = $join_button
        _JoinScript()
    Case Else
    ;;;
    EndSelect
WEnd
Exit

Func _JoinScript();Checks if Username and Password where given. If given, joins to domain
    If GUICtrlRead($username) = "" or GUICtrlRead($password) = "" Then
        GUICtrlSetData($join_edit, "Error: You must supply a username and password")
    Else
        _JoinToDomain (GUICtrlRead($domain), GUICtrlRead($username), GUICtrlRead($password), GUICtrlRead($machine_name), "worcester.local", $join_to)
    EndIf
EndFunc

Func _JoinToDomain($strUserDomain, $strUser, $strPassword, $strComputer, $strComputerDomain, $strOU);joins compuer to domain
    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

    Dim $strDomainUser
    Dim $strFailString
    Dim $strSucceedString

;proc1.innerText="Joining Domain"
    GUICtrlSetData($join_edit, "Joining Domain")
    $strDomainUser = $strUserDomain & "\" & $strUser

    $objNetwork = ObjCreate("WScript.Network")
    $objComputer = ObjGet("winmgmts:{impersonationLevel=Impersonate}!\\" & _
                                $strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & _
                                $strComputer & "'")
    $ReturnValue = $objComputer.JoinDomainOrWorkGroup($strComputerDomain, _
                                                    $strPassword, _
                                                    $strDomainUser, _
                                                    $strOU, _
                                                    $JOIN_DOMAIN + $ACCT_CREATE)

    If ($ReturnValue <> 0) Then
    ; get the net helpmsg from the error code returned
        Dim $strHelpMsg, $command
        $command = "net helpmsg " & $ReturnValue

        $objShell = ObjCreate("WScript.Shell")
        $objScriptExec = $objShell.Exec($command)

    ; store the error message into strHelpMsg
        $strHelpMsg = $objScriptExec.StdOut.ReadAll()
        $strFailString = "Join Failed" & @CRLF & _
                        "Computer: " & $strComputer & @CRLF & _
                        "Domain: " & $strComputerDomain & @CRLF & _
                        "User Name: " & $strDomainUser & @CRLF & _
                        "Error Message: " & $strHelpMsg
    ;proc1stat.innerText = strFailString
        GUICtrlSetData($join_edit, $strFailString)
    Else
        $strSucceedString = "Join Succeeded" & @CRLF & _
                        "Computer: " & $strComputer & @CRLF & _
                        "Domain: " & $strComputerDomain & @CRLF & _
                        "User Name: " & $strDomainUser

        _RecordData($strComputer, $strUser, $strUserDomain, $strOU)
    ;proc1stat.innerText = $strSucceedString
        GUICtrlSetData($join_edit, $strSucceedString)
    EndIf
EndFunc

Func _RecordData($strComputer, $strUser, $strUserDomain, $strOU)
    Dim $objConnection
    Dim $strConnection, $strSQL
    Dim $arrTemp1, $arrTemp2
    Dim $strNewOU

;extract OU from full OU path
    $arrTemp1 = StringSplit($strOU, "=")
    $arrTemp2 = StringSplit($arrTemp1[1], ",")
    $strNewOU = $arrTemp2[0]
    

;Create objects
    $objConnection = ObjCreate("ADODB.Connection")
    $objRS = ObjCreate("ADODB.RecordSet")

;Connection String Value
    $strConnectstring = "Driver={SQL Server};Server=wscdata1;UID=joined_dbo;PWD=join2DB05;WSID=;Language=us_english;Database=Joined;DSN=;
"

;Open the Connection
    $objConnection.Open $strConnectstring

;SQL statement
    $strSQL = "INSERT INTO computers " &_
                    "(compName, " &_
                    "usrName, " &_
                    "domainName, " &_
                    "OU) " &_
            "VALUES ('" &_
                    $strComputer & "', '" &_
                    $strUser & "', '" &_
                    $strUserDomain & "', '" &_
                    $strNewOU & "')"

;execute SQL command
    $objConnection.execute($strSQL)

;close the connection and free up resources
    $objConnection.Close
    $objConnection = "Nothing"
EndFunc

this is what it was converted from

<html>
<!--********************************************************************
'*
'*  File:          scriptomatic.hta
'*  Created:        August 2002
'*  Version:        1.0
'*
'*  Description:    Learning tool. Enables users to generate and run
'*                WSH scripts (in VBScript) that use WMI to display
'*                properties available through the Win32_ classes.
'*
'*
'* Copyright (C) 2002 Microsoft Corporation
'*
'***********************************************************************
'*
'* Modified by Pedro J Toro
'*
'* 07/07/2005   - added ability to choose OU to put computer into.
'*      - will record who added the computer and at what time
'*        into a SQL DB.
'*      - removed unnecessary code.
'*
'* This will allow a user to add a computer to the domain (worcester.local)
'* and into a specific Organizational Unit (OU).
'*
'********************************************************************-->
<title>Tech Computer Domain Join</title>

<HTA:APPLICATION
    ID="objScriptomatic"
    APPLICATIONNAME="Scriptomatic"
    SCROLL="no"
    SINGLEINSTANCE="yes"
    WINDOWSTATE="normal"
    MAXIMIZEBUTTON="no"
    MINIMIZEBUTTON="no"
>
<head>

<style>
BODY
{
    background-color: buttonface;
    font-family: Helvetica;
    font-size: 8pt;
    margin-top: 10px;
    margin-left: 10px;
    margin-right: 10px;
    margin-bottom: 10px;
}

.button
{
    font-family: Helvetica;
    font-size: 10pt;
    width: 100%;
}

textarea
{
    font-family: arial;
    font-size: 8pt;
    margin-left: 3px;
}

input
{
    width:100%;
}

select
{
    font-family: arial;
    font-size: 10pt;
    margin-left: 0px;
}

td
{
    valign:top;
}
</style>


<script language="vbscript">

Sub Window_onload
    On Error Resume Next
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)

    For Each objItem in colItems
        txtMachine.innerText = objItem.caption
    Next
End Sub


Sub JoinScript
    If ((txtEmail.value="") or (txtPassword.value="")) Then
        msgbox("You must supply a username/password")
    Else
        JoinToDomain txtDomain.value, txtEmail.value, txtPassword.value, txtMachine.innerText, "worcester.local", txtJoin.value
    End If
End Sub


Sub JoinToDomain(strUserDomain, strUser, strPassword, strComputer, strComputerDomain, strOU)
    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

    Dim strDomainUser
    Dim strFailString
    Dim strSucceedString

    proc1.innerText="Joining Domain"
    strDomainUser=trim(strUserDomain) & "\" & trim(strUser)

    Set objNetwork = CreateObject("WScript.Network")
    Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & _
                                strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & _
                                strComputer & "'")
    ReturnValue = objComputer.JoinDomainOrWorkGroup(strComputerDomain, _
                                                    strPassword, _
                                                    strDomainUser, _
                                                    strOU, _
                                                    JOIN_DOMAIN + ACCT_CREATE)

    If (ReturnValue <> 0) Then
        ' get the net helpmsg from the error code returned
        Dim strHelpMsg, command
        command = "net helpmsg " & ReturnValue

        Set objShell = CreateObject("WScript.Shell")
        Set objScriptExec = objShell.Exec(command)

        ' store the error message into strHelpMsg
        strHelpMsg = objScriptExec.StdOut.ReadAll()
        strFailString = "Join Failed" & vbcrlf & _
                        "Computer: " & strComputer & vbcrlf & _
                        "Domain: " & strComputerDomain & vbcrlf & _
                        "User Name: " & strDomainUser & vbcrlf & _
                        "Error Message: " & strHelpMsg
        proc1stat.innerText = strFailString
    Else
        strSucceedString = "Join Succeeded" & vbcrlf & _
                        "Computer: " & strComputer & vbcrlf & _
                        "Domain: " & strComputerDomain & vbcrlf & _
                        "User Name: " & strDomainUser

        RecordData strComputer, strUser, strUserDomain, strOU

        proc1stat.innerText = strSucceedString
    End If
End Sub

Sub RecordData(strComputer, strUser, strUserDomain, strOU)
    Dim objConnection
    Dim strConnection, strSQL
    Dim arrTemp1, arrTemp2
    Dim strNewOU

    'extract OU from full OU path
    arrTemp1 = split(strOU, "=")
    arrTemp2 = split(arrTemp1(1), ",")
    strNewOU = arrTemp2(0)

    'Create objects
    Set objConnection = CreateObject("ADODB.Connection")
    Set objRS = CreateObject("ADODB.RecordSet")

    'Connection String Value
    strConnectstring = "Driver={SQL Server};Server=wscdata1;UID=joined_dbo;PWD=join2DB05;WSID=;Language=us_english;Database=Joined;DSN=;
"

    'Open the Connection
    objConnection.Open strConnectstring

    'SQL statement
    strSQL = "INSERT INTO computers " &_
                    "(compName, " &_
                    "usrName, " &_
                    "domainName, " &_
                    "OU) " &_
            "VALUES ('" &_
                    strComputer & "', '" &_
                    strUser & "', '" &_
                    strUserDomain & "', '" &_
                    strNewOU & "')"

    'execute SQL command
    objConnection.execute(strSQL)

    'close the connection and free up resources
    objConnection.Close
    Set objConnection = Nothing
End Sub

</script>
</head>


<body>

<table border=1 width="100%">
  <tr>
    <td width="20%">Machine Name:</td>
    <td width="52%"><div id="txtMachine"></div></td>
  </tr>
  <tr>
      <td>Join To:</td>
      <td width="52%">  <select name=txtJoin size="1">
          <option value="ou=employees,dc=worcester,dc=local">Employee</option>
          <option value="ou=labs,dc=worcester,dc=local" selected>Lab</option>
          </select></td>
  </tr>

   <tr>
      <td>Username:</td>
      <td width="52%">

      <input type="text" id="txtEmail" name="txtEmail" width="100%"></td>
  </tr>
   <tr>
        <td>Password:</td>
        <td width="52%"><input type="password" width="300px" id="txtPassword" name="txtPassword"></td>
  </tr>
  <tr>
      <td>Domain:</td>
      <td width="52%">  <select name=txtDomain size="1">
          <option value="ACL" selected>ACL</option>
          <option value="wsc_domain">WSC_DOMAIN</option>
          <option value="Exchange_Domain">Exchange</option>
          </select>
      </td>
  </tr>

  <tr>
   <td colspan="2">
    </td>
</tr>

  <tr>
   <td colspan="2" height="30">


      <p align="center">


      <input id=joinbutton class="button" type="submit" width="100px" value="Join Domain" name="join_button" onclick="JoinScript()" style="float: center">
   </td>
</tr>
</table>
<table border=1 width=100%>
    <tr>
        <td><div id="proc1"></div></td>
    </tr>
    <tr>
        <td height="5"><div id="proc1stat"></div></td>
    </tr>
</table>



</body>

</html>

Andrew Calcutt

Http://www.Vistumbler.net

Http://www.TechIdiots.net

Its not an error, its a undocumented feature

Share this post


Link to post
Share on other sites



Line 180 has a typo error,

strConnectstring = "Driver={SQL Server};Server=wscdata1;UID=joined_dbo;PWD=join2DB05;WSID=;Language=us_english;Database=Joined;DSN=;

"

should be

strConnectstring = "Driver={SQL Server};Server=wscdata1;UID=joined_dbo;PWD=join2DB05;WSID=;Language=us_english;Database=Joined;DSN=;

"

not sure if it helps much

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

the problem ended up being that i was specifing the OU wrong....i was specifing it as the label "Employee" instead of the actual OU "ou=employees,dc=worcester,dc=local"

Edited by ACalcutt

Andrew Calcutt

Http://www.Vistumbler.net

Http://www.TechIdiots.net

Its not an error, its a undocumented feature

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0