【发布时间】: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