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.
This topic is 14 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
funnysun
Posts: 15
Last visit: Thu Jul 09, 2009 4:17 am

Put vbscript into HTA

Post by funnysun »

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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Put vbscript into HTA

Post by jvierra »

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
Last visit: Thu Jul 09, 2009 4:17 am

Put vbscript into HTA

Post by funnysun »

i have got my hta working,
thanks to jvierra, you inspired me :)
This topic is 14 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked