+ Post New Thread
Page 2 of 2 FirstFirst 12
Results 16 to 30 of 30
How do you do....it? Thread, Auto Populate AD Computer Description With Last User Logged On, Serial Number,... in Technical; whoa thats an old version of the code there a much newer one on here that dumps to a csv ...
  1. #16


    Join Date
    Mar 2009
    Location
    Leeds
    Posts
    7,059
    Thank Post
    232
    Thanked 926 Times in 795 Posts
    Rep Power
    309
    whoa thats an old version of the code there a much newer one on here that dumps to a csv file as well as being much tidier better commented code

    but as im lazy

    Code:
    Option Explicit
    'On Error Resume Next
    
    dim outputstring, computermodel, cpu, ram, hddcapacity, hddfree, serialno, macaddress, operatingsystem, csvoutput
    dim stcomputer, colitems, objItem, colcpu, colPhysicalMemory, objPhysicalMemory, intMemory, colDisks, objhdd
    dim colComputerSystem, colOperatingSystems, objOperatingSystem, colBIOS, objBIOS, colnicconfig, objnicconfig
    dim objwmiservice, hddtemp, weidata, colWSA, operatingsystemno, Lastuser, oreg, spath, svalue, strvalue, svaluename
    dim adoutputstring, adoRecordset, adoConnection, objRootDSE, objNetwork, objComputer, existing
    dim pcname, wshShell, strDirectory, strFile, objFSO, objFolder, objFile, objTextFile, myDateString
    dim colnet,objnet, linkspeed, objWMIService2
    
    stcomputer = "."
    myDateString = Date()
    
    Set objRootDSE = GetObject("LDAP://RootDSE")
    Set objNetwork = WScript.CreateObject("WScript.Network")
    Set objWMIService = GetObject("winmgmts:\\" & stcomputer & "\root\CIMV2") 
    Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem") 
    Set colcpu = objWMIService.ExecQuery("Select * from Win32_Processor")
    Set colPhysicalMemory = objWMIService.ExecQuery("Select * From Win32_PhysicalMemory")
    set coldisks = objWMIService.ExecQuery("Select * From Win32_LogicalDisk") 
    Set colComputerSystem = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem")
    Set colOperatingSystems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
    Set colBIOS = objWMIService.ExecQuery("Select * From Win32_BIOS")
    Set colnicconfig = objWMIService.ExecQuery("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
    Set colWSA = objWMIservice.ExecQuery("Select * From Win32_WinSAT")
    Set adoConnection = CreateObject("ADODB.Connection")
    adoConnection.Provider = "ADsDSOObject"
    adoConnection.Open "Active Directory Provider"
    Set adoRecordset = adoConnection.Execute("<LDAP://" & objRootDSE.Get("defaultNamingContext") & ">;(&(objectCategory=Computer)(name=" & objNetwork.Computername & "));adspath;subtree")
    Set wshShell = WScript.CreateObject( "WScript.Shell" )
    Set objWMIService2 = GetObject("winmgmts:\\" & stComputer & "\root\WMI")
    Set colnet = objWMIService2.ExecQuery("SELECT * FROM MSNdis_LinkSpeed",,48)
    
    'write file location
    pcname = wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )
    strDirectory = "\\server\logs$\pc\"
    strFile = pcname & ".csv"
    
    
    'make-model
    For Each objItem In colItems 
    	computermodel= objItem.Manufacturer & " " & objItem.Model
    	'wscript.echo computermodel
    Next
    
    ' Create the File System Object
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    'cpu info
    For Each objItem in colcpu
        cpu = objItem.Name
    	'wscript.echo cpu
    Next
    
    'ram
    If Not colPhysicalMemory Is Nothing Then
    	 intMemory = 0
    	 For Each objPhysicalMemory In colPhysicalMemory
    		 intMemory = intMemory + Int(objPhysicalMemory.Capacity)
    	 Next
    	 ram = (intMemory / 1024 / 1024 ) & " MB"
    	 'wscript.echo ram
    End If
    
    'hdd capacity
    for each objhdd in coldisks
    	If objhdd.DeviceID = "C:" Then
    		hddcapacity = int(objhdd.Size/1073741824) & " GB"
    		hddfree = int(objhdd.freespace/1073741824) & " GB"
    		'wscript.echo hddfree
    	end if
    next
    
    'windows version
    For Each objOperatingSystem in colOperatingSystems
        operatingsystem = objOperatingSystem.Caption & " sp " & objOperatingSystem.ServicePackMajorVersion
    	operatingsystemno = objOperatingSystem.Version
    	'wscript.echo operatingsystem
    Next
    
    'serial no
    If Not colBIOS Is Nothing Then
    	 For Each objBIOS in colBIOS
    		 serialno = objBIOS.SerialNumber
    		 'wscript.echo serialno
    	 Next
    End If
    
    'mac address(s)
    If Not colnicconfig Is Nothing Then
    	 For Each objnicconfig in colnicconfig
    		 If macaddress <> "" Then
    			 macaddress = macaddress & " - "
    		 End If
    		 macaddress = macaddress & objnicconfig.MACAddress
    		 'wscript.echo macaddress
    	 Next
    End If
    
    'windows experience
    operatingsystemno = Left( operatingsystemno, 3 )  
    if operatingsystemno >5.5 then
    	'vista+
    	For Each objItem in colWSA
    	'weidata = "Overall -" & "na" & " - CPU - " & "na" & " - Memory - " & "na" & " - Graphics - " & "na" & " - Gaming graphics - " & "na" & " - hdd - " & "na" 
    		weidata = "Overall -" & objItem.WinSPRLevel & " - CPU - " & objItem.CPUScore & " - Memory - " & objItem.MemoryScore & " - Graphics - " & objItem.GraphicsScore & " - Gaming graphics - " & objItem.D3DScore & " - hdd - " & objItem.DiskScore
    	next
    else
    	'xp-
    	weidata = "Overall -" & "na" & " - CPU - " & "na" & " - Memory - " & "na" & " - Graphics - " & "na" & " - Gaming graphics - " & "na" & " - hdd - " & "na" 
     end if
    
     'last user
     if operatingsystemno >5.5 then  
    	'vista+
    	Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
    	Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & stcomputer & "/root/default:StdRegProv")
    	If Err.Number <> 0 Then
    		On Error Goto 0
    	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" & stcomputer
    		End If
    	End If
    else
    	'xp-
    	Const HKEY_LOCAL_MACHINE = &H80000002
    	Set oreg=GetObject("winmgmts:\\" & stComputer & "\root\default:StdRegProv")
    	spath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\WinLogon"
    	svalue = "DefaultUserName"
    	oreg.GetStringValue HKEY_LOCAL_MACHINE, spath, svalue, strValue
    	lastuser=strvalue
    	svalue = "DefaultDomainName"
    	oreg.GetStringValue HKEY_LOCAL_MACHINE, spath, svalue, strValue
    	lastuser = strvalue & "\" & lastuser
    end if
     
     'Scans data for Net Speed
    For Each objnet in colnet
    
    'Removes "slow/unused links"
    if objnet.NdisLinkSpeed/10 > 1000 then
    	linkspeed= objnet.NdisLinkSpeed/10000 & " Mbps" & "-" & linkspeed '& "-" & objnet.NdisLinkSpeed/10000 & " Mbps"
    end if
    next
     
    'concatanate data for output
    outputstring = computermodel & "," & cpu &"," & ram &"," & hddcapacity& ","  & serialno & "," & macaddress & "," 
    csvoutput = pcname & "," & computermodel & "," & cpu &"," & ram &"," & hddcapacity& "," & hddfree & "," & serialno & "," & operatingsystem & "," & macaddress & "," & weidata & "," & Lastuser & "," & linkspeed &"," & myDateString &","
    ' wscript.echo outputstring
    
    ' Check that the strDirectory folder exists
    If objFSO.FolderExists(strDirectory) Then
       Set objFolder = objFSO.GetFolder(strDirectory)
    Else
       Set objFolder = objFSO.CreateFolder(strDirectory)
       'WScript.Echo "Just created " & strDirectory
    End If
    
    If objFSO.FileExists(strDirectory & strFile) Then
       Set objFolder = objFSO.GetFolder(strDirectory)
    Else
       Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
       'Wscript.Echo "Just created " & strDirectory & strFile
    End If 
    
    set objFile = nothing
    set objFolder = nothing
    ' OpenTextFile Method needs a Const value
    ' ForAppending = 8 ForReading = 1, ForWriting = 2
    Const ForAppending = 2
    
    Set objTextFile = objFSO.OpenTextFile _
    (strDirectory & strFile, ForAppending, True)
    
    ' Writes result every time you run this VBScript
    objTextFile.WriteLine(csvoutput)
    objTextFile.Close
    
    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)
    	 existing = objcomputer.Description
    	 if left (existing,1) = "#" and not right (existing,1) = "#" then outputstring = existing & " : " & outputstring & "#"
    	 if left (existing,1) = "#" and right (existing,1) = "#" then
    		'msgbox "data"
    		wscript.quit
    	end if
    if not objComputer.Description = outputstring then
    		'msgbox "diff"
    		objComputer.Put "description", outputstring
    		objComputer.SetInfo
    	else
    		'msgbox "matches"
    	 end if
    	  
    End If
    
    If Err.Number <> 0 Then
    	 MsgBox "Write Error: " & Err.Description
    	 WScript.Quit
    End If

  2. #17

    Join Date
    Apr 2010
    Posts
    2,169
    Thank Post
    111
    Thanked 192 Times in 159 Posts
    Rep Power
    85
    Thanks @sted

    Do I need to alter the following to my system?
    strDirectory = "\\server\logs$\pc\"
    If so, is that the only line that needs to be changed?

    Thanks

  3. #18


    Join Date
    Mar 2009
    Location
    Leeds
    Posts
    7,059
    Thank Post
    232
    Thanked 926 Times in 795 Posts
    Rep Power
    309
    Quote Originally Posted by edutech4schools View Post
    Thanks @sted

    Do I need to alter the following to my system?


    If so, is that the only line that needs to be changed?

    Thanks
    yup just set that to wherever you want the csv files dumped everything else is the same as the previous versions the pc accounts need access to write to their own description field in ad (unless you run it as an admin once logged in)

  4. #19

    Join Date
    Apr 2010
    Posts
    2,169
    Thank Post
    111
    Thanked 192 Times in 159 Posts
    Rep Power
    85
    @sted Sorry to be a pain in the ...

    Any reason why I am getting a script error

    Line - 162
    Char - 4
    Error - Bad file name or number

    The line in question is

    Set objFolder = objFSO.CreateFolder(strDirectory)

  5. #20
    themightymrp's Avatar
    Join Date
    Dec 2009
    Location
    Leeds, West Yorkshire
    Posts
    1,291
    Thank Post
    219
    Thanked 238 Times in 206 Posts
    Rep Power
    76
    If you have pre-created a folder for where strDirectory is pointing to, make sure that there are enough permissions for the system to write to it

  6. #21


    Join Date
    Mar 2009
    Location
    Leeds
    Posts
    7,059
    Thank Post
    232
    Thanked 926 Times in 795 Posts
    Rep Power
    309
    as above at a guess the \\whatever\wherever is a typo, auth users dosent have modify permissions or non existant

  7. #22

    Join Date
    Apr 2010
    Posts
    2,169
    Thank Post
    111
    Thanked 192 Times in 159 Posts
    Rep Power
    85
    Also does the logs folder need sharing on the server as logs$.

  8. #23

    Join Date
    Apr 2010
    Posts
    2,169
    Thank Post
    111
    Thanked 192 Times in 159 Posts
    Rep Power
    85
    OK got it working without errors but I am back to the same issue I had using the older script (forum page 1) in that in AD under the computer description only the first 60% of the fields get listed. If I look in the logs/pc file, all the fields are listed.

    The only way I could fix this previously with the older script (forum page 1) was to delete some of the fields from the script such as processor.

    Before I go and delete anything from the new script do you have any ideas.

    Cheers

  9. #24


    Join Date
    Mar 2009
    Location
    Leeds
    Posts
    7,059
    Thank Post
    232
    Thanked 926 Times in 795 Posts
    Rep Power
    309
    the newer version adds less to ad (it only adds stuff that dosent change and dosent add headers. look at line 156 and remove the ' then run it locally see what it displays on the screen it should look like

    wsh.jpg

    its only set now to add computermodel, cpu, ram, hdd size, serial no and mac address to ad as those are the things i decided id want there and dump the rest wei/free hdd etc in the csv file less writes to ad that way
    the csv records

    pcname, computermodel, cpu, ram, hddcapacity, hddfree, serialno, operatingsystem, macaddress, weidata, Lastuser, linkspeed, myDateString

  10. Thanks to sted from:

    edutech4schools (20th May 2013)

  11. #25

    Join Date
    Apr 2010
    Posts
    2,169
    Thank Post
    111
    Thanked 192 Times in 159 Posts
    Rep Power
    85
    @sted

    Thanks for the info and a very handy script.

  12. #26


    Join Date
    Mar 2009
    Location
    Leeds
    Posts
    7,059
    Thank Post
    232
    Thanked 926 Times in 795 Posts
    Rep Power
    309
    Quote Originally Posted by edutech4schools View Post
    @sted

    Thanks for the info and a very handy script.
    thats why i wrote it saves a lot of time running round inventorying everything and it updates itself

    at least this version i can see what i did the erlier versions were awful code in random, places uncommented in the end i gave up and started again it was less painful than fixing it lol

  13. #27


    Join Date
    Mar 2009
    Location
    Leeds
    Posts
    7,059
    Thank Post
    232
    Thanked 926 Times in 795 Posts
    Rep Power
    309
    Quote Originally Posted by edutech4schools View Post
    Also does the logs folder need sharing on the server as logs$.
    it makes no odds as long as it exists and has appropriate permissions thats just where i dump the output

  14. #28
    soveryapt's Avatar
    Join Date
    Jan 2009
    Location
    Lancashire
    Posts
    2,436
    Thank Post
    672
    Thanked 279 Times in 246 Posts
    Rep Power
    79
    *me being thick here*

    Before I go trial and error to find out the best way, is this script designed to be run as a Startup or Logon? I'm kind of guessing both given the fact it will want to update the AD details as a system account?


  15. #29


    Join Date
    Mar 2009
    Location
    Leeds
    Posts
    7,059
    Thank Post
    232
    Thanked 926 Times in 795 Posts
    Rep Power
    309
    startup would be my prefered method that way as long as a pc is turned on now and again it will log the details no real point doing it based on users as that just means you need to give them rights to parts of ad you might not want them to have. I do somewhere have a script for users that adds the last pc they logged into to the office (iirc) field of their ad account

  16. Thanks to sted from:

    soveryapt (20th May 2013)

  17. #30


    Join Date
    Mar 2009
    Location
    Leeds
    Posts
    7,059
    Thank Post
    232
    Thanked 926 Times in 795 Posts
    Rep Power
    309
    the script is better documented network pc inventory script

  18. 2 Thanks to sted:

    soveryapt (20th May 2013)



SHARE:
+ Post New Thread
Page 2 of 2 FirstFirst 12

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, 09: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, 01:00 PM
  3. Prevent showing last user logged in on 7
    By ranj in forum Windows 7
    Replies: 2
    Last Post: 18th August 2010, 02:46 PM
  4. Replies: 8
    Last Post: 16th November 2009, 11:08 AM
  5. Replies: 4
    Last Post: 12th July 2007, 09: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
  •