Powershell磁盘使用情况报告

时间:2014-12-26 23:57:41

标签: powershell

我有以下脚本将从远程服务器收集磁盘使用情况统计信息。我正在尝试让输出显示主机名而不是脚本正在读取的。\ ServerIPs.txt文件中的IP。我试过从get-wmiobject Win32_LogicalDisk中检索“SystemName”但是无法显示要显示的名称。我应该添加什么来允许主机名显示在报告中而不是主机IP地址?

$erroractionpreference = "SilentlyContinue" 
$a = New-Object -comobject Excel.Application 
$a.visible = $True  

$b = $a.Workbooks.Add() 
$c = $b.Worksheets.Item(1) 

$c.Cells.Item(1,1) = "Server Name" 
$c.Cells.Item(1,2) = "Drive" 
$c.Cells.Item(1,3) = "Total Size (GB)" 
$c.Cells.Item(1,4) = "Free Space (GB)" 
$c.Cells.Item(1,5) = "Free Space (%)" 

$d = $c.UsedRange 
$d.Interior.ColorIndex = 19 
$d.Font.ColorIndex = 11 
$d.Font.Bold = $True 

$intRow = 2 

$colComputers = get-content ".\ServerIPs.txt" 
foreach ($strComputer in $colComputers) 
{ 
  $colDisks = get-wmiobject Win32_LogicalDisk -computername $strComputer -Filter "DriveType = 3"  
  foreach ($objdisk in $colDisks) 
  { 
    $c.Cells.Item($intRow, 1) = $strComputer.ToUpper() 
    $c.Cells.Item($intRow, 2) = $objDisk.DeviceID 
    $c.Cells.Item($intRow, 3) = "{0:N0}" -f ($objDisk.Size/1GB) 
    $c.Cells.Item($intRow, 4) = "{0:N0}" -f ($objDisk.FreeSpace/1GB) 
    $c.Cells.Item($intRow, 5) = "{0:P0}" -f ([double]$objDisk.FreeSpace/[double]$objDisk.Size) 
    $intRow = $intRow + 1 
  } 
}

2 个答案:

答案 0 :(得分:1)

感谢@BaconBits的帮助。我还在输出中添加了一个日期列。

$erroractionpreference = "SilentlyContinue" 
$a = New-Object -comobject Excel.Application 
$a.visible = $True  

$b = $a.Workbooks.Add() 
$c = $b.Worksheets.Item(1) 

$c.Cells.Item(1,1) = "Server Name" 
$c.Cells.Item(1,2) = "Drive" 
$c.Cells.Item(1,3) = "Total Size (GB)" 
$c.Cells.Item(1,4) = "Free Space (GB)" 
$c.Cells.Item(1,5) = "Free Space (%)"
$c.Cells.Item(1,6) = "Date" 

$d = $c.UsedRange 
$d.Interior.ColorIndex = 19 
$d.Font.ColorIndex = 11 
$d.Font.Bold = $True 

$intRow = 2 

$colComputers = get-content ".\ServerIPs.txt" 
foreach ($strComputer in $colComputers) 
{ 
  $colDisks = get-wmiobject Win32_LogicalDisk -computername $strComputer -Filter "DriveType = 3"  
  foreach ($objdisk in $colDisks) 
  { 
    $c.Cells.Item($intRow, 1) = $objDisk.SystemName 
    $c.Cells.Item($intRow, 2) = $objDisk.DeviceID 
    $c.Cells.Item($intRow, 3) = "{0:N0}" -f ($objDisk.Size/1GB) 
    $c.Cells.Item($intRow, 4) = "{0:N0}" -f ($objDisk.FreeSpace/1GB) 
    $c.Cells.Item($intRow, 5) = "{0:P0}" -f ([double]$objDisk.FreeSpace/[double]$objDisk.Size) 
    $c.Cells.Item($intRow, 6) = Get-Date
    $intRow = $intRow + 1 
  } 
}

答案 1 :(得分:0)

因为许多人可能会使用其他答案中显示的代码,所以也可能值得一提的是,为了正确完全关闭Excel,您还需要释放COM引用。在我自己的测试中发现删除Excel的变量也确保没有剩余的引用会使Excel.exe保持打开状态(就像你在ISE中调试一样)。

如果不执行上述操作,如果查看任务管理器,您可能会看到Excel仍在运行......在某些情况下,还有许多副本。

这与COM对象如何包装在“运行时可调用包装器”中有关。

以下是应该使用的框架代码:

$excel = New-Object -ComObject Excel.Application
$excel.Visible = $true
$workbook = $excel.Workbooks.Add()
# or $workbook = $excel.Workbooks.Open($xlsxPath)

# do work with Excel...

$workbook.SaveAs($xlsxPath)
$excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)
# no $ needed on variable name in Remove-Variable call
Remove-Variable excel