Anyone need a script?
So for a final project for one of my classes, we had to create our own Virtual Basic Script (VBS). The project was to create a script that would be good in a networking environment. This was a tougher class for me as I’m just not good with the whole programming/scripting thing. But, through pain and punishment and a ton of reading, I managed to create a script that I haven’t quite seen lying around.
I will absolutely not take all the credit for this script. I did what I could from learning throughout the class so some things made sense and were easier to put together. But mostly, this took a lot of searching around for exactly how specific things worked and if it were possible to access certain services in the Windows Management Instrumentation (WMI). Alas, after searching around enough, reading enough, finding certain scripts to figure out how it pulled information, and a god awful amount of trial and error, I had what I wanted… mostly.
This VBS will pull information from any computer in your domain (even just your own computer). It does utilize a command line argument. This means you will have to run it at the command prompt. It will pull details of the Operating System, Total RAM, Processor Information, BIOS Information, Display Information, Hard Drive information, and certain installed software on the machine. It will then write all of the information nicely into a text file. The text file itself will be created in the same directory where you run the script.
Since this script does utilize the command line argument, you will have to run it from the command line. The easiest way to do this is to simply go to your start menu, click Run, and type CMD. This will open the Dos box. Of course if you’re using Windows 7 (and possibly Vista), just click the start menu and type CMD in the dialog box there. To run the script itself, you will need to point it to the directory where the script is saved. If it’s just in the root of the C drive, then you would type cd c:\
Pretty simple if you know your way around dos commands. Once you are in the right directory you can run it by typing, CScript scriptname.vbs computername. Where scriptname is the name of the script file and computename is the name of the computer you want to pull the information from.
Example: CScript hardware.vbs machine1
I created a domain using Windows Server 2003 and Windows XP via VMware. I was easily able to run the script and pull the information from whichever machine I wanted to. It works quite well. I understand it’s far from perfect and I am a complete n00b when it comes to this. But I am proud of what I managed to do with little knowledge.
Here is the script. If you want to run it, open notepad, copy everything below, paste it in notepad, and then save it as whatever you want but make sure you add the extension .vbs It Has to be saved as .vbs.
Example: Save As – hardware.vbs
If you save it as a text file, it isn’t going to run for you.
Don’t mind my very n00bish-generic commenting in the script. Seriously, I was in a rush and plus I was talking out of my ass at times. As I’ve said, I know it’s far from perfect and there are better ways to go about it and it, but hey.. I like it for my first big script. Anyway, enjoy!
——————————————————————————————————————————————
‘Here is a script that will provide valuable inventory information on computers that we
‘choose on our domain. Utilizing the registry and various WMI services, we can pull the
‘installed software, video, bios, ram, processor, and hard drive information.
‘This script will require a command line argument -name of machine- and will output
‘the information to a text file named after the computer we choose.
On Error Resume Next
Dim strComputer
Dim oReg
Dim colItems
Dim objItem
Dim objWMIService
Dim colSettings
Dim objRAM
Dim objProcessor
Dim objBIOS
Dim FSO
Dim text
Dim subkey
Dim arrSubkeys
strComputer = WScript.Arguments(0) ‘ using strComputer to create a command line argument – we want to be able to choose the computer
If WScript.Arguments.Count = 0 Then ‘ use the if then command – if the argument is 0 then
WScript.Echo(“You must enter a computer name”) ‘ we will have it echo that we must enter a computer name
Else ‘ if we do enter a name – then the script does not = 0 and the script moves to the else command which moves the script forward
Const HKEY_LOCAL_MACHINE = &H80000002 ‘ here is where a constant is used to access the locan machine registry
Const UnInstPath = “SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\” ‘a constant that points to exactly where we want to search for the registry software – in this case we are looking for software that can be uninstalled
Set oReg=GetObject(“winmgmts:” _
& “{impersonationLevel=impersonate}!\\” & strComputer & “\root\default:StdRegProv”) ‘the rest of the line is set to grab the information from the registry – I use strcomputer here as well so it will grab from the machine we choose instead of the local machine
write = strComputer & ” Computer Inventory” & vbCrLf & “——————————————” & vbCrLf & vbCrLf ‘ the report command here is used to write data to the text file – here we are using strcomptuer so that it writes the name of the computer we are inventoring along with the text “computer inventory”
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”) ‘ objWMIService is set to equal the getobject command in winmgmts
Set colSettings = objWMIService.ExecQuery(“Select * from Win32_OperatingSystem”,,48) ‘colItems is equaled to the objWMISergice query information from the Win32_OperatingSystem service
write = write & vbCrLf & “——————————————” & vbCrLf ‘ report is again used to create the lines (makes the text look more organized)
write = write & “Operating System” & vbCrLf & “——————————————” & vbCrLf & vbCrLf ‘ again we use report to write out Operating System in the text
For Each objItem in colSettings ‘ the for each command here is set to give us the information from the colItems that we queried above. For each item that we want in our collection – it will be displayed below by what we want from it
write = write & “Caption: ” & objItem.Caption & vbCrLf
write = write & “Description: ” & objItem.Description & vbCrLf
write = write & “EncryptionLevel: ” & objItem.EncryptionLevel & vbCrLf
write = write & “InstallDate: ” & objItem.InstallDate & vbCrLf
write = write & “Manufacturer: ” & objItem.Manufacturer & vbCrLf
write = write & “MaxNumberOfProcesses: ” & objItem.MaxNumberOfProcesses & vbCrLf
write = write & “Name: ” & objItem.Name & vbCrLf
write = write & “Organization: ” & objItem.Organization & vbCrLf
write = write & “OSProductSuite: ” & objItem.OSProductSuite & vbCrLf
write = write & “RegisteredUser: ” & objItem.RegisteredUser & vbCrLf
write = write & “SerialNumber: ” & objItem.SerialNumber & vbCrLf
write = write & “ServicePackMajorVersion: ” & objItem.ServicePackMajorVersion
write = write & “ServicePackMinorVersion: ” & objItem.ServicePackMinorVersion & vbCrLf
write = write & “Version: ” & objItem.Version & vbCrLf
write = write & “WindowsDirectory: ” & objItem.WindowsDirectory & vbCrLf
Next
Set colSettings = objWMIService.ExecQuery(“Select * from Win32_ComputerSystem”) ‘ and we use the select statement for the Win32_ComputerSystem
write = write & vbCrLf & “——————————————” & vbCrLf
write = write & “RAM Information” & vbCrLf & “——————————————” & vbCrLf & vbCrLf ‘ again we are using report to write text to the file
For Each objRAM in colSettings ‘ for each command is used here to tell us that for each item found in the colsettings we queried above
write = write & objRAM.TotalPhysicalMemory /1024\1024+1 & “MB Total memory” & vbcrlf ‘ this line is to report the total memory of ram that is available on the machine.
Next
Set colSettings = objWMIService.ExecQuery(“Select * from Win32_Processor” , , 48 ) ‘ and it will query information from the Win32_Processor service
write = write & vbCrLf & “——————————————” & vbCrLf
write = write & “Processor Information” & vbCrLf & “——————————————” & vbCrLf & vbCrLf ‘ again we are writing out the lines to organize the output text and will also write Processor
For Each objProcessor in colSettings ‘ for each command is used again to tell us that for each item that is found from the colsettings query above – it will display what we want below which is what the objProcessor is used for – to hold those items
write = write & “Name: ” & objProcessor.Name & vbCrLf
write = write & “Manufacturer: ” & objProcessor.Manufacturer & vbCrLf
write = write & “Description: ” & objProcessor.Description & vbCrLf
write = write & “Current Clock Speed: ” & objProcessor.CurrentClockSpeed & ” MHz” & vbCrLf & vbCrLf
Next
Set colSettings = objWMIService.ExecQuery(“Select * from Win32_BIOS” , , 48) ‘ here we are setting the colsettings variable to equal the objWMIService query in which case we will be gathering information from the Win32_BIOS service
write = write & vbCrLf & “——————————————” & vbCrLf
write = write & “BIOS Information” & vbCrLf & “——————————————” & vbCrLf & vbCrLf ‘ again we write out the lines and the ‘BIOS Information’ to the text file
For each objBIOS in colSettings ‘ for each command is used here to tell us that for each item that is found from the colsettings query above – it will display what we want below which is what the objBIOS variable is used for
write = write & “Build Number: ” & objBIOS.BuildNumber & vbCrLf
write = write & “Current Language: ” & objBIOS.CurrentLanguage & vbCrLf
write = write & “Installable Languages: ” & objBIOS.InstallableLanguages & vbCrLf
write = write & “Manufacturer: ” & objBIOS.Manufacturer & vbCrLf
write = write & “Name: ” & objBIOS.Name & vbCrLf
write = write & “Primary BIOS: ” & objBIOS.PrimaryBIOS & vbCrLf
write = write & “Release Date: ” & objBIOS.ReleaseDate & vbCrLf
write = write & “Serial Number: ” & objBIOS.SerialNumber & vbCrLf
write = write & “SMBIOS Version: ” & objBIOS.SMBIOSBIOSVersion & vbCrLf
write = write & “SMBIOS Major Version: ” & objBIOS.SMBIOSMajorVersion & vbCrLf
write = write & “SMBIOS Minor Version: ” & objBIOS.SMBIOSMinorVersion & vbCrLf
write = write & “SMBIOS Present: ” & objBIOS.SMBIOSPresent & vbCrLf
write = write & “Status: ” & objBIOS.Status & vbCrLf
write = write & “Version: ” & objBIOS.Version & vbCrLf
Next
write = write & vbCrLf & “——————————————” & vbCrLf
write = write & “Video/Display Information” & vbCrLf & “——————————————” & vbCrLf & vbCrLf ‘ again writing information to the text file
Set colSettings = objWMIService.ExecQuery( “Select * from Win32_VideoController”, , 48 ) ‘ colsettings is again equaled to the objWMIService in which case we will be gathering information from the Win32_VideoController service
For Each objItem in colSettings ‘ for each command again to tell us that for each item that is found in our above colsettings query – it will display what we want below and that is what objItem is used for – to hold that information
write = write & ” Name: ” & objItem.Name & vbCrLf
write = write & ” Description: ” & objItem.Description & vbCrLf
write = write & ” Video Processor: ” & objItem.VideoProcessor & vbCrLf
write = write & ” Adapter RAM: ” & Int( ( objItem.AdapterRAM + 524288 ) / 1048576 ) & ” MB” & vbCrLf
write = write & ” Video Mode Description: ” & objItem.VideoModeDescription & vbCrLf & vbCrLf
Next
write = write & vbCrLf & “——————————————” & vbCrLf
write = write & “Hard Drive Information” & vbCrLf & “——————————————” & vbCrLf & vbCrLf ‘ writing data to the text file
Set colSettings = objWMIService.ExecQuery( “Select * from Win32_DiskDrive”, , 48 ) ‘ colsettings is set to = the objWMIService query again – this time it will query from Win32_DiskDrive service
For Each objItem in colSettings ‘ for each command that will tell us that for each item found in the above colsettings query – it will ouput the information we request below
write = write & ” Manufacturer: ” & objItem.Manufacturer & vbCrLf
write = write & ” Model: ” & objItem.Model & vbCrLf
write = write & ” Size: ” & Int( ( objItem.Size + 536870912 ) / 1073741824 ) & ” GB” & vbCrLf & vbCrLf
Next
oReg.EnumKey HKEY_LOCAL_MACHINE, UnInstPath, arrSubKeys ‘ here oReg.EnumKey is again in conjunction with the registry path of our choice
write = write & vbCrLf & “——————————————” & vbCrLf
write = write & “Installed Software” & vbCrLf & “——————————————” & vbCrLf & vbCrLf ‘ writing the reports to the text file
For Each subkey In arrSubKeys ‘ we set up the for each command one last time to tell us that for iten found in query from the arrSubkeys variable that we will output what we need below
If Left (subkey, 1) <> “{” Then
write = write & subkey & vbCrLf
End If
Next
Set fso = CreateObject(“Scripting.FileSystemObject”) ‘ here is where fso is set to the createobject command – this is where it understands that it must create a file system object – such as a text file
Set text = fso.CreateTextFile (strComputer & “.txt”, ForWriting) ‘ the variable text is set to equal the fso variable that we defined above – here it is used to create a text file – the text file will be named whatever the computer we are searching for is named – this is why we use strcomputer variable here – we could name it anything
text.write write ‘ this command tells it to write the reports that were gathered throughout the entire script – yes we could have named it anything
text.close ‘ here is where we tell the script to close the text file when no more writing is needed
End If
Wscript.Echo “I have finished retrieving the information” ‘ and of course we end it with a Wscript.Echo command which will state that it is finished when the script is completed
April 29, 2010 at 7:27 PM
um. . . yeah, i don’t know what u said lol
April 29, 2010 at 7:28 PM
English?