mbt masai
 
Welcome !
         

                                
After experiencing a lot of down time, We decided to move this site to CrystalTech.com. CrystalTech.com is powered by only the finest Windows servers providing the best performance, reliability, and value anywhere.

 Getting User Display Name by Quering Workstation

Author Message
inamays

  • Total Posts : 5
  • Scores: 0
  • Reward points : 0
  • Joined: 3/10/2010
  • Status: offline
Getting User Display Name by Quering Workstation Wednesday, March 10, 2010 7:51 AM (permalink)
0
Greetings, I've written a little script which fetches the last logged-on user of a workstation ID as well as the computer manufacturer and model. It then outputs the data to be displayed in an explorer window. While I'm calling and retrieving the user id (network logon), I also want to show the Display Name (Last, First) of the logged on user and being the complete noob that I am, I just can't figure this crap out! Can someone please tell me how to display the users actual name???
 
Here's my code:
     '---------------------- 
     ' Title: Last Logged-on User 
     ' Why: To quickly show the last logged on user of a specified workstation. This will not return a user when the machine is off the network. 
     '----------------------            On Error Resume Next 
     Const HKEY_CURRENT_USER = &H80000001 
     Const HKEY_LOCAL_MACHINE = &H80000002            Dim strComputer, objWMIService, propValue, objItem 
     Dim strUserName, strPassword, colItems, SWBemlocator            ' This section querries for the workstation to be scanned. 
     strComputer = inputbox ("Enter the workstation ID to scan.", "Logged-on User",".") 
     UserName = "" 
     Password = ""            'Sets up the connections and opjects to be used throughout the script. 
     Set SWBemlocator = CreateObject("WbemScripting.SWbemLocator") 
     Set objWMIService = SWBemlocator.ConnectServer(strComputer,"root\CIMV2",strUserName,strPassword) 
     Set WshNetwork = WScript.CreateObject("WScript.Network")            'Gets the workstation ID to display in the results. 
     Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem",,48) 
     For Each objItem in colItems 
         WSID = objItem.CSName 
     Next            'This returns current logged on user, manufacture and model. 
     Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48) 
     For Each objItem in colItems 
         UserName = objItem.UserName 
         Manufacturer = objItem.Manufacturer 
         Model = objItem.Model 
     Next            'This sets up the Internet Explorer window in order to show the results. 
     Set objExplorer = WScript.CreateObject("InternetExplorer.Application", "IE_") 
     With objExplorer 
         .Navigate "about:Blank" 
         .Toolbar = 0 
         .StatusBar = 0 
         .Width = 480 
         .Height = 480 
         .Left = 75 
         .Top = 75 
         .Visible = 1 
     End With      Set fileOutput = objExplorer.Document            fileOutput.WriteLn "<html>" 
     fileOutput.WriteLn "<head>" 
     fileOutput.WriteLn "<title>System Information for '" & WSID & "'</title>" 
     fileOutput.WriteLn "</head>" 
     fileOutput.WriteLn "<body>" 
     fileOutput.WriteLn "<table width='100%'  border='0' cellspacing='1' cellpadding='1' align='center'>" 
     fileOutput.WriteLn "<tr>" 
     fileOutput.WriteLn "<td colspan='2' bgcolor='#999999'><div align='left'>Current Logged on User for " & WSID & "</div></td>" 
     fileOutput.WriteLn "</tr>" 
     fileOutput.WriteLn "<tr>" 
     fileOutput.WriteLn "<td width='30%' bgcolor='#e0e0e0'>Username:</td>" 
     fileOutput.WriteLn "<td width='70%' bgcolor='#f0f0f0'>" & UserName & " ("WHERE_I_WANT_LAST,FIRST_DISPLAYED)</td>" 
     fileOutput.WriteLn "</tr>" 
     fileOutput.WriteLn "<tr>" 
     fileOutput.WriteLn "<td width='30%' bgcolor='#e0e0e0'>WS Model:</td>" 
     fileOutput.WriteLn "<td width='70%' bgcolor='#f0f0f0'>" & Manufacturer & " | " & Model & "</td>" 
     fileOutput.WriteLn "</tr>" 
     fileOutput.WriteLn "</table>" 
     fileOutput.WriteLn "<br><div align='center'><font size='-1'>&copy; 2010 COMPANY NAME</font></div>" 
     fileOutput.WriteLn "</body>" 
     fileOutput.WriteLn "</html>" fileOutput.close 
     WScript.Quit
 
 
<message edited by inamays on Wednesday, March 10, 2010 7:56 AM>
#1
    Deckyon

    • Total Posts : 45
    • Scores: 0
    • Reward points : 0
    • Joined: 8/1/2006
    • Location: Louisville, KY - USA
    • Status: offline
    Re:Getting User Display Name by Quering Workstation Wednesday, March 10, 2010 7:55 AM (permalink)
    0
    Look into WMIService for the Win32_NetworkLoginProfile for the FullName objItem.   You will want to include a "Where" or it will list all the users with an account on the machine.
    #2
      inamays

      • Total Posts : 5
      • Scores: 0
      • Reward points : 0
      • Joined: 3/10/2010
      • Status: offline
      Re:Getting User Display Name by Quering Workstation Wednesday, March 10, 2010 8:20 AM (permalink)
      0
      Sorry to be a putz, but can you provide some direction as to what you mean? How can I impliment your recommendation into the code I have? I'm SERIOUSLY a noob.
      #3
        Deckyon

        • Total Posts : 45
        • Scores: 0
        • Reward points : 0
        • Joined: 8/1/2006
        • Location: Louisville, KY - USA
        • Status: offline
        Re:Getting User Display Name by Quering Workstation Wednesday, March 10, 2010 8:48 AM (permalink)
        0
        Here is a script I use to grab information for the current logged in user.  Probably overkill, but easy enough.

         On Error Resume Next
         Const ForAppending = 8
         Dim strComputer
         Dim objWMIService
         Dim propValue
         Dim colItems
         strComputer = inputbox ("Workstation", "Enter the workstation ID to scan.",".")
         strTextFile = "ADUserInfo.txt"
         
         Set objFSO = CreateObject("Scripting.FileSystemObject")
         
         If objFSO.FileExists(strTextFile) = True Then
             objFSO.DeleteFile(strTextFile)
         End If
         
         Set objTextFile = objFSO.OpenTextFile(strTextFile, ForAppending, True)
         Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
         
         Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
         For Each objItem in colItems
             strComputer = objItem.Name
             strUserID = objItem.UserName
             arrUserName = Split(strUserID, "\", -1, 1)
             strUserID = arrUserName(1)
         Next
         Set colItems = Nothing
         Set objItem = Nothing
         
         objTextFile.WriteLine("==========================================================================")
         objTextFile.WriteLine("Computer: " & strComputer)
         objTextFile.WriteLine("UserID: " & strUserID)
         objTextFile.WriteLine("==========================================================================")
         Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkLoginProfile Where Caption = '" & strUserID & "'",,48)
         For Each objItem in colItems
             objTextFile.WriteLine("AccountExpires: " & objItem.AccountExpires)
             objTextFile.WriteLine("AuthorizationFlags: " & objItem.AuthorizationFlags)
             objTextFile.WriteLine("BadPasswordCount: " & objItem.BadPasswordCount)
             objTextFile.WriteLine("Caption: " & objItem.Caption)
             objTextFile.WriteLine("CodePage: " & objItem.CodePage)
             objTextFile.WriteLine("Comment: " & objItem.Comment)
             objTextFile.WriteLine("CountryCode: " & objItem.CountryCode)
             objTextFile.WriteLine("Description: " & objItem.Description)
             objTextFile.WriteLine("Flags: " & objItem.Flags)
             objTextFile.WriteLine("FullName: " & objItem.FullName)
             objTextFile.WriteLine("HomeDirectory: " & objItem.HomeDirectory)
             objTextFile.WriteLine("HomeDirectoryDrive: " & objItem.HomeDirectoryDrive)
             objTextFile.WriteLine("LastLogoff: " & objItem.LastLogoff)
             objTextFile.WriteLine("LastLogon: " & objItem.LastLogon)
             objTextFile.WriteLine("LogonHours: " & objItem.LogonHours)
             objTextFile.WriteLine("LogonServer: " & objItem.LogonServer)
             objTextFile.WriteLine("MaximumStorage: " & objItem.MaximumStorage)
             objTextFile.WriteLine("Name: " & objItem.Name)
             objTextFile.WriteLine("NumberOfLogons: " & objItem.NumberOfLogons)
             objTextFile.WriteLine("Parameters: " & objItem.Parameters)
             objTextFile.WriteLine("PasswordAge: " & objItem.PasswordAge)
             objTextFile.WriteLine("PasswordExpires: " & objItem.PasswordExpires)
             objTextFile.WriteLine("PrimaryGroupId: " & objItem.PrimaryGroupId)
             objTextFile.WriteLine("Privileges: " & objItem.Privileges)
             objTextFile.WriteLine("Profile: " & objItem.Profile)
             objTextFile.WriteLine("ScriptPath: " & objItem.ScriptPath)
             objTextFile.WriteLine("SettingID: " & objItem.SettingID)
             objTextFile.WriteLine("UnitsPerWeek: " & objItem.UnitsPerWeek)
             objTextFile.WriteLine("UserComment: " & objItem.UserComment)
             objTextFile.WriteLine("UserId: " & objItem.UserId)
             objTextFile.WriteLine("UserType: " & objItem.UserType)
             objTextFile.WriteLine("Workstations: " & objItem.Workstations)
         Next
         objTextFile.WriteLine("==========================================================================")
         objTextFile.Close
         Set colItems = Nothing
         Set objItem = Nothing
         
         MsgBox "Results have been stored in '" & strTextFile & "'.", 64, "Script Complete"
         WScript.Quit
         

        #4
          inamays

          • Total Posts : 5
          • Scores: 0
          • Reward points : 0
          • Joined: 3/10/2010
          • Status: offline
          Re:Getting User Display Name by Quering Workstation Wednesday, March 10, 2010 9:49 AM (permalink)
          0
          I love this script! However, when I run it on remote computers, anything under
           
          Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkLoginProfile Where Caption = '" & strUserID & "'",,48)
           
          I don't get a return. I'm a network administrator so I should be getting results. Are there ways I can default credentials so that it allows me to retrieve this info? Thanks!
          #5
            Deckyon

            • Total Posts : 45
            • Scores: 0
            • Reward points : 0
            • Joined: 8/1/2006
            • Location: Louisville, KY - USA
            • Status: offline
            Re:Getting User Display Name by Quering Workstation Wednesday, March 10, 2010 9:53 AM (permalink)
            0
            That means your user IDs must be formatted a bit different.  You will need to check your user id format and change the following to match.
             For Each objItem in colItems
                  strComputer = objItem.Name
                 strUserID = objItem.UserName
                 arrUserName = Split(strUserID, "\", -1, 1)
                 strUserID = arrUserName(1)
             Next
            
            #6
              inamays

              • Total Posts : 5
              • Scores: 0
              • Reward points : 0
              • Joined: 3/10/2010
              • Status: offline
              Re:Getting User Display Name by Quering Workstation Wednesday, March 10, 2010 10:11 AM (permalink)
              0
              I'm sorry, I don't understand what variables would make this have to change. In our organization, our domain is
              \\XXX-XXX\USERNAME
              . The username always beings with a letter with 5 numbers following (e.g. Q12345 would be a username).
              #7
                TomRiddle

                • Total Posts : 608
                • Scores: 12
                • Reward points : 0
                • Joined: 2/7/2008
                • Location: Australia
                • Status: offline
                Re:Getting User Display Name by Quering Workstation Wednesday, March 10, 2010 3:01 PM (permalink)
                0
                Try this, if you can get the user id from the remotre machine then the following code will return the full name from AD.


                'Script by Tomriddle 2010 
                on error resume next
                   strUserID=inputbox("Enter UserID", "Return Full Name From AD")
                   Set objRootDSE = GetObject("LDAP://RootDSE")
                   strDomain= objRootDSE.Get("defaultNamingContext")
                   Const ADS_SCOPE_SUBTREE = 2
                   Set objConnection = CreateObject("ADODB.Connection")
                   Set objCommand =   CreateObject("ADODB.Command")
                   objConnection.Provider = "ADsDSOObject"
                   objConnection.Open "Active Directory Provider"
                   Set objCommand.ActiveConnection = objConnection
                   objCommand.Properties("Page Size") = 1000
                   objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
                   objCommand.CommandText = _
                      "SELECT distinguishedName FROM 'LDAP://"&strDomain&"' WHERE objectCategory='user' " & _
                         "AND sAMAccountName='"& strUserID &"'"
                   Set objRecordSet = objCommand.Execute
                   objRecordSet.MoveFirst
                   Do Until objRecordSet.EOF
                      DistinguishedADPath=objRecordSet.Fields("distinguishedName").Value
                      objRecordSet.MoveNext
                   Loop
                   if DistinguishedADPath="" then
                      Msgbox strUserID&" Not found in AD"
                   else
                      Set objUser = GetObject ("LDAP://" & DistinguishedADPath)
                      msgbox objUser.displayName
                   end if
                <message edited by TomRiddle on Wednesday, March 10, 2010 3:04 PM>
                -join([int[]][char[]]'Ut|jwXmjqq%Wzqjx'|%{[char]($_-5)})
                #8
                  inamays

                  • Total Posts : 5
                  • Scores: 0
                  • Reward points : 0
                  • Joined: 3/10/2010
                  • Status: offline
                  Re:Getting User Display Name by Quering Workstation Thursday, March 11, 2010 9:12 AM (permalink)
                  0
                  You're a blasted genius!
                  #9
                    TomRiddle

                    • Total Posts : 608
                    • Scores: 12
                    • Reward points : 0
                    • Joined: 2/7/2008
                    • Location: Australia
                    • Status: offline
                    Re:Getting User Display Name by Quering Workstation Thursday, March 11, 2010 10:27 AM (permalink)
                    0
                    Thanks, but it took a very long time to put that together with bits and peices I have found over the years. 

                       Set objRootDSE = GetObject("LDAP://RootDSE")
                       strDomain= objRootDSE.Get("defaultNamingContext")

                    Is cool because it returns your domain name, makes the script portable across domains

                    Finding the distinguished path of an AD object is made easy with the middle bit

                    and then it is a piece of cake to get any AD atribute from the distinguishedName
                    -join([int[]][char[]]'Ut|jwXmjqq%Wzqjx'|%{[char]($_-5)})
                    #10

                      Online Bookmarks Sharing: Share/Bookmark

                      Jump to:

                      Current active users

                      There are 0 members and 1 guests.

                      Icon Legend and Permission

                      • New Messages
                      • No New Messages
                      • Hot Topic w/ New Messages
                      • Hot Topic w/o New Messages
                      • Locked w/ New Messages
                      • Locked w/o New Messages
                      • Read Message
                      • Post New Thread
                      • Reply to message
                      • Post New Poll
                      • Submit Vote
                      • Post reward post
                      • Delete my own posts
                      • Delete my own threads
                      • Rate post

                      2000-2012 ASPPlayground.NET Forum Version 3.8
                      mbt shoes www.wileywilson.com