首页 > 解决方案 > powershell - 远程磁盘信息唯一描述

问题描述

在创建以下脚本时需要一些帮助,我可以为我指定的所有远程机器创建磁盘空间的 HTML 报告;但是,我如何为每个主机添加有意义的描述,即下面的图片。

在此处输入图像描述

下面的脚本

$Machine = @("Fakehost1", "Fakehost2", "fakehost3")

Get-CimInstance Win32_LogicalDisk -ComputerName $Machine -Filter "DriveType = '3'" -ErrorAction SilentlyContinue | 
Select-Object PsComputerName, DeviceID, 
    @{N="Disk Size (GB) "; e={[math]::Round($($_.Size) / 1073741824,0)}}, 
    @{N="Free Space (GB)"; e={[math]::Round($($_.FreeSpace) / 1073741824,0)}}, 
    @{N="Free Space (%)"; e={[math]::Round($($_.FreeSpace) / $_.Size * 100,1)}} | 
Sort-Object -Property 'Free Space (%)' | 
ConvertTo-Html -Head $Head -Title "$Title" -PreContent "<p><font size=`"6`">$Title</font><p>Generated on $date</font></p>" > $HTML

标签: powershelldiskspace

解决方案


Select-Object一个快速解决方法是在同一命令中整理 PSComputerName 对象。你已经做了很多计算的属性还有1个......

Get-CimInstance Win32_LogicalDisk -ComputerName $Machine -Filter "DriveType = '3'" -ErrorAction SilentlyContinue | 
Select-Object @{N = 'PSComputerName'; E = { $_.PSComputerName + " : Description" }}, 
    DeviceID, 
    @{N="Disk Size (GB) ";e={[math]::Round($($_.Size) / 1073741824,0)}}, 
    @{N="Free Space (GB)";e={[math]::Round($($_.FreeSpace) / 1073741824,0)}}, 
    @{N="Free Space (%)";e={[math]::Round($($_.FreeSpace) / $_.Size * 100,1)}} | 
Sort-Object -Property 'Free Space (%)' | 
ConvertTo-Html -Head $Head -Title "$Title" -PreContent "<p><font size=`"6`">$Title</font><p>Generated on $date</font></p>" > $HTML

这在我的环境中测试得很好,但是您必须决定如何或实际描述应该是什么......如果您在 AD 环境中,也许您可​​以创建一个哈希来保存如下描述:

$Descriptions = @{}
$Machine | 
Get-ADComputer -Properties Description |
ForEach-Object{ $Descriptions.Add( $_.Name, $_.Description ) }

然后更改 PSComputerName 表达式,如:

@{N = 'PSComputerName'; E = { $_.PSComputerName + $Descriptions[$_.PSComputerName] }}

这将引用哈希并返回您从 AD 描述属性中获得的值。当然,这意味着必须填充属性。但是,证明必须从某个地方挖掘描述这一点只是一个想法。

更新:

要回答您的评论,您可以手动指定哈希值。Get-CimInstance在命令之前使用类似下面的东西。确保删除以前的广告内容...

$Descriptions = 
@{
    Fakehost1 = "SQL Server for some bug app..."
    Fakehost2 = "File Server 1"
    Fakehost3 = "Another file server"
}

推荐阅读