Monday, January 6, 2014

VBScript to get Total Space details of drives on Multiple Servers

Hello everyone.. i am sharing one of my very first script used to get size details of drives on multiple servers in a domain. Hope it helps.

Dim strExcelPath, objExcel, objSheet, a, b, c
Const xlExcel7 = 39
 ' Spreadsheet file to be created.Result.xls will store the output.
strExcelPath = "c:\temp\Result.xls"

Set fso = CreateObject("Scripting.FileSystemObject")
' Server.txt is the input file for the script under C:\temp. Save server names in this file.
Set objInputFile = fso.OpenTextFile("c:\temp\Server.txt",1,True)

Set objExcel = CreateObject("Excel.Application")
If (Err.Number <> 0) Then
    On Error Goto 0
    Wscript.Echo "Excel application not found."
End If
On Error Goto 0
 ' Create a new workbook.
objExcel.Visible = True
' Bind to worksheet.
Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)
objSheet.Name = "Server Details"
' Format the spreadsheet.
objSheet.Range("A1:Z1").Font.Bold = True
intRow = 2
objExcel.Cells(1, 1).Value = "Hostname"
objExcel.Cells(1, 2).Value = "C:"
objExcel.Cells(1, 3).Value = "E:"
objExcel.Cells(1, 4).Value = "F:"
objExcel.Cells(1, 5).Value = "G:"
objExcel.Cells(1, 6).Value = "H:"
objExcel.Cells(1, 7).Value = "M:"
objExcel.Cells(1, 8).Value = "Q:"
objExcel.Cells(1, 9).Value = "T:"
objExcel.Cells(1, 10).Value ="I:"
Do While objInputFile.AtEndOfLine <> True

 strComputer = objInputFile.ReadLine
  objExcel.Cells(intRow, 1).Value = strComputer

 On Error resume Next
Set oWMI = GetObject("winmgmts:\\.\root\cimv2")
Set oPing = oWMI.Get("Win32_PingStatus.Address='"& strComputer & "'")

If oPing.StatusCode = 0 Then
  Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_LogicalDisk")
     For Each objItem In colItems

        a = objItem.DeviceID
        b =  objItem.Size /1048576\1024 + 1
     Select Case a
         Case "C:"
             objExcel.Cells(intRow, 2).Value = b
         Case "E:"
             objExcel.Cells(intRow, 3).Value = b
         Case "F:"
             objExcel.Cells(intRow, 4).Value = b
         Case "G:"
             objExcel.Cells(intRow, 5).Value = b
         Case "H:"
             objExcel.Cells(intRow, 6).Value = b
         Case "M:"
             objExcel.Cells(intRow, 7).Value = b
         Case "Q:"
             objExcel.Cells(intRow, 8).Value = b
         Case "T:"
             objExcel.Cells(intRow, 9).Value = b
         Case "I:"
             objExcel.Cells(intRow, 10).Value = b
     End Select
intRow = intRow + 1
End If

Set objWMIService = Nothing
Set colItems = Nothing
Set objItem = Nothing


' Save the spreadsheet and close the workbook.
 ' Specify Excel7 File Format.
objExcel.ActiveWorkbook.SaveAs strExcelPath, xlExcel7
 ' Quit Excel.
 ' Clean Up
Set objSheet = Nothing
Set objExcel = Nothing
Wscript.Echo "Done"

Note :
  • The script can be used to retrieve the values from servers in the same domain.
  • Save the script as a .vbs file and double click the same to run.
  • Server.txt will store the list of servers for which output is required.
  • Result.xls is created which stored the output.
 Please write your queries in comment section.

No comments:

Post a Comment