Link to home
Start Free TrialLog in
Avatar of jbyrd1981
jbyrd1981Flag for United States of America

asked on

Scripting Question

I have the following code snippet... I am trying to figure out why it displays the first message box even if the command fails. Also, I would like it to keep the CMD window open after running psexec until the user hits any key to continue. Thanks!

Set objShell = CreateObject("WScript.Shell")
	btn_settz.disabled = True
	btn_rename.disabled = True
	sOldName = txt_oldname.Value
	sNewName = txt_newname.Value
	intReturn = objShell.Run("c:\windows\system32\psexec...
If intReturn = 0 Then
		MsgBox "Please Check CMD window if rename was successful"
	Else
		MsgBox "Rename failed with return code " & intReturn 
	End If

Open in new window

Avatar of WebDevEM
WebDevEM
Flag of United States of America image

Try this -
Sub Pause(strPause)
     WScript.Echo (strPause)
     z = WScript.StdIn.Read(1)
End Sub

Set objShell = CreateObject("WScript.Shell")
	btn_settz.disabled = True
	btn_rename.disabled = True
	sOldName = txt_oldname.Value
	sNewName = txt_newname.Value
	intReturn = objShell.Run("c:\windows\system32\psexec...
If intReturn = 0 Then (MsgBox "Please Check CMD window if rename was successful")
If intReturn <> 0 Then (MsgBox "Rename failed with return code " & intReturn )

Pause("Press Enter to continue")

Open in new window

http://www.w3schools.com/vbscript/vbscript_conditionals.asp
Avatar of Steve Knight
The psexec command should return the code from whatever you are running with it.  Depending upon what that is, does it return an error code?

You could get psexec to run a number of commands in a batch file you drop / create on the remote computer, or add & pause to the existing command to get it to pause in the command window.

Steve
Avatar of jbyrd1981

ASKER

I am getting errors when launching... Probably cause I am doing something wrong. Here is your code and more code.

Sub RenameComputer
	If txt_admname.Value = "" Then
		MsgBox "Please enter an admin username"
		txt_admname.focus
		Exit Sub
	End If
	If txt_admpass.Value = "" Then
		MsgBox "Please enter an admin password"
		txt_admpass.focus
		Exit Sub
	End If
	If txt_newname.Value = "" Then
		MsgBox "Please enter a new computer name"
		txt_newname.focus
		Exit Sub
	End If

	End Sub

	Sub Pause(strPause)
     		WScript.Echo (strPause)
     		z = WScript.StdIn.Read(1)
	
	Set objShell = CreateObject("WScript.Shell")
	btn_settz.disabled = True
	btn_rename.disabled = True
	sOldName = txt_oldname.Value
	sNewName = txt_newname.Value
	intReturn = objShell.Run("c:\windows\system32\psexec...
                  If intReturn = 0 Then (MsgBox "Please Check CMD window if rename was successful")
	If intReturn <> 0 Then (MsgBox "Rename failed with return code " & intReturn )
	
	Pause("Press Enter to continue")
	
	End If

	btn_settz.disabled = False
	btn_rename.disabled = False
End Sub

Open in new window

In this instance the psexec command is calling netdom.
I'm on the road now so typing on the phone will be brief. Try moving the pause sub outside your other sub - I don't think you can define one within another
For starters put the

Sub Pause(strPause)
                 WScript.Echo (strPause)
                 z = WScript.StdIn.Read(1)
End Sub

below your existing End Sub at the end or above the RenameComputers Sub.

These two lines should not have () around them:

            If intReturn = 0 Then MsgBox "Please Check CMD window if rename was successful"
      If intReturn <> 0 Then MsgBox "Rename failed with return code " & intReturn
Sorry about that... Dragon- it is right about the parens. My laptop won't run vbs at the moment so I couldn't test it.
Hi, this should at least work syntax wise, so see how you go.  It's a mash of the above suggestions...nothing new...

Rob.

Sub RenameComputer
	If txt_admname.Value = "" Then
		MsgBox "Please enter an admin username"
		txt_admname.focus
		Exit Sub
	End If
	If txt_admpass.Value = "" Then
		MsgBox "Please enter an admin password"
		txt_admpass.focus
		Exit Sub
	End If
	If txt_newname.Value = "" Then
		MsgBox "Please enter a new computer name"
		txt_newname.focus
		Exit Sub
	End If

End Sub

Sub Pause(strPause)
     		WScript.Echo (strPause)
     		z = WScript.StdIn.Read(1)
End Sub
    
Sub RunPSExec()
	
	Set objShell = CreateObject("WScript.Shell")
	btn_settz.disabled = True
	btn_rename.disabled = True
	sOldName = txt_oldname.Value
	sNewName = txt_newname.Value
	intReturn = objShell.Run("c:\windows\system32\psexec.exe...")
    If intReturn = 0 Then
    	MsgBox "Please Check CMD window if rename was successful"
	ElseIf intReturn <> 0 Then
		MsgBox "Rename failed with return code " & intReturn 
	End If
	
	Pause("Press Enter to continue")

	btn_settz.disabled = False
	btn_rename.disabled = False
End Sub

Open in new window

the above works with no errors, however, the button I click to execute the code does nothing now. :(
Which Sub procedure is your button running?  I think it should be running the RunPSExec() procedure.

Rob.
Ok, I figured out what needed changed with your comment above and it is working now. However the CMD window sill closed after psexec ran. It does not wait for the user to review for errors.
Are you meaning pausing on the local machine or remote screen? Have you tried adding  " & pause" at the end of the psexec command line?
Just realized it did have an error at the end of the execution and probably why the pause is not working. I got this... Line: 201
Error: Object required: 'WScript'

Line 201 is this sub

Sub Pause(strPause)
     		WScript.Echo (strPause)
     		z = WScript.StdIn.Read(1)
End Sub

Open in new window

I have trie the & pause and it does not seem to work... Is the placement key? I put it after the /force... Also, it should pause on the local machine not remote.
How much more to this script is there?  Also this needs to be run with

cscript //nologo yourscript.vbs

not just launched from explorer otherwise wscript.stdin.read(1) will error

Steve
Actually... Looking back... the entire script was written by Rob. :) it can be found in the past question here.

https://www.experts-exchange.com/questions/27671290/Script-for-Netdom-and-Time-Zone.html

As I have been using it I have noticed that netdom doesn't seem to work all the time. If it fails the popup still comes up and says successful even if it was not. This is why I need the command window to stay up so that the error is displayed there. Now that I think about it... I wonder if the successful popup window is being displayed because it is determining this based on the psexec being successful and not netdom? That would make so much sense.
In that script that is the accepted answer, it's a HTA, but I think you must already be running the HTA, because otherwise it wouldn't work at all.

But also in there, there is this line that builds the netdom command:
	intReturn = objShell.Run("cmd.exe /K netdom renamecomputer " & sOldName & " /newname:" & sNewName & " /userd:MYDOMAIN\" & txt_admname.Value & " /passwordd:" & txt_admpass.Value & " /usero:MYDOMAIN\" & txt_admname.Value & " /passwordo:" & txt_admpass.Value & " /force")

Open in new window


As long as the cmd.exe /K is there at the start, the command prompt should open, complete the command, and then wait for the user to close it.

The only reason it wouldn't (I think) is if the syntax of the entire command was wrong.  Change that line to this:
	strCommand = "cmd.exe /K netdom renamecomputer " & sOldName & " /newname:" & sNewName & " /userd:MYDOMAIN\" & txt_admname.Value & " /passwordd:" & txt_admpass.Value & " /usero:MYDOMAIN\" & txt_admname.Value & " /passwordo:" & txt_admpass.Value & " /force"
	InputBox "Running command", "Running command", strCommand
	intReturn = objShell.Run(strCommand)

Open in new window


Then, when you run it, the command will be presented in an input box.  From there, you can copy and paste that directly into a manually started command prompt, and see if the command returns any errors.

Regards,

Rob.
The reason netdom failed is because with the cmd from the previous code executes from the local machine to reach out to the remote machine. I am not sure why this happens on our network, but the XP systems seemed more problematic with this than Win 7. Since this was occuring, I changed the line in the code to run netdom with Psexec so that it executed on the remote machine. This greatly reduced the number of failures on these systems. Everything else in the code from the previous post should be the same. I just cannot get it to keep the CMD window open. I think the successful popup window can be misleading because it appears before psexec executes netdom.

intReturn = objShell.Run("c:\windows\system32\psexec -accepteula -c -f -u XYZ\" & txt_admname.Value & " -p " & txt_admpass.Value & " c:\Windows\System32\netdom.exe \\" & txt_oldname.Value & " renamecomputer " & sOldName & " /newname:" & sNewName & " /ud:XYZ\" & txt_admname.Value & " /pd:" & txt_admpass.Value & " /uo:XYZ\" & txt_admname.Value & " /po:" & txt_admpass.Value & " /force")

Open in new window

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
Rob, again... THANK YOU!!! I knew it had to be something that simple. The windows stay open now to verify the results. :) Now I am wondering about going down the path of adding Netdom Join to this script... May open that one up in another question.
Sure no problem. Thanks for the grade.

Rob.