使用 PowerShell,我想将计算机名称包含在 WMI 查询的一些返回数据中。 我研究了自定义列 - 需要类似的东西 - 但我不知道如何将计算机名称传递到下一个循环中以包含在结果表中。 例如:
Get-WmiObject -Class Win32_LogicalDisk -ComputerName MailServer01
实际上,我在另一个角度看$_
是计算机名称:
Get-WmiObject -Class Win32_LogicalDisk -ComputerName $_
这很好用,但我想将ComputerName
($_)
作为最终输出或报告的一部分。 有什么想法吗?
计算机名已经存在(作为系统名称)。 它只是不是默认显示属性的一部分。
跑
Get-WmiObject -Class Win32_LogicalDisk -ComputerName MailServer01 | format-list *
以查看返回对象的所有属性。
"__Server"属性将始终可用于 get-wmiobject 对象
另请注意:
从Windows PowerShell 3.0开始,Get-WmiObject返回的对象的__Server属性具有PSComputerName别名。这样可以更轻松地在输出和报告中包含源计算机名称。
http://technet.microsoft.com/en-us/library/hh849824.aspx
试试这个
Get-WMiObject -Class Win32_LogicalDisk -ComputerName $_ | Add-Member -MemberType NoteProperty -Name ComputerName -Value $_ -PassThru
然后,您可以根据需要输出或操作数据,并且只要该对象存在,您通过Add-Member
添加的属性就可用于该对象。
该 cmdlet 的文档位于此处
是的,我没有意识到还有更多的属性。 效果很棒:__Server是我需要的物业:
$myServers = @("server1", "server2", "server3")
"" > space.txt
$myServers | foreach-object {
write-host "Server: $_"
Get-WmiObject -Class Win32_LogicalDisk -ComputerName $_ | ? { $_.DeviceID -notmatch " [AR]"} | Select -Property __Server, DeviceID, @{Name=’FreeSpaceMB’;Expression={$_.FreeSpace/1MB} } | Format-Table -AutoSize >> c:space.txt
}