+ Post New Thread
Results 1 to 2 of 2
Windows Thread, network pc inventory script in Technical; notes the pcs account needs to be able to write to its own description field in active directory (easily done ...
  1. #1


    Join Date
    Mar 2009
    Location
    Leeds
    Posts
    6,802
    Thank Post
    231
    Thanked 883 Times in 759 Posts
    Rep Power
    300

    network pc inventory script

    notes
    the pcs account needs to be able to write to its own description field in active directory (easily done with delegate control to either domain pcs or as i do an allpcs group containing domain pcs (as i dont like delegating control to a built in group)
    needs a share on network somewhere that auth users can write to in the main script its \\server\log$\pcs\
    and the obvious disclaimer ive not had any issues but that dosent mean you wont so use it at your own risk

    records basic details in ad (make model, cpu,ram,hdd,serial, mac(s) basically stuff that dosent change often) and if you have existing info in the computers description before deploying the script add a # before it (so if your description field said staffab laptop change to #staffab laptop before deploying script) and it will keep that there (though the data after will not update after the first time until you manually remove the # at the very end of the line)

    it now only writes to ad when a change is detected

    it creates a csv file with more info in it in the format below (and copy and past that to a file called 01.csv if you want a nice formatted spreadsheet of all pcs

    Code:
    make-model,cpu,ram,hdd size,hdd free,serial no,Windows Version,mac address(es),Windows experience ratings,last user,Network Speed,date collected
    the following batch file will concatenate all the individual files into 1 file (needs to be a drive letter you run it from not a unc path

    Code:
    del all.csv /q
    copy "*.csv" "all.csv" /y
    main code itself

    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\log$\pcs\"
    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 = 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. #2


    Join Date
    Mar 2009
    Location
    Leeds
    Posts
    6,802
    Thank Post
    231
    Thanked 883 Times in 759 Posts
    Rep Power
    300
    repost as i forgot to put the pcs name in the csv file whichh made it a bit pointless lol (and for some reason i cant edit the old post)


    notes
    the pcs account needs to be able to write to its own description field in active directory (easily done with delegate control to either domain pcs or as i do an allpcs group containing domain pcs (as i dont like delegating control to a built in group)
    needs a share on network somewhere that auth users can write to in the main script its \\server\log$\pcs\
    and the obvious disclaimer ive not had any issues but that dosent mean you wont so use it at your own risk

    records basic details in ad (make model, cpu,ram,hdd,serial, mac(s) basically stuff that dosent change often) and if you have existing info in the computers description before deploying the script add a # before it (so if your description field said staffab laptop change to #staffab laptop before deploying script) and it will keep that there (though the data after will not update after the first time until you manually remove the # at the very end of the line)

    it now only writes to ad when a change is detected

    it creates a csv file with more info in it in the format below (and copy and past that to a file called 01.csv if you want a nice formatted spreadsheet of all pcs

    Code:
    name,make-model,cpu,ram,hdd size,hdd free,serial no,Windows Version,mac address(es),Windows experience ratings,last user,Network Speed,date collected
    the following batch file will concatenate all the individual files into 1 file (needs to be a drive letter you run it from not a unc path

    Code:
    del all.csv /q
    copy "*.csv" "all.csv" /y
    main code itself

    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\log$\pcs\"
    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

  3. 2 Thanks to sted:

    jamiet147 (20th June 2012), reggiep (2nd April 2012)

SHARE:
+ Post New Thread

Similar Threads

  1. install Photoshop elements 8 on a network PC
    By Hedghog in forum How do you do....it?
    Replies: 1
    Last Post: 10th September 2010, 04:44 PM
  2. Adding a Curriculum networked PC to a SIMS Network
    By speckytecky in forum MIS Systems
    Replies: 17
    Last Post: 27th February 2010, 12:03 AM
  3. Windows Firewall on networked PCs?
    By Nozza in forum Windows
    Replies: 14
    Last Post: 4th May 2009, 11:48 AM
  4. Replies: 11
    Last Post: 16th October 2008, 05:10 PM
  5. How do you stop synchronisation on an ex networked PC
    By speckytecky in forum How do you do....it?
    Replies: 5
    Last Post: 24th August 2008, 10:15 AM

Thread Information

Users Browsing this Thread

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

Tags for this Thread

Posting Permissions

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