Link to home
Start Free TrialLog in
Avatar of madstylex
madstylex

asked on

HTA script to change computer name and add to domain

Hi Experts,

I would like to create a HTA script to change a computer name and add it to a domain of my choice.  This does NOT need to be done remotely.  The input screen should look as follows.

Domain name:
New Computer Name:
Username:
Password:

I have created a vbs script using input boxes, but I can't figure out how to do with HTA.

Please help.
Avatar of aikimark
aikimark
Flag of United States of America image

@madstylex

How would you do such a task manually?
Have you tried htaedit?
Hi, this is a very simplistic HTML layout, as I just knocked it together, but I think it will work.

Regards,

Rob.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>My HTML Application</title>
<script language="vbscript">
Sub Window_OnLoad
	intWidth = 700
	intHeight = 500
	Me.ResizeTo intWidth, intHeight
    Me.MoveTo ((Screen.Width / 2) - (intWidth / 2)),((Screen.Height / 2) - (intHeight / 2))
    Set objNetwork = CreateObject("WScript.Network")
    span_computer.InnerHTML = objNetwork.ComputerName
End Sub

Sub RenameComputer	
	strNewName = Trim(txt_newcomputername.Value)
	If strNewName <> "" Then
		Set oWMI = GetObject("winmgmts:\\.\root\cimv2")
		For Each oCmp in oWMI.InstancesOf("Win32_ComputerSystem")
			sReturn = oCmp.Rename(strNewName)
		Next
		Set objOSSet = GetObject("winmgmts:{(RemoteShutdown)}//./root/cimv2").ExecQuery("select * from Win32_OperatingSystem where Primary=true")
		For Each objOS In objOSSet
			objOS.Reboot()
		Next
	Else
		MsgBox "Please enter a computer name."
	End If
End Sub

Sub JoinDomain
	' Specify credentials.
	sUser = Trim(txt_domainuser.Value)
	sPassword = Trim(txt_domainpassword.Value)
	sDomain = Trim(txt_domain.Value)
	
	If sUser <> "" Then
		If sPassword <> "" Then
			If sDomain <> "" then
				' Join Domain
				Const JOIN_DOMAIN = 1
				Const ACCT_CREATE = 2
				
				Set oWMI = GetObject("winmgmts:\\.\root\cimv2")
				For Each oCmp in oWMI.InstancesOf("Win32_ComputerSystem")
					sReturn = oCmp.JoinDomainOrWorkgroup(sDomain, sPassword, sDomain & "\" & sUser,, JOIN_DOMAIN+ACCT_CREATE)
				Next
				MsgBox "Computer has been joined to the domain."
			Else
				MsgBox "Please enter a username."
			End If
		Else
			MsgBox "Please enter a password."
		End If
	Else
		MsgBox "Please enter a domain name."
	End If
End Sub
</script>
<hta:application
	applicationname="Rename and Join Domain"	
	border="dialog"
	borderstyle="normal"
	caption="My HTML Application"
	contextmenu="no"
	icon="myicon.ico"
	maximizebutton="no"
	minimizebutton="yes"
	navigable="no"
	scroll="no"
	selection="no"
	showintaskbar="yes"
	singleinstance="yes"
	sysmenu="yes"
	version="1.0"
	windowstate="normal"
>
</head>
<body>
	<table>
		<tr>
			<td align="center">
				<h2>Rename Computer</h2>
			</td>
		</tr>
		<tr>
			<td align="center">
				Verify the computer account is correct before joining the domain.  If a rename is required, the computer will automatically reboot, and you must run this HTA again to join it to the domain.
			</td>
		</tr>
		<tr>
			<td>
				Current computer name: <span id="span_computer"></span>
			</td>
		</tr>
		<tr>
			<td>
				New Computer Name: <input type="text" id="txt_newcomputername" name="txt_newcomputername">&nbsp;&nbsp;
				<input type="button" id="btn_rename" name="btn_rename" value="Rename..." onclick="RenameComputer">
			</td>
		</tr>
		<tr>
			<td>
				&nbsp;
			</td>
		</tr>
		<tr>
			<td align="center">
				<h2>Join Domain</h2>
			</td>
		</tr>
		<tr>
			<td align="center">
				Verify the computer account is correct before joining the domain.
			</td>
		</tr>
		<tr>
			<td>
				Domain Username: <input type="text" id="txt_domainusername" name="txt_domainusername">
			</td>
		</tr>
		<tr>
			<td>
				Domain Password: <input type="password" id="txt_domainpassword" name="txt_domainpassword">
			</td>
		</tr>
		<tr>
			<td>
				Domain: <input type="text" id="txt_domain" name="txt_domain">
			</td>
		</tr>
		<tr>
			<td>
				<input type="button" id="btn_joindomain" name="btn_joindomain" value="Join Domain..." onclick="JoinDomain">
			</td>
		</tr>
	</table>
</body>
</html>

Open in new window

Avatar of madstylex
madstylex

ASKER

rob,

this seems to work.  is there a way to do both tasks at the same time without rebooting?
Hi, you can try by commenting out this line:
                  objOS.Reboot()

but I'm not sure whether AD will recognise the account properly if you don't restart after renaming.  See what it does anyway.  I've just always rebooted before joining a domain.

Regards,

Rob.
Hi Rob,

Yes you need to reboot after joining a domain.  When the script is run it says 'if a rename is required, the computer will automatically reboot'.

When I'm setting up machines, I usually rename and join a domain at the same time, then reboot.
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial