Put vbscript into HTA

Batch, ASP, JScript, Kixtart, etc.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
funnysun
Posts: 15
Joined: Wed Apr 08, 2009 7:16 pm

Put vbscript into HTA

Post by funnysun » Sun Apr 12, 2009 10:53 am

I already have script and tring to put it into hta file so it's more user friendly, but somehow, it does not work, no error found at all, Can anyone please help troubleshoot?

i have attached my code

<html><head><title>Add Computer To Domain</title><style></style></head><script>window.resizeTo(500,400);window.moveTo(150,100);</script><HTA:APPLICATION APPLICATIONNAME="Add Computer To Domain" SCROLL="No" SINGLEINSTANCE="Yes" borderstyle="Thin" minimizebutton="No" maximizebutton="No" icon=""></head>
<body><center><br><h2>Add Computer To Domain</h2><br><br>Computer Name: <input type="text" maxlength="20" size="30" name="txt_computername"><br><br><b>Domain Credentials:</b><br>User Name: <input type="text" maxlength="20" size="40" name="txt_username"><br><br>Password: <input type="password" maxlength="20" size="40" name="txt_password"><br><br><input type="button" value="Add To Domain" onClick="vbs:add2Domain"><br><br></center> <SCRIPT Language="VBScript"> Sub Window_OnLoad intHeight = 480 intWidth = 600 Me.ResizeTo intWidth, intHeight Me.MoveTo (screen.width / 2) - (intWidth / 2), (screen.height / 2) - (intHeight / 2) Set objNetwork = CreateObject("WScript.Network") txt_computername.Value = objNetwork.ComputerNameEnd SubSub add2Domain
If Trim(txt_username.Value) = "" Then MsgBox "Please enter a username." txt_username.Focus ElseIf Trim(txt_password.value) = "" Then MsgBox "Please enter a password." txt_password.Focus Else On Error Resume Next
Const ADS_SCOPE_SUBTREE = 2 Const ADS_SECURE_AUTHENTICATION = 1 Const HKEY_LOCAL_MACHINE = &H80000002 Const ForReading = 1 Const ForWriting = 2
Dim strDomain, strPassword, strUser, strAdsPath, strOU,fPath
Set WshShell =WScript.CreateObject("Wscript.Shell") Set objFSO = CreateObject("Scripting.FileSystemObject")
UserProfile = WshShell.ExpandEnvironmentStrings("%userprofile%") strLogDir = UserProfile&"Desktop" strDomain = "TEST" strComputer=Trim(txt_computername.Value) strPassword=Trim(txt_password.Value) strUser=Trim(txt_username.Value)
If objFSO.FileExists(strLogDir & "Domain Log.txt") Then Set objFile = objFso.OpenTextFile(strLogDir & "Domain Log.txt",ForWriting) objFile.WriteLine "Log Started : " & Now objFile.WriteLine DelAcct() Join2Domain() objFile.Writeline "Log Ended :" & NOW objFile.close Else fPath=strLogDir & "Domain Log.txt" Set objFile = objFso.CreateTextFile (fPath, True) objFile.WriteLine "Log Started : " & Now objFile.WriteLine DelAcct() Join2Domain() objFile.Writeline "Computer Booted :" & NOW objFile.close End If ShutDown strComputerEnd IfEnd Sub
Function ShutDown(strComputer)Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!" & strComputer & "rootcimv2:Win32_Process")E = objWMIService.Create("cmd /c shutdown -r -t 30 ", null, null, intProcessID)
'Set objWMIService = GetObject("winmgmts:" _'& "{impersonationLevel=impersonate}!" & strComputer & "rootcimv2")
Set WMIService = GetObject("winmgmts:{impersonationLevel=impersonate,(Shutdown)}!" & strComputer & "rootcimv2") Set OperatingSystems = WMIService.ExecQuery("Select * From Win32_OperatingSystem") For Each OperatingSystem in OperatingSystems OperatingSystem.Reboot() Nextif E = 0 Then ShutDown = "OK"end If
End Function
Function DelAcct()
Set objConnection = CreateObject("ADODB.Connection")Set objCommand = CreateObject("ADODB.Command")objConnection.Provider = "ADsDSOObject"objConnection.Properties("User ID") = strDomain&""&strUserobjConnection.Properties("Password") = strPasswordobjConnection.Properties("Encrypt Password") = TrueobjConnection.Properties("ADSI Flag") = 1
objConnection.Open "Active Directory Provider"Set objCommand.ActiveConnection = objConnectionobjCommand.Properties("Page Size") = 100objCommand.Properties("Cache Results") = FalseobjCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE objCommand.CommandText = _ "SELECT ADsPath FROM 'LDAP://" & strDomain & ".com" & "' WHERE objectCategory='computer' " & _ "AND Name='" & strComputer & "'"Set objRecordSet = objCommand.Execute
If objRecordSet.EOF Then MsgBox "Computer not found." objFile.WriteLine "Computer not found."ElseobjRecordSet.MoveFirstWhile Not objRecordSet.EOF strAdsPath = objRecordSet.Fields("ADsPath").Value intDNLength = Len(strAdsPath)- Len("LDAP://"&strDomain&".com/CN=")-Len (strComputer)-Len (",") strOU=Right(strAdsPath, intDNLength) objRecordSet.MoveNextWend Set objADS = GetObject("LDAP:") Set objComputer = objADS.OpenDSObject(strAdsPath, strDomain&""&strUser, strPassword,ADS_SECURE_AUTHENTICATION) objComputer.DeleteObject (0) objFile.WriteLine "Computer path: " & strOU MsgBox "Computer was removed from AD " objFile.WriteLine "Computer was removed from AD "End IfEnd Function
Function Join2Domain()Const JOIN_DOMAIN = 1Const ACCT_CREATE = 2
Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!" & _ strComputer & "rootcimv2:Win32_ComputerSystem.Name='" & _ strComputer & "'")ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, _ strPassword, _ strDomain & "" & strUser, _ strOU, _ JOIN_DOMAIN + ACCT_CREATE)If ReturnValue = 5 ThenobjFile.WriteLine "Access was Denied for adding the Computer to the Domain"objFile.WriteLine "RetureValue: " & ReturnValueEnd IfIf ReturnValue = 2224 ThenobjFile.WriteLine "Account exists in the Domain"objFile.WriteLine "RetureValue: " & ReturnValueEnd IfIf ReturnValue = 0 ThenobjFile.WriteLine "The Computer was sucssefully added to the domain"objFile.WriteLine "RetureValue: " & ReturnValueEnd ifIf ReturnValue > 2691 ThenobjFile.WriteLine "The Computer was sucssefully added to the domain"objFile.WriteLine "RetureValue: " & ReturnValueElseobjFile.WriteLine "The Computer failed to join domain: "& strDomainobjFile.WriteLine "RetureValue: " & ReturnValueEnd IfEnd Function
</Script></body></html>funnysun2009-04-12 18:07:34

User avatar
jvierra
Posts: 13718
Joined: Tue May 22, 2007 9:57 am
Contact:

Put vbscript into HTA

Post by jvierra » Sun Apr 12, 2009 1:06 pm

Well this isn't as bvad as it loked at first.

I am reseting the code so you can see how to debug these beasts. I have completely separated teh functional code from teh UI components.

Here is teh UI. Get it looking the way you want and I will show you how to merge in teh functional VB Script code so that it works both from teh command line and from the HTA.

uploads/2491/Add-Computer.hta.txt

YOu will have right click and "download target" to get this file.

User avatar
funnysun
Posts: 15
Joined: Wed Apr 08, 2009 7:16 pm

Put vbscript into HTA

Post by funnysun » Mon Apr 13, 2009 3:03 am

i have got my hta working,
thanks to jvierra, you inspired me :)

Locked