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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error return code from authentication

Status
Not open for further replies.

bertieuk

IS-IT--Management
Jun 1, 2004
175
I have written a command line script to return the Dell Service tag of a local or remote machine. I check the return error code to establish whether the machine running the script authenticates and then moves on. The idea was to have three attempts at authentication.....after that the script would either display or end. I use a do until loop to check for the three times.

The return codes seems to be.....
If I get the username right first time - 70 (decimal)
If I get it wrong the first time - 424 (decimal)

If I then try to get it right I still get a return code of 424.

I am stuck on this one.

The code is....

Code:
On Error Resume Next
strNamespace = "root\cimv2"

Function ping(hostname)
	set wshShell = CreateObject("WScript.Shell")
	ping = Not CBool(wshShell.run("ping -n 1 " & hostname,0,True))
End Function

'------------------------------------------------------------------------------------

Wscript.Echo
Wscript.Echo " Get Dell Service Tag Number"
Wscript.Echo " ---------------------------"
Wscript.Echo
Wscript.Echo " This script will return the Dell Service Tag of a local or remote server."
Wscript.Echo " If connecting to a remote machine and the local cached credentials differ,"
Wscript.Echo " then you will be asked to authenticate."
Wscript.Echo
Wscript.Echo


Wscript.StdOut.Write " Enter the Computer Name or IP Address: "
strComputer = WScript.StdIn.ReadLine

If strComputer = "" then 
	wscript.quit
End If

' Check the Machine is contactable

If ping(strComputer) Then
	Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" &_ 
		strComputer & "\root\cimv2")
	' If the machine is local or the cached user credentials are correct then the returned
	' error code is zero.  If a non zero value is returned then ask for authentication details.
	If Err.Number = 0 then
		Set colSMBIOS = objWMIService.ExecQuery ("Select * from Win32_SystemEnclosure")
		For Each objSMBIOS in colSMBIOS
    			Wscript.Echo vbCRLF & " Dell Service tag (serial number): " & objSMBIOS.SerialNumber
		Next
		Wscript.Echo
	Else
    		' Ask for a valid username and password.  Upto three attempts can be made
		
		ValidFlag = FALSE
		TryCount = 0
		Do Until ValidFlag = TRUE OR Trycount = 3  
			If TryCount <> 0 Then
				Wscript.Echo " Authentication Failed - Try " & TryCount +1
			End If

			Wscript.StdOut.Write " Please enter the user name: "
			strUser = WScript.StdIn.ReadLine

			Set objPassword = CreateObject("ScriptPW.Password") 
			WScript.StdOut.Write " Please enter your password (There will be NO display): " 
			strPassword = objPassword.GetPassword() 
 			Wscript.Echo

			Set objWbemLocator = CreateObject("WbemScripting.SWbemLocator")
			Set objWMIService = objwbemLocator.ConnectServer _
    				(strComputer, strNamespace, strUser, strPassword)
			objWMIService.Security_.authenticationLevel = WbemAuthenticationLevelPktPrivacy
			Wscript.Echo Err.Number

			' Error code 70 returned for successful remote authentication 
			If Err.Number = 70 then
				Set colSMBIOS = objWMIService.ExecQuery ("Select * from Win32_SystemEnclosure")
				For Each objSMBIOS in colSMBIOS
    					Wscript.Echo vbCRLF & " Dell Service tag (serial number): " & objSMBIOS.SerialNumber
				Next
				ValidFlag = TRUE
			End If

			TryCount = TryCount + 1
			'If TryCount = 3 Then
			'	ValidFlag = TRUE
			'End If
		Loop
		If TryCount = 3 Then
			Wscript.Echo " You've had three authenticate attempts......Script Now Ending..."
			Wscript.Quit (1)
		End If
	End If
	
Else
' Machine was not contactable
Wscript.echo "The computer is not pingable"
End If


Thanks
Si
 
The behaviours you seem to abstract from the running of the script as such are all illusion and self-misleading. The script is defective.

[1] Declare the constant is needed.
[tt]Const WbemAuthenticationLevelPktPrivacy = 6[/tt]

[2] The err.number won't clear itself. You have to clear it.
[tt]>Else
[red]err.clear[/red] 'add this line
> ' Ask for a valid username and password. Upto three attempts can be made
[/tt]
[3] It is the locator which has an issue to set its authentication level.
[tt]
>Set objWbemLocator = CreateObject("WbemScripting.SWbemLocator")
>Set objWMIService = objwbemLocator.ConnectServer _
(strComputer, strNamespace, strUser, strPassword)
>objWMIService.Security_.authenticationLevel = WbemAuthenticationLevelPktPrivacy [red]_wrong_[/red]
[/tt]
should be this.
[tt]
Set objWbemLocator = CreateObject("WbemScripting.SWbemLocator")
objWbemLocator.Security_.authenticationLevel = WbemAuthenticationLevelPktPrivacy
Set objWMIService = objwbemLocator.ConnectServer _
(strComputer, strNamespace, strUser, strPassword)
[/tt]
[4] Consequential to [3], the err.number=70 conditional is fundamentally flawed. It should rather something like this.
[tt]
If Err.number[red]=0[/red] then
'set colsmbios etc...
End If
[red]Err.clear[/red]
'...etc
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top