simple windows/VB script to do the server inventory

Great for for getting all Windows related scripts.

simple windows/VB script to do the server inventory

Postby Praveen » Mon May 11, 2009 5:24 pm

This is the very simple and easily understandable script to do a server inventory. It can be run from a remote computer to collect the information of onsite machine. It includes the information such as
Server Name,Operating system,SP Version,Drive information,File System,Total Space (in MB),IP Address,Backup IP,Processor type,No. Of processor,Type, total RAM Size ( in MB)


Code: Select all
'
List the Latest Installed Service Pack
On Error Resume Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\server.txt", 1)
sReportName = "C:\log.txt"
Set oFSo = CreateObject("Scripting.FileSystemObject")
Set fReport = oFSo.CreateTextFile(sReportName, True)

fReport.WriteLine "Server Name" & "," & "OS" & "," & "SP Version"
Do until objFile.AtEndOfStream
Line = objFile.ReadLine
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & Line & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem")
Set colOperatingSystems1 = objWMIService.ExecQuery _
    ("Select * from Win32_LogicalDisk")
Set colOperatingSystems2 = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
Set colOperatingSystems3 = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapter")
Set colOperatingSystems4 = objWMIService.ExecQuery _
    ("Select * from Win32_PhysicalMemory")
Set colOperatingSystems5 = objWMIService.ExecQuery("Select * from Win32_Processor")
Set colOperatingSystems6  = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem")
   
' Space   
For Each objOperatingSystem1 In colOperatingSystems1
MsgBox objOperatingSystem1.Caption
MsgBox FormatNumber(objOperatingSystem1.FreeSpace/1024/1024,1) & "MBytes"
MsgBox objOperatingSystem1.Filesystem
MsgBox FormatNumber(objOperatingSystem1.Size/1024/1024,1) & "MBytes"
Next

'IpAddress
For Each objOperatingSystem2 In colOperatingSystems2
If Not IsNull(objOperatingSystem2.IPAddress) Then
    For i=LBound(objOperatingSystem2.IPAddress) to UBound(objOperatingSystem2.IPAddress)
    Msgbox objOperatingSystem2.IPAddress(i)
    MsgBox objOperatingSystem2.DefaultIPGateway(i)
    Next
End If
Next

'Name
For Each objOperatingSystem in colOperatingSystems
fReport.WriteLine objOperatingSystem.CSName & "," & objOperatingSystem.Caption & "," & objOperatingSystem.CSDVersion
Next

'MAC Address
For Each objOperatingSystem3 In colOperatingSystems3
MsgBox objOperatingSystem3.MACAddress
Next

'RAM
For Each objOperatingSystem4 In colOperatingSystems4
MsgBox FormatNumber(objOperatingSystem4.Capacity/1024/1024,2) & "MBytes"
Next

'processor
For Each objOperatingSystem5 In colOperatingSystems5
MsgBox objOperatingSystem5.Name
Next


'No.of Processor
For Each objOperatingSystem6 In colOperatingSystems6
MsgBox objOperatingSystem6.NumberOfProcessors
MsgBox objOperatingSystem6.SystemType
Next
Loop


the same script to write in CSV format

Code: Select all
' List the Latest Installed Service Pack
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\server.txt", 1)
sReportName = "C:\log.txt"
Set oFSo = CreateObject("Scripting.FileSystemObject")
Set fReport = oFSo.CreateTextFile(sReportName, 8)

fReport.WriteLine "Server Name" & "," & "OS" & "," & "SP Version"
Do until objFile.AtEndOfStream
Line = objFile.ReadLine
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & Line & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem")
Set colOperatingSystems1 = objWMIService.ExecQuery _
    ("Select * from Win32_LogicalDisk")
Set colOperatingSystems2 = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
Set colOperatingSystems3 = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapter")
Set colOperatingSystems4 = objWMIService.ExecQuery _
    ("Select * from Win32_PhysicalMemory")
Set colOperatingSystems5 = objWMIService.ExecQuery("Select * from Win32_Processor")
Set colOperatingSystems6  = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem")
   
'Name
For Each objOperatingSystem in colOperatingSystems
fReport.Write vbNewLine
fReport.Write objOperatingSystem.CSName & "," & objOperatingSystem.Caption & "," & objOperatingSystem.CSDVersion & ","
Next

' Space   
For Each objOperatingSystem1 In colOperatingSystems1
fReport.Write objOperatingSystem1.Caption & "," &  objOperatingSystem1.FreeSpace/1024/1024 & " MBytes" & "," &  objOperatingSystem1.Filesystem & "," &  objOperatingSystem1.Size/1024/1024 & " MBytes" & ","

Next

'IpAddress
For Each objOperatingSystem2 In colOperatingSystems2
If Not IsNull(objOperatingSystem2.IPAddress) Then
    For i=LBound(objOperatingSystem2.IPAddress) to UBound(objOperatingSystem2.IPAddress)
    fReport.Write objOperatingSystem2.IPAddress(i)& ","
    Next
End If
Next

'MAC Address
For Each objOperatingSystem3 In colOperatingSystems3
fReport.Write objOperatingSystem3.MACAddress & ","
Next

'RAM - added to last line ( with no.of processor)
'For Each objOperatingSystem4 In colOperatingSystems4
'fReport.Write objOperatingSystem4.Capacity/1024/1024 & "MBytes" & ","
'Next

'processor
For Each objOperatingSystem5 In colOperatingSystems5
fReport.Write objOperatingSystem5.Name & ","
Next


'No.of Processor and RAM
For Each objOperatingSystem6 In colOperatingSystems6
fReport.Write objOperatingSystem6.NumberOfProcessors & "," & objOperatingSystem6.SystemType & "," & objOperatingSystem6.TotalPhysicalMemory/1024/1024 & " MByte" & ","
Next
Loop

User avatar
Praveen
Site Admin
 
Posts: 84
Joined: Sat Apr 25, 2009 2:21 pm

Return to Windows Scripting/WMI/VBScript

Who is online

Users browsing this forum: No registered users and 0 guests

cron