Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error Trapping using WshShell.Run

Status
Not open for further replies.

sromine

Technical User
Apr 21, 2006
38
0
0
US
Have a problem that seems simple but is driving me insane and would be very grateful if someone could give me some assistance. What I need is to verify a command executes or not. The command in strCommand has the incorrect username and password and errors out if I do it on the command line. But, for some reason Err.Number always returns 0. Am I not catching the error correctly?

Let me say ultimately what I am trying to do is verify a username and password on a remote machine, if the username and password is not correct then I am going to set it to another username and password. The issue is that we have some machines with an administrator username of xxxx and other machines with a administrator username of yyyy...I need to identify which is the correct username and password so I can execute additional script....I mention this in case someone has a better way of doing this....

Code:
On Error Resume Next

Dim WshShell, strCommand
Set WshShell = Wscript.CreateObject("Wscript.Shell")
 

strCommand = "NET USE \\libvgtrts\IPC$ /USER:local\admin hello123"
WshShell.Run strcommand, 0, true
wscript.echo Err.Number


 
How about this?
Code:
Dim WshShell, strCommand
Set WshShell = Wscript.CreateObject("Wscript.Shell")
 

strCommand = "NET USE \\libvgtrts\IPC$ /USER:local\admin hello123"
rtnErr = WshShell.Run(strCommand, 0, True)
wscript.echo rtnErr


--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
'there are lots of pros and cons to handle errors, should you ever have on error resume next in the sub? blaa blaa
'i like the sub main approach with on error resume next globally as it makes sure that cscript.exe/wscript.exe returns non zero on run time errors but doesnt have turn on error resume next in all your functions/subs



Option Explicit
On Error Resume Next
Dim intError

Call Main()
If intError <> 0 Then
Wscript.Quit intError
Else
Wscript.Quit Err.Number 'if you really want to?
End If

'#################################
Sub Main()
Dim WshShell, strCommand
Set WshShell = Wscript.CreateObject("Wscript.Shell")
strCommand = "NET USE \\libvgtrts\IPC$ /USER:local\admin hello123"
'On Error Resume Next, should we turn it on here???
intError = WshShell.Run strcommand, 0, true
'On Error Goto 0
Set WshShell = Nothing
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top