• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 768
  • Last Modified:

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

0
jbyrd1981
Asked:
jbyrd1981
  • 9
  • 5
  • 4
  • +1
1 Solution
 
WebDevEMCommented:
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
0
 
Steve KnightIT ConsultancyCommented:
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
0
 
jbyrd1981Author Commented:
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

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jbyrd1981Author Commented:
In this instance the psexec command is calling netdom.
0
 
WebDevEMCommented:
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
0
 
Steve KnightIT ConsultancyCommented:
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
0
 
WebDevEMCommented:
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.
0
 
RobSampsonCommented:
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

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

Rob.
0
 
jbyrd1981Author Commented:
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.
0
 
Steve KnightIT ConsultancyCommented:
Are you meaning pausing on the local machine or remote screen? Have you tried adding  " & pause" at the end of the psexec command line?
0
 
jbyrd1981Author Commented:
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

0
 
jbyrd1981Author Commented:
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.
0
 
Steve KnightIT ConsultancyCommented:
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
0
 
jbyrd1981Author Commented:
Actually... Looking back... the entire script was written by Rob. :) it can be found in the past question here.

http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_27671290.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.
0
 
RobSampsonCommented:
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.
0
 
jbyrd1981Author Commented:
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

0
 
RobSampsonCommented:
Does it help if you use:
intReturn = objShell.Run("cmd /k 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


Regards,

Rob.
0
 
jbyrd1981Author Commented:
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.
0
 
RobSampsonCommented:
Sure no problem. Thanks for the grade.

Rob.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 9
  • 5
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now