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....
Thanks
Si
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