【问题标题】:Add computer name to Table将计算机名称添加到表
【发布时间】: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


【解决方案1】:

Invoke-Command 在这样运行时会自动添加PSComputerName 属性。使用PSComputerName 而不是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

$Results = Invoke-Command -ComputerName $Computer {

    Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion"

}
$Results | Format-Table -Property PSComputerName, ProductName, ReleaseId

我还建议捕获结果,然后将其通过管道传输到 Format-Table,以便可以再次使用结果输出到文件或其他内容,或过滤或排序。

【讨论】:

    猜你喜欢
    • 2015-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多