RabidAlien
IS-IT--Management
Hey, first wanted to say that I've browsed the livin' daylights out of these forums, and have learned a TON from them! That being said, I'm about the noob-iest (yep, just made that up) scripter you've ever met, and my boss wants me to learn. His idea of "teaching" is to assign tasks, such as "write a script to copy all files/folders/permissions/settings from one fileserver to another, oh, and we're changing where some folders reside and resetting permissions on others..." or "write a script that will pull inventory from a PC and put it on a server so that exec-level users (ie....VP) can see who's got what under the hood of their towers, but it can't write to Excel since we want the servers included." Blah. This last one has been kicking my butt. Basically, we need something that writes to a text-file as a scheduled action whenever a user logs in, overwriting the data that's already in the existing text file, for about 75 PC's. It needs to pull username, PC name, manufacturer, model, serial number, processor, memory, hard drive free/total space (for two partitions, C: and D:, respectively), OS, service pack, IP Address, and date of latest inventory. I'll figure out the merge-text-to-excel bit later. I don't have a gift for languages, so learning this is kicking my butt!
Here's what I have so far (if I take out the hard drive stuff, it sorta works...):
Here's what I have so far (if I take out the hard drive stuff, it sorta works...):
Code:
Option Explicit
'On Error Resume Next
Dim objFSO, wshNetwork, strComputer, objWMIService, colItems, strSerialNumber, strFileName, strResults, objItem, objOutputFile, strCPUDetails
Set objFSO = CreateObject("Scripting.FileSystemObject")
' To convert to a logon script that runs without user interaction, add a rem in front of the strComputer = InputBox
' line below and remove the rem from the strComputer = "." line below that. This will then only check the PC on which
' the script runs
Set WshNetwork = WScript.CreateObject("WScript.Network")
strComputer = "."
' Get the serial number first to see if it already exists in the spreadsheet
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_BIOS",,48)
For Each objItem In colItems
strSerialNumber = "" & Trim(objItem.SerialNumber)
Next
Set objWMIService = Nothing
Set colItems = Nothing
strFileName = "O:\IT_Department\Inventory\" & strSerialNumber & ".txt"
' Create Excel Spreadsheet
strResults = "Computername;Username;Manufacturer;Model;Serial Number;CPU;Operating System;Service Pack;Total Memory;IP Address;Audit Date" & VbCrLf
' Get Computer System Details
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
For Each objItem In colItems
strResults = strResults & Trim(objItem.Caption) & ";" & Trim(objItem.UserName) & ";" & Trim(objItem.Manufacturer) & ";" & Trim(objItem.Model)
Next
Set objWMIService = Nothing
Set colItems = Nothing
'
'Output the Serial Number
strResults = strResults & ";" & strSerialNumber
'
' Get CPU Details
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Processor",,48)
For Each objItem In colItems
strCPUDetails = Trim(objItem.Name)
Next
Set objWMIService = Nothing
Set colItems = Nothing
strResults = strResults & ";" & strCPUDetails
'
' Get OS Details
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem",,48)
For Each objItem In colItems
strResults = strResults & ";" & Trim(objItem.Caption) & ";" & Trim(objItem.CSDVersion) & ";" & Trim(FormatNumber(objItem.TotalVisibleMemorySize/1024,0))
Next
Set objWMIService = Nothing
Set colItems = Nothing
'
'Hard Drive Partition Info
'
' Get IP Address
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each objItem In colItems
strIPAddress = Join(objItem.IPAddress, ",")
Next
Set objWMIService = Nothing
Set colItems = Nothing
'
' Get & Writeout current Date
strResults = strResults & ";" & Trim(Day(Now) & "-" & Month(Now) & "-" & Year(Now))
Set objOutputFile = objFSO.CreateTextFile(strFileName, True)
objOutputFile.Write strResults
objOutputFile.Close
Set objOutputFile = Nothing
Set objFSO = Nothing