2011-02-03 140 views
1

我想創建一個VB腳本,它將爲我的環境中的服務器的磁盤空間生成基於Excel的報告。我有3個障礙,我無法克服。VB腳本需要幫助並生成一個Excel工作表

  1. 如何將工作表中的所有列/單元格對齊?第25行是我嘗試這樣做的地方,但它引發錯誤,「無法設置Range類的Horizo​​ntalAlignment屬性」。

  2. 某些服務器有多個驅動器(例如C,D,E)。當腳本生成報告時,它只會顯示最後一個驅動器(例如E)。我怎樣才能讓它顯示每個服務器的每個驅動器?

  3. 當我運行該腳本時,我希望它附加報告當天的磁盤使用情況。目前,它將使用當天的磁盤使用率替換現有的單元格。

我的腳本代碼如下:

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 

回答

2
  1. 看起來好像你已經爲xlHAlignLeft的值定義了一個常數。

  2. 你應該增加計數器x磁盤環內:

    For Each objDisk in colDisks 
        x = x + 1 ' <-- add this line 
    

    你可能對你怎麼想的輸出與代碼中的確切位置在哪裏,你增加計數器,根據打看。我想我放在我的例子中的地方會導致每臺機器之間出現空白。

  3. 這裏的訣竅是將x初始化爲第一個可用行,而不總是默認爲1.下面的代碼在第一列('A')中搜索最後一個非空行。 (reference

    Const xlUp = -4162 
    x = objExcel.Cells(Rows.Count, 1).End(xlUp).Row 
    
0

你知道嗎,你可以把對HTML表格標記爲.xls文件,並與Excel中打開它?它甚至適用於Excel 2000!試試吧,你會非常高興,你不必創建「Excel.Application」COM對象!