自分の環境内のサーバーのディスク容量に関するExcelベースのレポートを生成するVBスクリプトを作成しようとしています。私は乗り越えることができない3つのハードルが残っています。VBスクリプトで必要なヘルプとExcelワークシートの作成
ワークシート内のすべての列/セルをどのように配置することができますか? 25行目でこれをやってみましたが、 "RangeクラスのHorizontalAlignmentプロパティを設定できません"というエラーがスローされます。
一部のサーバーには、複数のドライブ(たとえば、C、D、E)があります。スクリプトがレポートを生成すると、最後のドライブ(たとえばE)のみが表示されます。各サーバーのすべてのドライブを表示するにはどうすればよいですか?
スクリプトを実行すると、現在の日付のディスク使用量でレポートを追加したいと思います。現時点では、既存のセルを当日のディスク使用量に置き換えます。次のように
私のスクリプトのコードは次のとおりです。
On Error Resume Next
Const ForReading = 1
Const HARD_DISK = 3
x = 1
dtmDate = Date
strDay = Day(Date)
strMonth = Month(Date)
strYear = Right(Year(Date), 2)
strFileName = "C:\Users\cvandal\Desktop\Scripts\Server_Disk_Space_Report.xlsx"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists("C:\Users\cvandal\Desktop\Scripts\Server_Disk_Space_Report.xlsx") Then
Set serverList = objFSO.OpenTextFile("servers.txt", ForReading)
Set objExcel = CreateObject("Excel.Application")
objExcel.Workbooks.Open "C:\Users\cvandal\Desktop\Scripts\Server_Disk_Space_Report.xlsx"
objExcel.Visible = True
objExcel.Columns("A:ZZ").ColumnWidth = 25
objExcel.Columns("A:ZZ").HorizontalAlignment = xlHAlignLeft
objExcel.Cells(2, 1).Value = "Server Disk Space Report"
objExcel.Cells(4, 1).Value = dtmDate
objExcel.Cells(5, 1).Value = "Drives:"
objExcel.Cells(6, 1).Value = "Total Capacity (in GB):"
objExcel.Cells(7, 1).Value = "Used Capacity (in GB):"
objExcel.Cells(8, 1).Value = "Free Space (in GB):"
objExcel.Cells(9, 1).Value = "Free Space (in %):"
Do Until serverList.AtEndOfStream
x = x + 1
strComputer = serverList.ReadLine
Set objWMIService = GetObject("winmgmts:{impersonationlevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk WHERE DriveType = " & HARD_DISK & "")
If Err.Number <> 0 Then
'WScript.Echo "Error: " & Err.Number
'WScript.Echo "Error (Hex): " & Hex(Err.Number)
'WScript.Echo "Source: " & Err.Source
'WScript.Echo "Description: " & Err.Description
objExcel.Cells(4, x).Value = strComputer & " - " & Err.Description
objExcel.Cells(4, x).Columns.AutoFit
Err.Clear
Else
For Each objDisk in colDisks
drives = "Error"
totalCapacity = 0
freeSpace1 = 0
usedCapacity = 0
freeSpace2 = 0
drives = objDisk.DeviceID
totalCapacity = Round((objDisk.Size/1073741824), 2)
freeSpace1 = Round((objDisk.FreeSpace/1073741824), 2)
usedCapacity = Round((totalCapacity - freeSpace1), 2)
freeSpace2 = Round((freeSpace1/totalCapacity)*100, 0)
If freeSpace2 > 20 Then
objExcel.Cells(4, x).Value = strComputer
objExcel.Cells(5, x).Value = drives
objExcel.Cells(6, x).Value = totalCapacity & " GB"
objExcel.Cells(7, x).Value = usedCapacity & " GB"
objExcel.Cells(8, x).Value = freeSpace1 & " GB"
objExcel.Cells(9, x).Value = freeSpace2 & "%"
objExcel.Cells(9, x).Interior.Color = RGB(198,239,206)
ElseIf freeSpace2 < 10 Then
objExcel.Cells(4, x).Value = strComputer
objExcel.Cells(5, x).Value = drives
objExcel.Cells(6, x).Value = totalCapacity & " GB"
objExcel.Cells(7, x).Value = usedCapacity & " GB"
objExcel.Cells(8, x).Value = freeSpace1 & " GB"
objExcel.Cells(9, x).Value = freeSpace2 & "%"
objExcel.Cells(9, x).Interior.Color = RGB(255,199,206)
Else
objExcel.Cells(4, x).Value = strComputer
objExcel.Cells(5, x).Value = drives
objExcel.Cells(6, x).Value = totalCapacity & " GB"
objExcel.Cells(7, x).Value = usedCapacity & " GB"
objExcel.Cells(8, x).Value = freeSpace1 & " GB"
objExcel.Cells(9, x).Value = freeSpace2 & "%"
objExcel.Cells(9, x).Interior.Color = RGB(255,235,156)
End If
Next
End If
Loop
Else
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False
Set objWorkbook = objExcel.Workbooks.Add()
objWorkbook.SaveAs(strFileName)
objExcel.Quit
WScript.Echo "Server_Disk_Space_Report.xlsx has been created. Please re-run the script."
End If