VBScript to Check server / computer OS version and Service Pack information

'Below script will provide the output in Ms-Excel. Excel application is mandatory to run this script

' List Operating System and Service Pack Information

Const Reading = 1

Dim objExcel, objWorksheet, objWorkbook, objRange
Dim objNtpad, ofjfile
'Dim inSvr as String

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Add()
Set objWorksheet = objWorkbook.Worksheets(1)

Set objNtpad = CreateObject("Scripting.FileSystemObject")
Set objFile = objNtpad.OpenTextFile("C:\servers.txt", Reading) ' You can mention the input file location

' Checking for Excel application
On Error Resume Next
Set objexcel=CreateObject("Excel.application")
If(number <> 0) Then
  On Error Goto 0
  WScript.Echo "Excel application not found"
  WScript.Quit
End If

objworksheet.cells(1,1) = "Servername"
objworksheet.cells(1,2) = "Operating System"
objworksheet.cells(1,3) = "Version: "
objworksheet.cells(1,4) = "Service Pack: "

objworksheet.range("A1:D1").font.size= 12
objworksheet.range("A1:D1").font.bold= True

x = 2

Do
strComputer = objfile.ReadLine

Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
Set colOSes = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")

objworksheet.cells(x,1).value = strComputer

For Each objOS in colOSes
  objworksheet.cells(x,1).value = objOS.CSName
  objworksheet.cells(x,2).value = objOS.Caption 'Name
  objworksheet.cells(x,3).value = objOS.Version 'Version & build
  objworksheet.cells(x,4).value = objOS.ServicePackMajorVersion & "." & _
   objOS.ServicePackMinorVersion
Next


 x = x + 1

Loop Until objfile.AtEndOfStream = True

Set objrange = objWorksheet.Usedrange
objrange.entirecolumn.autofit()

Set objexcel = Nothing
Set objworksheet = Nothing
Set flag = Nothing
Set objWMIservice= Nothing
Set objrange = Nothing
Set objFile = Nothing
Set objNtpad = Nothing
'Set inSvr = Nothing

6 comments:

  1. I need a VB script to get Server Host Name, IP Address, Model No., Serial No. and Operating System. Please Help...

    ReplyDelete
  2. Here you go.

    Try this script:

    ' Description : This script will display the Computername, model #, Serial #, OS type, IP addresses in an Excel file
    ' Instructions : Create a text file by name servers.txt in C: drive with the server names
    ' which u need to check for these details. Output will be an excel
    ' file which will be saved in C: drive with file name as "Computer_Info.xls"


    Const Reading = 1

    Dim objExcel, objWorksheet, objWorkbook, objRange
    Dim objNtpad, ofjfile

    Set objExcel = CreateObject("Excel.Application")
    objExcel.Visible = True
    Set objWorkbook = objExcel.Workbooks.Add()
    Set objWorksheet = objWorkbook.Worksheets(1)

    Set objNtpad = CreateObject("Scripting.FileSystemObject")
    Set objFile = objNtpad.OpenTextFile("C:\servers.txt", Reading)

    On Error Resume Next
    Set objexcel=CreateObject("Excel.application")
    If(number <> 0) Then
    On Error Goto 0
    WScript.Echo "Excel application not found"
    WScript.Quit
    End If

    objworksheet.cells(1,1) = "Servername"
    objworksheet.cells(1,2) = "Server Model"
    objworksheet.cells(1,3) = "Serial Number"
    objworksheet.cells(1,4) = "OS Type"
    objworksheet.cells(1,5) = "IP Address1"
    objworksheet.cells(1,6) = "IP Address2"
    objworksheet.cells(1,7) = "IP Address3"

    objworksheet.range("A1:G1").font.size= 12
    objworksheet.range("A1:G1").font.bold= True

    x = 2
    strDate= Replace(Date,"/","-")
    strTime= Replace(Time,":","-")
    strSavefile = "C:\Computer_Info_" & strDate & " " & strTime & ".xls"

    flag = 0
    Do
    strComputer = objfile.ReadLine

    Set objWMISvc = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

    Set colItems = objWMISvc.ExecQuery( "Select * from Win32_ComputerSystem", , 48 )

    objworksheet.cells(x,1).value = strComputer

    For Each objItem in colItems
    objworksheet.cells(x,1).value = ObjItem.Name
    objworksheet.cells(x,2).value = ObjItem.Model

    Set colItemsCSP = objWMISvc.ExecQuery("SELECT * FROM Win32_ComputerSystemProduct")
    For Each objItemCSP in colItemsCSP
    objworksheet.cells(x,3).value = objItemCSP.IdentifyingNumber

    Set colItemsOS = objWMISvc.ExecQuery( "Select * from Win32_OperatingSystem")
    For Each objItemOS in colItemsOS
    objworksheet.cells(x,4).value = objItemOS.Caption

    j = 5
    Set colItemsNAC = objWMISvc.ExecQuery( "Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
    For Each objItemNAC in colItemsNAC
    If Not IsNull(objitemNAC.IPAddress) Then
    For i=LBound(objitemNAC.IPAddress) to UBound(objitemNAC.IPAddress)
    objWorksheet.cells(x,j).value = objitemNAC.IPAddress(i)

    Next
    End If
    j = j + 1

    Next
    Next
    Next
    Next

    x = x + 1

    Loop Until objfile.AtEndOfStream = True

    Set objrange = objWorksheet.Usedrange
    objrange.entirecolumn.autofit()

    objworkbook.SaveAs strSavefile
    WScript.Echo "Excel file has been saved in C:\Computer_Info_" & strDate & " " & strTime & ".xls"

    objExcel.ActiveWorkbook.close
    objExcel.Application.Quit

    Set objExcel = Nothing
    Set objWorksheet = Nothing
    Set flag = Nothing
    Set objWMIsvc= Nothing
    Set objrange = Nothing
    Set objFile = Nothing
    Set objNtpad = Nothing

    ReplyDelete
  3. Try this for single server/machine:

    Set objWMISvc = GetObject( "winmgmts:\\.\root\cimv2" )
    Set colItems = objWMISvc.ExecQuery( "Select * from Win32_ComputerSystem", , 48 )
    For Each objItem in colItems
    WScript.Echo "Computer Name: " & ObjItem.Name
    WScript.Echo "Computer Model: " & ObjItem.Model

    Set colItemsCSP = objWMISvc.ExecQuery("SELECT * FROM Win32_ComputerSystemProduct")
    For Each objItemCSP in colItemsCSP
    Wscript.Echo "Serial Number: " & objItemCSP.IdentifyingNumber

    Set colItemsOS = objWMISvc.ExecQuery( "Select * from Win32_OperatingSystem")
    For Each objItemOS in colItemsOS
    WScript.Echo "OS Type: " & objItemOS.Caption

    Set colItemsNAC = objWMISvc.ExecQuery( "Select * from Win32_NetworkAdapterConfiguration",, 48)
    For Each objItemNAC in colItemsNAC
    If objItemNAC.IPEnabled = True Then
    strIPAddress=join(objitemNAC.IPAddress,",")
    WScript.Echo "IPAddress: " & strIPAddress

    Else
    If objitemNAC.dhcpenabled = True then
    strIPAddress=join(objitemNAC.IPAddress,",")

    Else
    End If
    End If

    Next
    Next
    Next
    Next

    ReplyDelete
  4. VBScript to Check server / computer OS version and Service Pack information
    this script works fine if the servers in the list are up and ruinning, but if you have a server that is powered down or out of network it simply copies the above servers details. if this can be marked as not reachable then it could be easy.

    ReplyDelete
    Replies
    1. Hi Vara Prasad,

      Use the below vbscript which addressed your query.

      ' List Operating System and Service Pack Information

      Const Reading = 1

      Dim objExcel, objWorksheet, objWorkbook, objRange
      Dim objNtpad, ofjfile
      'Dim inSvr as String

      Set objExcel = CreateObject("Excel.Application")
      objExcel.Visible = True
      Set objWorkbook = objExcel.Workbooks.Add()
      Set objWorksheet = objWorkbook.Worksheets(1)

      Set objNtpad = CreateObject("Scripting.FileSystemObject")
      Set objFile = objNtpad.OpenTextFile("C:\servers.txt", Reading) ' You can mention the input file location

      Set WSHShell = WScript.CreateObject("WScript.Shell")
      Set objLocator = CreateObject("WbemScripting.SWbemLocator")

      ' Checking for Excel application
      On Error Resume Next
      Set objexcel=CreateObject("Excel.application")
      If(number <> 0) Then
      On Error Goto 0
      WScript.Echo "Excel application not found"
      WScript.Quit
      End If

      objworksheet.cells(1,1) = "Servername"
      objWorksheet.cells(1,2) = "Operating System"
      objworksheet.cells(1,3) = "Version: "
      objworksheet.cells(1,4) = "Service Pack: "
      objworksheet.cells(1,5) = "Remarks "


      objworksheet.range("A1:E1").font.size= 12
      objworksheet.range("A1:E1").font.bold= True


      strDate= Replace(Date,"/","-")
      strTime= Replace(Time,":","-")
      strSavefile = "C:\Computer OS info_" & strDate & " " & strTime & ".xls"

      x = 2


      Do
      strComputer = objfile.ReadLine
      objworksheet.cells(x,1).value = strComputer

      On Error Resume Next
      Err.Clear
      ' Set objWMIService = objLocator.ConnectServer(strcomputer, "root\cimv2",username,password)
      Set objWMIService = objLocator.ConnectServer(strcomputer, "root\cimv2")
      If Err.Number <> 0 Then
      Call failed
      Else
      Call success
      End If


      x = x + 1

      Loop Until objfile.AtEndOfStream = True

      Sub Success()

      Set objWMIService = GetObject("winmgmts:" _
      & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

      Set colOSes = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")



      For Each objOS in colOSes
      objworksheet.cells(x,1).value = objOS.CSName
      objworksheet.cells(x,2).value = objOS.Caption 'Name
      objworksheet.cells(x,3).value = objOS.Version 'Version & build
      objworksheet.cells(x,4).value = objOS.ServicePackMajorVersion & "." & _
      objOS.ServicePackMinorVersion

      Next

      End Sub

      Sub failed()
      objworksheet.cells(x,5).Value = "No login Rights/No DNS record/Server may be offline"
      End Sub


      Set objrange = objWorksheet.Usedrange
      objrange.entirecolumn.autofit()


      objWorkbook.SaveAs strSavefile
      WScript.Echo "Excel file has been saved in C:\Computer OS info_" & strDate & " " & strTime & ".xls"

      ' Quit Excel.
      objExcel.Quit



      Set objExcel = Nothing
      Set objworksheet = Nothing
      Set flag = Nothing
      Set objWMIservice= Nothing
      Set objrange = Nothing
      Set objFile = Nothing
      Set objNtpad = Nothing
      'Set inSvr = Nothing

      Delete
  5. do we have a vb script to check the below for multiple servers and export it in to excel or csv format. i need to check the same details on windows 2000/2003/2008 servers

    fqdn, OS Version, physical or virtual, make( dell/hp/ibm/vmware), power status online or offline, last boot time,cpu, memory, c drive total size, c drive used space, c drive free space, cluster or not, number of nic,Number of nic active, number of nic disabled, nic teaming available or not

    ReplyDelete