【问题标题】:Powershell Disk Usage ReportPowershell 磁盘使用情况报告
【发布时间】:2014-12-26 23:57:41
【问题描述】:

我有以下脚本,它将从远程服务器收集磁盘使用情况统计信息。我试图让输出显示主机名,而不是脚本正在读取的 .\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 
  } 
}

【问题讨论】:

  • 您尝试过什么让检索到的名称显示?我在任何地方都没有看到$objDisk.SystemName。您可以尝试将-Property DeviceID, SystemName, Size, FreeSpace 添加到您的Get-WmiObject 通话中。
  • 如果由于某种原因 wmi 不适合您,您可以使用 [System.Net.Dns]::GetHostByAddress("x.x.x.x") 从您的 dns 获取主机名
  • 这两个 cmet 中的任何一个/两个都可能是答案
  • 感谢您的帮助。 @BaconBits 我用 $objDisk.SystemName 替换了 $strComputer.ToUpper() ,这给了我在输出中寻找的东西。再次感谢您的帮助!

标签: powershell


【解决方案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 
  } 
}

【讨论】:

    【解决方案2】:

    由于许多人可能会使用其他答案中显示的代码,因此可能还值得指出的是,要正确和完全关闭 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-30
      • 2016-05-28
      • 1970-01-01
      • 2020-06-08
      • 2013-05-03
      • 2011-08-04
      相关资源
      最近更新 更多