+ Post New Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 30
How do you do....it? Thread, Auto Populate AD Computer Description With Last User Logged On, Serial Number,... in Technical; Link: Automatically fill the computer description field in Active Directory - 4sysops We have it working here: [Computer names, usernames ...
  1. #1

    DaveP's Avatar
    Join Date
    Oct 2006
    Location
    Can't talk now: The mother-ship is calling!
    Posts
    8,304
    Thank Post
    331
    Thanked 1,192 Times in 814 Posts
    Blog Entries
    2
    Rep Power
    1065

    Auto Populate AD Computer Description With Last User Logged On, Serial Number,...

    Link: Automatically fill the computer description field in Active Directory - 4sysops

    We have it working here:



    [Computer names, usernames and station serial numbers removed from this view]

    The next time we have a damaged computer and we want to find out who logged on last All I have to do is look at Active Directory.

    Perhaps this script could be modified to include free disk space and system RAM?

    Code:
    Set WshNetwork = WScript.CreateObject("WScript.Network")Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
    ' get service tag and computer manufacturer
    For Each objSMBIOS in objWMI.ExecQuery("Select * from Win32_SystemEnclosure")
    serviceTag = replace(objSMBIOS.SerialNumber, ",", ".")
    manufacturer = replace(objSMBIOS.Manufacturer, ",", ".")
    Next
    ' get computer model
    For Each objComputer in objWMI.ExecQuery("Select * from Win32_ComputerSystem")
    model = trim(replace(objComputer.Model, ",", "."))
    Next
    ' get computer object in AD
    Set objSysInfo = CreateObject("ADSystemInfo")
    Set objComputer = GetObject("LDAP://" & objSysInfo.ComputerName)
    ' build up description field data and save into computer object
    objComputer.Description = WshNetwork.UserName & " (" & serviceTag & " - " & manufacturer & " " & model & ")"
    objComputer.SetInfo

  2. 3 Thanks to DaveP:

    ChrisH (9th March 2012), edutech4schools (9th March 2012), FragglePete (12th March 2012)

  3. #2

    Join Date
    Apr 2010
    Posts
    1,832
    Thank Post
    62
    Thanked 161 Times in 133 Posts
    Rep Power
    64
    Nice, cheers

  4. #3


    Join Date
    Mar 2009
    Location
    Leeds
    Posts
    6,210
    Thank Post
    218
    Thanked 812 Times in 694 Posts
    Rep Power
    274
    try this

    Code:
    Option Explicit
    On Error Resume Next
    
    Dim objRootDSE, objNetwork, objWMIService, objComputer
    Dim strComputer, strMake, strModel, strSerialNumber, strMacAddresses, strMemory, intMemory
    Dim colBIOS, objBIOS
    Dim colNetworkAdapterConfiguration, objNetworkAdapterConfiguration
    Dim colComputerSystem, objComputerSystem
    Dim colPhysicalMemory, objPhysicalMemory
    Dim adoConnection, adoRecordset
    dim mydatestring
    dim colOperatingSystems,objOperatingSystem,osver
    dim oReg, lastuser, snode, spath, svaluename, svalue
    dim colcpu, objItem, cpuspec
    dim objDisk, colDisks , cspace
    
    strComputer = "."
    strMemory = ""
    strMake = ""
    strModel = ""
    strSerialNumber = ""
    strMacAddresses = ""
    myDateString = Date()
    
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" _
        & strComputer & "\root\cimv2")
    Set colDisks = objWMIService.ExecQuery _
        ("Select * from Win32_LogicalDisk")
    For Each objDisk in colDisks
    	 If objDisk.DeviceID = "C:" Then
    		 'Wscript.Echo "DeviceID: " & objDisk.DeviceID       
    		 cspace = int(objDisk.Size/1073741824) & " GB"
    	 end if
    Next
    
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colcpu = objWMIService.ExecQuery("Select * from Win32_Processor")
    For Each objItem in colcpu
        cpuspec = objItem.Name
    Next
    
    ' use "." for local computer
    sNode = "."
    
    Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
    
    On Error Resume Next
    Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
    & sNode & "/root/default:StdRegProv")
    
    If Err.Number <> 0 Then
    On Error Goto 0
    WScript.Echo "Could not connect to computer " & sNode
    Else
    On Error Goto 0
    sPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI"
    sValueName = "LastLoggedonUser"
    
    If oReg.GetStringValue(HKLM, sPath, sValueName, sValue) = 0 Then
    lastuser = sValue
    Else
    lastuser = "unknown" & sNode
    End If
    End If
    
    
    Set objRootDSE = GetObject("LDAP://RootDSE")
    Set objNetwork = WScript.CreateObject("WScript.Network")
    Set objWMIService = GetObject("Winmgmts:\\" & strComputer & "\root\cimv2") 
    
    Set colComputerSystem = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem") 
    Set colOperatingSystems = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem")
    For Each objOperatingSystem in colOperatingSystems
        osver = objOperatingSystem.Caption & " sp " & objOperatingSystem.ServicePackMajorVersion'& "  " & objOperatingSystem.Version
    Next
    
    If Not colComputerSystem Is Nothing Then
    	 For Each objComputerSystem In colComputerSystem 
    		strMake = objComputerSystem.Manufacturer 
    		strModel = objComputerSystem.Model 
    	Next
    End If
    
    Set colBIOS = objWMIService.ExecQuery("Select * From Win32_BIOS")
    
    If Not colBIOS Is Nothing Then
    	 For Each objBIOS in colBIOS
    		 strSerialNumber = objBIOS.SerialNumber
    	 Next
    End If
    
    Set colNetworkAdapterConfiguration = objWMIService.ExecQuery("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
    
    strMacAddresses = ""
    
    If Not colNetworkAdapterConfiguration Is Nothing Then
    	 For Each objNetworkAdapterConfiguration in colNetworkAdapterConfiguration
    		 If strMacAddresses <> "" Then
    			 strMacAddresses = strMacAddresses & ", "
    		 End If
    		 strMacAddresses = strMacAddresses & objNetworkAdapterConfiguration.MACAddress
    	 Next
    End If
    
    Set colPhysicalMemory = objWMIService.ExecQuery("Select * From Win32_PhysicalMemory")
    
    If Not colPhysicalMemory Is Nothing Then
    	 intMemory = 0
    	 For Each objPhysicalMemory In colPhysicalMemory
    		 intMemory = intMemory + Int(objPhysicalMemory.Capacity)
    	 Next
    	 strMemory = (intMemory / 1024 / 1024) & " MB"
    End If
    
    Set adoConnection = CreateObject("ADODB.Connection")
    adoConnection.Provider = "ADsDSOObject"
    adoConnection.Open "Active Directory Provider"
    
    If Err.Number <> 0 Then
    	 MsgBox "Connect Error: " & Err.Description
    	 WScript.Quit
    End If
    
    Set adoRecordset = adoConnection.Execute("<LDAP://" & objRootDSE.Get("defaultNamingContext") & ">;(&(objectCategory=Computer)(name=" & objNetwork.Computername & "));adspath;subtree")
    
    If Err.Number <> 0 Then
    	 MsgBox "Query Error: " & Err.Description
    	 WScript.Quit
    End If
    
    If Not adoRecordset.EOF Then
    	 Set objComputer = GetObject(adoRecordset.Fields(0).Value)
    	 objComputer.Put "description", "Make, " & strmake & " ,Model, " &strModel & ",cpu," & cpuspec &  ",Ram, " & strMemory & ",hdd size," & cspace & " ,Serial No, " & strSerialNumber & " ,MAC Addresses, " & strMACAddresses & "," & osver & ", Last booted, " & mydatestring & ", lastuser, " &lastuser
    	 objComputer.SetInfo
    End If
    
    If Err.Number <> 0 Then
    	 MsgBox "Write Error: " & Err.Description
    	 WScript.Quit
    End If
    adds system details ram, serial no, hdd size, last time booted, last user mac address and a few others

  5. 2 Thanks to sted:

    DaveP (9th March 2012), FragglePete (12th March 2012)

  6. #4

    DaveP's Avatar
    Join Date
    Oct 2006
    Location
    Can't talk now: The mother-ship is calling!
    Posts
    8,304
    Thank Post
    331
    Thanked 1,192 Times in 814 Posts
    Blog Entries
    2
    Rep Power
    1065
    Thanks. I will try that next week.

    Is there a limit to the number of characters allowed in the description field? [I am imagining 255 chars]

    If there is a limit what happens if the limit is exceeded? Hopefully it will just be ignored [and not break Active Directory!]

  7. #5


    Join Date
    Mar 2009
    Location
    Leeds
    Posts
    6,210
    Thank Post
    218
    Thanked 812 Times in 694 Posts
    Rep Power
    274
    honestly no idea but i dont seem to of killed anything yet lol

  8. Thanks to sted from:

    DaveP (12th March 2012)

  9. #6

    DaveP's Avatar
    Join Date
    Oct 2006
    Location
    Can't talk now: The mother-ship is calling!
    Posts
    8,304
    Thank Post
    331
    Thanked 1,192 Times in 814 Posts
    Blog Entries
    2
    Rep Power
    1065
    sted: I have added your modified script and it is working without any errors/problems.

    Thanks.

  10. #7


    Join Date
    Feb 2007
    Location
    51.405546, -0.510212
    Posts
    8,123
    Thank Post
    203
    Thanked 2,387 Times in 1,767 Posts
    Rep Power
    703
    Quote Originally Posted by DaveP View Post
    Is there a limit to the number of characters allowed in the description field? [I am imagining 255 chars]
    It's 1024 characters. This applies to Windows 2000 Server all the way up to Windows Server 8.

  11. Thanks to Arthur from:

    DaveP (12th March 2012)

  12. #8

    Join Date
    Apr 2010
    Posts
    1,832
    Thank Post
    62
    Thanked 161 Times in 133 Posts
    Rep Power
    64
    In the post above they talk about

    Guys, please beware: if you do it after every logon, you can quickly exhaust the USN for the whole AD domain! And then the domain is dead. At least check if this new objComputer.Description is different from old description (already in AD).
    is this an issue in steds code above?

  13. #9


    Join Date
    Mar 2009
    Location
    Leeds
    Posts
    6,210
    Thank Post
    218
    Thanked 812 Times in 694 Posts
    Rep Power
    274
    Quote Originally Posted by edutech4schools View Post
    In the post above they talk about



    is this an issue in steds code above?
    yes mine just dumps it no checking. Wouldnt of though it mattered though as its just replacing the contents of one table with fresh one?

  14. #10


    Join Date
    Mar 2009
    Location
    Leeds
    Posts
    6,210
    Thank Post
    218
    Thanked 812 Times in 694 Posts
    Rep Power
    274
    just had a quick google and as usn is a 64 bit integer per item that means i can change an item 18.5 quadrillion times a pc wont get booted that often (i run it as a startup script) i wouldnt of thought. Looks like the usn is per object anyway so worst case i can see is pc01 needs a new account creating

  15. #11


    Join Date
    Mar 2009
    Location
    Leeds
    Posts
    6,210
    Thank Post
    218
    Thanked 812 Times in 694 Posts
    Rep Power
    274
    just to be on the safe side new version check existing data and only overwrites if theres a change (tempted to remove last user and last logon then or its going to change everytime anyway)

    Code:
    Option Explicit
    On Error Resume Next
    
    Dim objRootDSE, objNetwork, objWMIService, objComputer, datastring
    Dim strComputer, strMake, strModel, strSerialNumber, strMacAddresses, strMemory, intMemory
    Dim colBIOS, objBIOS
    Dim colNetworkAdapterConfiguration, objNetworkAdapterConfiguration
    Dim colComputerSystem, objComputerSystem
    Dim colPhysicalMemory, objPhysicalMemory
    Dim adoConnection, adoRecordset
    dim mydatestring
    dim colOperatingSystems,objOperatingSystem,osver
    dim oReg, lastuser, snode, spath, svaluename, svalue
    dim colcpu, objItem, cpuspec
    dim objDisk, colDisks , cspace
    
    strComputer = "."
    strMemory = ""
    strMake = ""
    strModel = ""
    strSerialNumber = ""
    strMacAddresses = ""
    myDateString = Date()
    
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" _
        & strComputer & "\root\cimv2")
    Set colDisks = objWMIService.ExecQuery _
        ("Select * from Win32_LogicalDisk")
    For Each objDisk in colDisks
    	 If objDisk.DeviceID = "C:" Then
    		 'Wscript.Echo "DeviceID: " & objDisk.DeviceID       
    		 cspace = int(objDisk.Size/1073741824) & " GB"
    	 end if
    Next
    
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colcpu = objWMIService.ExecQuery("Select * from Win32_Processor")
    For Each objItem in colcpu
        cpuspec = objItem.Name
    Next
    
    ' use "." for local computer
    sNode = "."
    
    Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
    
    On Error Resume Next
    Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
    & sNode & "/root/default:StdRegProv")
    
    If Err.Number <> 0 Then
    On Error Goto 0
    WScript.Echo "Could not connect to computer " & sNode
    Else
    On Error Goto 0
    sPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI"
    sValueName = "LastLoggedonUser"
    
    If oReg.GetStringValue(HKLM, sPath, sValueName, sValue) = 0 Then
    lastuser = sValue
    Else
    lastuser = "unknown" & sNode
    End If
    End If
    
    
    Set objRootDSE = GetObject("LDAP://RootDSE")
    Set objNetwork = WScript.CreateObject("WScript.Network")
    Set objWMIService = GetObject("Winmgmts:\\" & strComputer & "\root\cimv2") 
    
    Set colComputerSystem = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem") 
    Set colOperatingSystems = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem")
    For Each objOperatingSystem in colOperatingSystems
        osver = objOperatingSystem.Caption & " sp " & objOperatingSystem.ServicePackMajorVersion'& "  " & objOperatingSystem.Version
    Next
    
    If Not colComputerSystem Is Nothing Then
    	 For Each objComputerSystem In colComputerSystem 
    		strMake = objComputerSystem.Manufacturer 
    		strModel = objComputerSystem.Model 
    	Next
    End If
    
    Set colBIOS = objWMIService.ExecQuery("Select * From Win32_BIOS")
    
    If Not colBIOS Is Nothing Then
    	 For Each objBIOS in colBIOS
    		 strSerialNumber = objBIOS.SerialNumber
    	 Next
    End If
    
    Set colNetworkAdapterConfiguration = objWMIService.ExecQuery("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
    
    strMacAddresses = ""
    
    If Not colNetworkAdapterConfiguration Is Nothing Then
    	 For Each objNetworkAdapterConfiguration in colNetworkAdapterConfiguration
    		 If strMacAddresses <> "" Then
    			 strMacAddresses = strMacAddresses & ", "
    		 End If
    		 strMacAddresses = strMacAddresses & objNetworkAdapterConfiguration.MACAddress
    	 Next
    End If
    
    Set colPhysicalMemory = objWMIService.ExecQuery("Select * From Win32_PhysicalMemory")
    
    If Not colPhysicalMemory Is Nothing Then
    	 intMemory = 0
    	 For Each objPhysicalMemory In colPhysicalMemory
    		 intMemory = intMemory + Int(objPhysicalMemory.Capacity)
    	 Next
    	 strMemory = (intMemory / 1024 / 1024) & " MB"
    End If
    
    Set adoConnection = CreateObject("ADODB.Connection")
    adoConnection.Provider = "ADsDSOObject"
    adoConnection.Open "Active Directory Provider"
    
    If Err.Number <> 0 Then
    	 MsgBox "Connect Error: " & Err.Description
    	 WScript.Quit
    End If
    
    Set adoRecordset = adoConnection.Execute("<LDAP://" & objRootDSE.Get("defaultNamingContext") & ">;(&(objectCategory=Computer)(name=" & objNetwork.Computername & "));adspath;subtree")
    
    If Err.Number <> 0 Then
    	 MsgBox "Query Error: " & Err.Description
    	 WScript.Quit
    End If
    
    If Not adoRecordset.EOF Then
    	 Set objComputer = GetObject(adoRecordset.Fields(0).Value)
    	 datastring = "Make, " & strmake & " ,Model, " &strModel & ",cpu," & cpuspec &  ",Ram, " & strMemory & ",hdd size," & cspace & " ,Serial No, " & strSerialNumber & " ,MAC Addresses, " & strMACAddresses & "," & osver & ", Last booted, " & mydatestring & ", lastuser, " &lastuser
    	 if not objComputer.Description = datastring then
    		'msgbox "diff"
    		objComputer.Put "description", datastring
    		objComputer.SetInfo
    	else
    		'msgbox "matches"
    	 end if
    	  
    End If
    
    If Err.Number <> 0 Then
    	 MsgBox "Write Error: " & Err.Description
    	 WScript.Quit
    End If

  16. #12

    Join Date
    Jan 2012
    Posts
    70
    Thank Post
    1
    Thanked 3 Times in 3 Posts
    Rep Power
    5
    looks good, will give it a try when I get a chance.

  17. #13

    Join Date
    May 2013
    Posts
    1
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    I actually asked Microsoft's Directory Services team about this question because we store everything in separate attributes for AD (and I am talking ManagedBy, Comments, Notes, blank). Here is Microsoft's response: "The current USN is a 64-bit counter maintained by each Active Directory domain controller as the highestCommittedUsn attribute on rootDSE. Being an unsigned 64-bit integer, that means 264-1, which is 18,446,744,073,709,551,615 (i.e. 18 quintillion). Under normal use that is never going to run out. Even more, when AD reaches that top number, it would restart at 1 all over again!" You can read about the ManagedBy and storing the last logged on user here: Find Out What Computer a User Logged Into - DeployHappiness. You can read Microsoft's full reply here: Friday Mail Sack: Drop the dope, hippy! edition - Ask the Directory Services Team - Site Home - TechNet Blogs

  18. #14

    Join Date
    Apr 2010
    Posts
    1,832
    Thank Post
    62
    Thanked 161 Times in 133 Posts
    Rep Power
    64
    Finally got round to trying sted's code out above and it does everything except list the last logged user. When I list a computer it has all the details like serial number, memory, OS version etc but not user info.

    Can someone else confirm my findings as the forum title says otherwise.
    I am not able to change the code but can see some lastuser references so it should be working.

    UPDATE:
    In the AD computer description field at the far right of all the computer info is the word 'Last' and then nothing. Looking at the code it looks like it should say 'Last Booted' followed but last user logged on.
    Last edited by edutech4schools; 17th May 2013 at 01:19 PM.

  19. #15

    Join Date
    Apr 2010
    Posts
    1,832
    Thank Post
    62
    Thanked 161 Times in 133 Posts
    Rep Power
    64
    Fixed the issue which was too many items. I removed the cpu item and the missing fields display

SHARE:
+ Post New Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Populating AD computer description field during import
    By googlemad in forum Windows Server 2008 R2
    Replies: 21
    Last Post: 20th October 2011, 08:51 AM
  2. hp thinclient t5530 remembering last user logged on
    By FN-GM in forum Thin Client and Virtual Machines
    Replies: 3
    Last Post: 16th June 2011, 12:00 PM
  3. Prevent showing last user logged in on 7
    By ranj in forum Windows 7
    Replies: 2
    Last Post: 18th August 2010, 01:46 PM
  4. Replies: 8
    Last Post: 16th November 2009, 10:08 AM
  5. Replies: 4
    Last Post: 12th July 2007, 08:11 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •