Posted By

RobertHirabayashi on 02/10/12


Tagged

number directory mobile ip user active ad office Workstations AddressHost NamePhone


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

ionman


WhoIsLoggedIn.vbs


 / 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.

  1. On Error Resume Next
  2.  
  3. WScript.StdOut.Write("IP Address/Computer Name> ")
  4. WScript.StdIn.Read(0)
  5. strComputer = WScript.StdIn.ReadLine()
  6.  
  7. If IsHostAlive(strComputer) = False Then Wscript.quit
  8.  
  9. For Each strComputer In arrComputers
  10. Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
  11. Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
  12. For Each objItem in colItems
  13. If IsNull(objItem.UserName) = False Then
  14. Wscript.Echo "Computer Name: " & objItem.DNSHostName
  15. Wscript.Echo "UserName: " & objItem.UserName
  16. GetUserName(objItem.UserName)
  17. Else
  18. Wscript.Echo "Computer Name: " & objItem.DNSHostName
  19. Wscript.Echo "UserName: Null"
  20. End If
  21. Next
  22. Next
  23.  
  24. '*******************************************************************************
  25.  
  26. Function GetUserName(LoginID)
  27. ' Constants for the NameTranslate object.
  28. Const ADS_NAME_INITTYPE_GC = 3
  29. Const ADS_NAME_TYPE_NT4 = 3
  30. Const ADS_NAME_TYPE_1779 = 1
  31.  
  32. ' Determine DNS name of domain from RootDSE.
  33. Set objRootDSE = GetObject("LDAP://RootDSE")
  34. strDNSDomain = objRootDSE.Get("defaultNamingContext")
  35.  
  36. ' Use the NameTranslate object to find the NetBIOS domain name from the
  37. ' DNS domain name.
  38. Set objTrans = CreateObject("NameTranslate")
  39. objTrans.Init ADS_NAME_INITTYPE_GC, ""
  40. objTrans.Set ADS_NAME_TYPE_1779, strDNSDomain
  41. strNetBIOSDomain = objTrans.Get(ADS_NAME_TYPE_NT4)
  42. ' Remove trailing backslash.
  43. strNetBIOSDomain = Left(strNetBIOSDomain, Len(strNetBIOSDomain) - 1)
  44.  
  45. strNTName = Trim(LoginID)
  46.  
  47. ' Use the Set method to specify the NT format of the object name.
  48. ' Trap error if user does not exist.
  49. On Error Resume Next
  50.  
  51. objTrans.Set ADS_NAME_TYPE_NT4, strNTName
  52.  
  53. If (Err.Number = 0) Then
  54. On Error GoTo 0
  55. ' Use the Get method to retrieve the RPC 1779 Distinguished Name.
  56. strUserDN = objTrans.Get(ADS_NAME_TYPE_1779)
  57.  
  58. ' Bind to the user object (if desired).
  59. Set objUser = GetObject("LDAP://" & strUserDN)
  60. ' Do whatever you want...
  61. wscript.echo "Name: " & objUser.GivenName & " " & objUser.sn
  62. wscript.echo "Phone: " & objUser.physicalDeliveryOfficeName
  63. wscript.echo "Office: " & objUser.telephonenumber
  64. wscript.echo "Mobile: " & objUser.mobile
  65. Else
  66. On Error GoTo 0
  67. ' Alert user about bad user name.
  68. Wscript.Echo "User " & strNTName & " does not exist"
  69. End If
  70. End Function
  71.  
  72.  
  73. Function IsHostAlive(strComputer)
  74. IsHostAlive = False ' normally we will return false, unless ping is successful
  75.  
  76. Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
  77. Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_PingStatus WHERE Address = '" + strComputer + "'")
  78.  
  79. For Each oRow In colItems
  80. Select Case oRow.StatusCode
  81. Case 0
  82. IsHostAlive = True
  83. wscript.echo strComputer & " is alive..."
  84. Case 11001 wscript.echo "Buffer Too Small"
  85. Case 11002 wscript.echo "Destination Net Unreachable"
  86. Case 11003 wscript.echo "Destination Host Unreachable"
  87. Case 11004 wscript.echo "Destination Protocol Unreachable"
  88. Case 11005 wscript.echo "Destination Port Unreachable"
  89. Case 11006 wscript.echo "No Resources"
  90. Case 11007 wscript.echo "Bad Option"
  91. Case 11008 wscript.echo "Hardware Error"
  92. Case 11009 wscript.echo "Packet Too Big"
  93. Case 11010 wscript.echo "Request Timed Out"
  94. Case 11011 wscript.echo "Bad Request"
  95. Case 11012 wscript.echo "Bad Route"
  96. Case 11013 wscript.echo "TimeToLive Expired Transit"
  97. Case 11014 wscript.echo "TimeToLive Expired Reassembly"
  98. Case 11015 wscript.echo "Parameter Problem"
  99. Case 11016 wscript.echo "Source Quench"
  100. Case 11017 wscript.echo "Option Too Big"
  101. Case 11018 wscript.echo "Bad Destination"
  102. Case 11032 wscript.echo "Negotiating IPSEC"
  103. Case 11050 wscript.echo "General Failure"
  104. Case Else wscript.echo "Status code " & objPing.StatusCode & " - Unable to determine cause of failure."
  105. End Select
  106. Next
  107. End Function

Report this snippet  

You need to login to post a comment.