【发布时间】:2019-04-06 02:24:25
【问题描述】:
我正在使用脚本从远程计算机列表中获取 Windows 操作系统版本信息。我想将计算机主机名添加到输出中。
我能够在表输出中创建“ComputerName”标题,但我正在查看的注册表配置单元中没有实际的计算机名称或主机名属性。我已经修补了一段时间试图弄清楚它,但没有得到任何结果。
Write-Host "Please select the machine list"
Function Get-FileName($InitialDirectory) {
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
#$OpenFileDialog.filter = "CSV (*.csv)| *.csv|TXT (*.txt)| *.txt"
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
}
$inputfile = Get-FileName "C:"
$Computer = get-content $inputfile
Invoke-Command -ComputerName $Computer {
Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion"
} | Format-Table -Property ComputerName, ProductName, ReleaseId
这就是我得到的
ComputerName ProductName ReleaseId
------------ ----------- ---------
Windows 10 Enterprise 1709
有谁知道我如何用我的计算机列表中的机器填充计算机名称列?
【问题讨论】:
-
通常的方法是不使用有问题的
Format-*cmdlet 并使用Select-Object或 - 更好地 - 构建自定义对象。 [grin] 只需让您的脚本块构建一个自定义对象并返回它。如果你想要这个想法的演示,我可以发布一个作为答案。 -
Invoke-Command在这样运行时会自动添加PSComputerName属性。使用PSComputerName而不是ComputerName。 -
Lee_Dailey 和 TheMadTechnician。这两个都有帮助。我现在得到了我想要的输出。谢谢你们!
标签: powershell