/ Published in: Visual Basic

Run this script from a command prompt, and you'll be asked for the IP Address or Host Name. Script will first check if PC is ping-able and will return an error message on any unreachable host. If the computer is running, script will return with user details about the logged in user from Active Directory.
Expand |
Embed | Plain Text
On Error Resume Next WScript.StdOut.Write("IP Address/Computer Name> ") WScript.StdIn.Read(0) strComputer = WScript.StdIn.ReadLine() If IsHostAlive(strComputer) = False Then Wscript.quit For Each strComputer In arrComputers Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48) For Each objItem in colItems If IsNull(objItem.UserName) = False Then Wscript.Echo "Computer Name: " & objItem.DNSHostName Wscript.Echo "UserName: " & objItem.UserName GetUserName(objItem.UserName) Else Wscript.Echo "Computer Name: " & objItem.DNSHostName Wscript.Echo "UserName: Null" End If Next Next '******************************************************************************* Function GetUserName(LoginID) ' Constants for the NameTranslate object. Const ADS_NAME_INITTYPE_GC = 3 Const ADS_NAME_TYPE_NT4 = 3 Const ADS_NAME_TYPE_1779 = 1 ' Determine DNS name of domain from RootDSE. Set objRootDSE = GetObject("LDAP://RootDSE") strDNSDomain = objRootDSE.Get("defaultNamingContext") ' Use the NameTranslate object to find the NetBIOS domain name from the ' DNS domain name. Set objTrans = CreateObject("NameTranslate") objTrans.Init ADS_NAME_INITTYPE_GC, "" objTrans.Set ADS_NAME_TYPE_1779, strDNSDomain strNetBIOSDomain = objTrans.Get(ADS_NAME_TYPE_NT4) ' Remove trailing backslash. strNetBIOSDomain = Left(strNetBIOSDomain, Len(strNetBIOSDomain) - 1) strNTName = Trim(LoginID) ' Use the Set method to specify the NT format of the object name. ' Trap error if user does not exist. On Error Resume Next objTrans.Set ADS_NAME_TYPE_NT4, strNTName If (Err.Number = 0) Then On Error GoTo 0 ' Use the Get method to retrieve the RPC 1779 Distinguished Name. strUserDN = objTrans.Get(ADS_NAME_TYPE_1779) ' Bind to the user object (if desired). Set objUser = GetObject("LDAP://" & strUserDN) ' Do whatever you want... wscript.echo "Name: " & objUser.GivenName & " " & objUser.sn wscript.echo "Phone: " & objUser.physicalDeliveryOfficeName wscript.echo "Office: " & objUser.telephonenumber wscript.echo "Mobile: " & objUser.mobile Else On Error GoTo 0 ' Alert user about bad user name. Wscript.Echo "User " & strNTName & " does not exist" End If End Function Function IsHostAlive(strComputer) IsHostAlive = False ' normally we will return false, unless ping is successful Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_PingStatus WHERE Address = '" + strComputer + "'") For Each oRow In colItems Select Case oRow.StatusCode Case 0 IsHostAlive = True wscript.echo strComputer & " is alive..." Case 11001 wscript.echo "Buffer Too Small" Case 11002 wscript.echo "Destination Net Unreachable" Case 11003 wscript.echo "Destination Host Unreachable" Case 11004 wscript.echo "Destination Protocol Unreachable" Case 11005 wscript.echo "Destination Port Unreachable" Case 11006 wscript.echo "No Resources" Case 11007 wscript.echo "Bad Option" Case 11008 wscript.echo "Hardware Error" Case 11009 wscript.echo "Packet Too Big" Case 11010 wscript.echo "Request Timed Out" Case 11011 wscript.echo "Bad Request" Case 11012 wscript.echo "Bad Route" Case 11013 wscript.echo "TimeToLive Expired Transit" Case 11014 wscript.echo "TimeToLive Expired Reassembly" Case 11015 wscript.echo "Parameter Problem" Case 11016 wscript.echo "Source Quench" Case 11017 wscript.echo "Option Too Big" Case 11018 wscript.echo "Bad Destination" Case 11032 wscript.echo "Negotiating IPSEC" Case 11050 wscript.echo "General Failure" Case Else wscript.echo "Status code " & objPing.StatusCode & " - Unable to determine cause of failure." End Select Next End Function
You need to login to post a comment.