powershell - 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
解决方案
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"
}
推荐阅读
- c# - 尽管接收到数据,变量仍为 Null
- android - Android Lollipop 的 USB 连接问题
- testing - 我想使用带有 BAZEL 的 jest 测试代码,但是当我安装 jest 并运行规则时,一切似乎都失败了
- elasticsearch - 如何为海量数据设置弹性集群?
- jquery - 在wordpress插件中通过ajax和jquery上传文件选择文件
- uml - Uml 2.5 活动图中标记的用途
- php - 将html标签添加到PHP中的字符串
- oracle - 带有游标的过程,以便使用视图增量填充表
- php - 搜索不起作用 - 致命错误 - magento 2
- java - 访问远程服务器上的 Google Admin SDK Directory API