Thursday, December 14, 2017

Excel VBA WMI script which pulls out storage volume details for a list of servers

''' Excel vba program for accessing the storage volume details of a servers & capture the output in another csv file.


Const sPath = "F:\Files\ServerName.csv"   '''' edit the path where all the server name
Const oPath = "F:\Files\outputFile.csv"   '''' edit the path where output will be saved

Dim objWMIService, strComputer, colItems, objItem, i, lRow, rCount, wbMain, wbOutput
Dim mSht As Worksheet
Dim oSht As Worksheet

Sub Main_Mdl()

Workbooks.Open sPath
Set wbMain = ActiveWorkbook
Set mSht = wbMain.ActiveSheet
lRow = mSht.Cells(mSht.Rows.Count, 1).End(xlUp).Row

Workbooks.Open oPath
Set wbOutput = ActiveWorkbook
Set oSht = wbOutput.ActiveSheet

oSht.Cells(1, 1) = "ServerName/IP Address"
oSht.Cells(1, 2) = "BlockSize"
oSht.Cells(1, 3) = "BootVolume"
oSht.Cells(1, 4) = "Total Capacity"
oSht.Cells(1, 5) = "Free Capacity"
oSht.Cells(1, 6) = "Used Capacity"
oSht.Cells(1, 7) = "Label"
oSht.Cells(1, 8) = "Name"
oSht.Cells(1, 9) = "PageFilePresent"
oSht.Cells(1, 10) = "SupportsFileBasedCompression"

rCount = 2
For i = 2 To lRow
    strComputer = mSht.Cells(i, 1)
    Call Extract_Storage_Details
Next
wbMain.Close False

wbOutput.Save
wbOutput.Close True

End Sub

Sub Extract_Storage_Details()
On Error GoTo err462
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Volume", , 48)
For Each objItem In colItems
        oSht.Cells(rCount, 1) = strComputer
        oSht.Cells(rCount, 2) = objItem.BlockSize
        oSht.Cells(rCount, 3) = objItem.BootVolume
        oSht.Cells(rCount, 4) = Round(((objItem.capacity / 1024) / 1024) / 1024, 1)
        oSht.Cells(rCount, 5) = Round(((objItem.FreeSpace / 1024) / 1024) / 1024, 1)
        oSht.Cells(rCount, 6) = oSht.Cells(rCount, 4) - oSht.Cells(rCount, 5)
        oSht.Cells(rCount, 7) = objItem.Label
        oSht.Cells(rCount, 8) = objItem.Name
        oSht.Cells(rCount, 9) = objItem.PageFilePresent
        oSht.Cells(rCount, 10) = objItem.SupportsFileBasedCompression
        rCount = rCount + 1
Next
err462:
    If Err.Number = 462 Then
        oSht.Cells(rCount, 1) = strComputer
        oSht.Range("B" & rCount & ":J" & rCount) = "Access Denied"
        rCount = rCount + 1
        Exit Sub
    End If
End Sub

No comments:

Post a Comment

Excel VBA to send email from excel with HTML table in a email body

'' Excel vba script to send email from outlook & email body in HTML table format. ''Write down email body in HTML tags...