We help IT Professionals succeed at work.

Script to open 2 ADUC's of 2 different Domains with a specific credentials.

bsharath
bsharath used Ask the Experts™
on
Hi,

Script to open 2 ADUC's of 2 different Domains with a specific credentials.
Connect 2 2 different Domains and open 2 at the same time
Username should be hardcoded but password prompt should come. Password should not be seens to others when yyped and just dots or X marks should poulate.

Regards
Sharath
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
are the domains in a trust or same forest?

Author

Commented:
The are seperate forests but have a trust
Most Valuable Expert 2012
Top Expert 2014

Commented:
Sharath, try this HTA.  You only need to change the path for strPSExec to point to your copy of PSExec.exe

Regards,

Rob.
<Html>
<Head>
<Title>Run ADUC With Alternate Credentials</Title>
 
<HTA:Application
Caption = Yes
Border = Thin
Scroll = Yes
SingleInstance = Yes
ShowInTaskBar = Yes
MaximizeButton = Yes
MinimizeButton = Yes>
 
<script Language = VBScript>
	Sub Window_OnLoad
		intWidth = 800
		intHeight = 600
		Me.ResizeTo intWidth, intHeight
		Me.MoveTo ((Screen.Width / 2) - (intWidth / 2)),((Screen.Height / 2) - (intHeight / 2))
	End Sub

	Sub Default_Buttons
		If Window.Event.KeyCode = 13 Then
			btn_run.Click
		End If
	End Sub

	Sub Run_ADUC
		Set objFSO = CreateObject("Scripting.FileSystemObject")
		Set objNetwork = CreateObject("WScript.Network")
		Set objShell = CreateObject("WScript.Shell")
		strPSExec = "\\server\share\psexec.exe"
		strPSExec = objFSO.GetFile(strPSExec).ShortPath
		strCommand = strPSExec & " -accepteula -e -i -u " & txt_username.Value & " -p " & txt_password.Value & " \\" & objNetwork.ComputerName & " mmc dsa.msc /domain=" & txt_domain.Value
		objShell.Run strCommand, 0, True
	End Sub
</script>
<body style="background-color:#B0C4DE;" onkeypress='vbs:Default_Buttons'>
	<table height="90%" width width= "90%" border="0" align="center">
		<tr>
			<td align="center" colspan="2">
				<h2>Run ADUC With Alternate Credentials</h2>
			</td>
		</tr>
		<tr>
			<td>
				Enter domain (FQDN):
			</td>
			<td>
				<input type="text" id="txt_domain" name="txt_domain" size="50" maxlength="40"
			</td>
		</tr>
		<tr>
			<td>
				Username (DOMAIN/Username):
			</td>
			<td>
				<input type="text" id="txt_username" name="txt_username" size="50" maxlength="40"
			</td>
		</tr>
		<tr>
			<td>
				Password:
			</td>
			<td>
				<input type="password" id="txt_password" name="txt_password" size="50" maxlength="40"
			</td>
		</tr>
		<tr align="center">
			<td colspan="2">
				<button name="btn_run" id="btn_run" accessKey="R" onclick="vbs:Run_ADUC"><u>R</u>un ADUC</button>
			</td>
		</tr>
	</table>
</body>
</head>
</html>

Open in new window

Author

Commented:
Thanks Rob works perfect
Can i have both the FqDN's fixed and user names fixed. So each time i just need to give both domain passwords
Most Valuable Expert 2012
Top Expert 2014

Commented:
Instead of that, what if we used drop down boxes for the first two options, so you can add as many as you want, then you select each option that you need?
Most Valuable Expert 2012
Top Expert 2014

Commented:
I only say that because it only has one spot to enter the details, so instead of duplicating that, when it comes time that you might need another entry, you could just add that to the drop down boxes.

Rob.

Author

Commented:
Yes Rob that would be fine
Most Valuable Expert 2012
Top Expert 2014
Commented:
OK, try this. You will need to modify these lines:
            strPSExec = "\\server\share\psexec.exe"

and

                              <option id="opt_domain1" value="domain1.com">domain1.com
                              <option id="opt_domain2" value="domain2.com">domain2.com

and

                              <option id="opt_username1" value="domain1\administrator">domain1\administrator
                              <option id="opt_username2" value="domain2\administrator">domain2\administrator

For the domains and users, leave the id parameter as is, but change the value for each, and each string after the last bracket to match the value.

Regards,

Rob.
<Html>
<Head>
<Title>Run ADUC With Alternate Credentials</Title>
 
<HTA:Application
Caption = Yes
Border = Thin
Scroll = Yes
SingleInstance = Yes
ShowInTaskBar = Yes
MaximizeButton = Yes
MinimizeButton = Yes>
 
<script Language = VBScript>
	Sub Window_OnLoad
		intWidth = 800
		intHeight = 600
		Me.ResizeTo intWidth, intHeight
		Me.MoveTo ((Screen.Width / 2) - (intWidth / 2)),((Screen.Height / 2) - (intHeight / 2))
	End Sub

	Sub Default_Buttons
		If Window.Event.KeyCode = 13 Then
			btn_run.Click
		End If
	End Sub

	Sub Run_ADUC
		Set objFSO = CreateObject("Scripting.FileSystemObject")
		Set objNetwork = CreateObject("WScript.Network")
		Set objShell = CreateObject("WScript.Shell")
		strPSExec = "\\server\share\psexec.exe"
		strPSExec = objFSO.GetFile(strPSExec).ShortPath
		strCommand = strPSExec & " -accepteula -e -i -u " & cbo_username.Value & " -p " & txt_password.Value & " \\" & objNetwork.ComputerName & " mmc dsa.msc /domain=" & cbo_domain.Value
		objShell.Run strCommand, 0, True
	End Sub
</script>
<body style="background-color:#B0C4DE;" onkeypress='vbs:Default_Buttons'>
	<table height="90%" width width= "90%" border="0" align="center">
		<tr>
			<td align="center" colspan="2">
				<h2>Run ADUC With Alternate Credentials</h2>
			</td>
		</tr>
		<tr>
			<td>
				Select domain (FQDN):
			</td>
			<td>
				<select id="cbo_domain" name="cbo_domain" size="1">
					<option id="opt_domain1" value="domain1.com">domain1.com
					<option id="opt_domain2" value="domain2.com">domain2.com
				</select>
			</td>
		</tr>
		<tr>
			<td>
				Select username (DOMAIN/Username):
			</td>
			<td>
				<select id="cbo_username" name="cbo_username" size="1">
					<option id="opt_username1" value="domain1\administrator">domain1\administrator
					<option id="opt_username2" value="domain2\administrator">domain2\administrator
				</select>
			</td>
		</tr>
		<tr>
			<td>
				Password:
			</td>
			<td>
				<input type="password" id="txt_password" name="txt_password" size="50" maxlength="40"
			</td>
		</tr>
		<tr align="center">
			<td colspan="2">
				<button name="btn_run" id="btn_run" accessKey="R" onclick="vbs:Run_ADUC"><u>R</u>un ADUC</button>
			</td>
		</tr>
	</table>
</body>
</head>
</html>

Open in new window

Author

Commented:
it works Rob thanks
But when opened i get access denied message in
Line 18
Char 3
Most Valuable Expert 2012
Top Expert 2014

Commented:
That's on the resizing of the window.....strange.....to get around that, change these lines:

            Me.ResizeTo intWidth, intHeight
            Me.MoveTo ((Screen.Width / 2) - (intWidth / 2)),((Screen.Height / 2) - (intHeight / 2))


to this

            On Error Resume Next
            Me.ResizeTo intWidth, intHeight
            Me.MoveTo ((Screen.Width / 2) - (intWidth / 2)),((Screen.Height / 2) - (intHeight / 2))
            Err.Clear
            On Error GoTo 0



Regards,

Rob.

Author

Commented:
Rob all fine now
But if i open one ADUC and then try to open another it does not open until i close the other. Can i not have both opened one after the other
Most Valuable Expert 2012
Top Expert 2014

Commented:
Oh yeah, whoops. Change this line:
            objShell.Run strCommand, 0, True

to this
            objShell.Run strCommand, 0, False

Regards,

Rob.

Author

Commented:
Thanks a lot Rob :-)

Author

Commented:
Rob.
The user names that are harcoded. Can i get say 5 different names in the dropdown?
And have the one i select alway the default when opened?
Most Valuable Expert 2012
Top Expert 2014

Commented:
To add more users, just add more of these
                              <option id="opt_username1" value="domain1\administrator">domain1\administrator
                              <option id="opt_username2" value="domain2\administrator">domain2\administrator

like
                              <option id="opt_username3" value="domain1\otheruser1">domain1\otheruser1
                              <option id="opt_username4" value="domain2\otheruser2">domain2\otheruser2

For the saving of the default user, I need to write to a file called "defaultuser" in the same folder as the HTA.  Try this.

Regards,

Rob.
<Html>
<Head>
<Title>Run ADUC With Alternate Credentials</Title>
 
<HTA:Application
Caption = Yes
Border = Thin
Scroll = Yes
SingleInstance = Yes
ShowInTaskBar = Yes
MaximizeButton = Yes
MinimizeButton = Yes>
 
<script Language = VBScript>
	Dim strINIFile
	Dim strHTAPath
	Dim objFSO
	strINIFile = "defaultuser"
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	
	Sub Window_OnLoad
		intWidth = 800
		intHeight = 600
		On Error Resume Next
		Me.ResizeTo intWidth, intHeight
		Me.MoveTo ((Screen.Width / 2) - (intWidth / 2)),((Screen.Height / 2) - (intHeight / 2))
		Err.Clear
		On Error GoTo 0
	    If Mid(document.location, 6, 3) = "///" Then
	    	strHTAPath = Mid(Replace(Replace(document.location, "%20", " "), "/", "\"), 9)
	    Else
	    	strHTAPath = Mid(Replace(Replace(document.location, "%20", " "), "/", "\"), 6)
	    End If
		strHTAPath = Left(strHTAPath, InStrRev(strHTAPath, "\"))
		If objFSO.FileExists(strHTAPath & strINIFile) = True Then
			Set objFile = objFSO.OpenTextFile(strHTAPath & strINIFile)
			If Not objFile.AtEndOfStream Then strDefaultUser = objFile.ReadLine
			objFile.Close
			cbo_username.Value = strDefaultUser
		End If
	End Sub

	Sub Window_OnUnload
		Set objFile = objFSO.CreateTextFile(strHTAPath & strINIFile, True)
		objFile.Write cbo_username.Value
		objFile.Close
	End Sub
	
	Sub Default_Buttons
		If Window.Event.KeyCode = 13 Then
			btn_run.Click
		End If
	End Sub

	Sub Run_ADUC
		Set objFSO = CreateObject("Scripting.FileSystemObject")
		Set objNetwork = CreateObject("WScript.Network")
		Set objShell = CreateObject("WScript.Shell")
		strPSExec = "\\server\share\psexec.exe"
		strPSExec = objFSO.GetFile(strPSExec).ShortPath
		strCommand = strPSExec & " -accepteula -e -i -u " & cbo_username.Value & " -p " & txt_password.Value & " \\" & objNetwork.ComputerName & " mmc dsa.msc /domain=" & cbo_domain.Value
		objShell.Run strCommand, 0, False
	End Sub
</script>
<body style="background-color:#B0C4DE;" onkeypress='vbs:Default_Buttons'>
	<table height="90%" width width= "90%" border="0" align="center">
		<tr>
			<td align="center" colspan="2">
				<h2>Run ADUC With Alternate Credentials</h2>
			</td>
		</tr>
		<tr>
			<td>
				Select domain (FQDN):
			</td>
			<td>
				<select id="cbo_domain" name="cbo_domain" size="1">
					<option id="opt_domain1" value="domain1.com">domain1.com
					<option id="opt_domain2" value="domain2.com">domain2.com
				</select>
			</td>
		</tr>
		<tr>
			<td>
				Select username (DOMAIN/Username):
			</td>
			<td>
				<select id="cbo_username" name="cbo_username" size="1">
					<option id="opt_username1" value="domain1\administrator">domain1\administrator
					<option id="opt_username2" value="domain2\administrator">domain2\administrator
				</select>
			</td>
		</tr>
		<tr>
			<td>
				Password:
			</td>
			<td>
				<input type="password" id="txt_password" name="txt_password" size="50" maxlength="40"
			</td>
		</tr>
		<tr align="center">
			<td colspan="2">
				<button name="btn_run" id="btn_run" accessKey="R" onclick="vbs:Run_ADUC"><u>R</u>un ADUC</button>
			</td>
		</tr>
	</table>
</body>
</head>
</html>

Open in new window

Author

Commented:
Thanks Rob works perfect... :-)