【问题标题】:Export computer info to CSV file using get-content使用 get-content 将计算机信息导出到 CSV 文件
【发布时间】:2020-05-19 16:13:12
【问题描述】:

我希望得到一些帮助,让我称之为简单的脚本工作,但我已经达到了我的 PS 知识上限。我发现了一些帖子,其他人试图做类似的事情,但对我没有帮助。

我有一个概念,它将数据输出到每台计算机的 CSV 文件中,但每台计算机的信息是相同的。我已经多次修改脚本,但我现在没有想法。我正在运行 PS 5.0。这是我目前所拥有的:

$Computers = get-content "C:\Users\test\documents\computers.txt"
$ExportLocation = "C:\Users\test\documents"

$Score = Get-CimInstance Win32_ReliabilityStabilityMetrics | measure SystemStabilityIndex -Average | select-object -ExpandProperty Average
$Manufacturer = Get-ComputerInfo | select -ExpandProperty "CsManufacturer"
$Output = foreach ($Computer in $Computers) {
    New-Object -TypeName PSObject -Property @{
    ComputerName = $Computer
    StabilityScore = $Score
    Manufacturer = $Manufacturer

  } | Select-Object ComputerName,StabilityScore,Manufacturer
}
$Output | Export-CSV $ExportLocation\StabilityResults.csv -Append -Force -Encoding ASCII -NoTypeInformation

任何帮助将不胜感激!

【问题讨论】:

    标签: powershell powershell-2.0 powershell-3.0 powershell-4.0


    【解决方案1】:

    我相信您想从远程计算机收集信息,对吧? 当前,您正在从本地计算机收集分数和制造商,并在前面放置一个不同的计算机名称。

    我会这样做,并会解释原因:

    $Computers = get-content "C:\Users\test\documents\computers.txt"
    $ExportLocation = "C:\Users\test\documents"
    
    $Output = Invoke-Command -ComputerName $Computers {
        [PSCustomObject]@{
            ComputerName = $env:COMPUTERNAME
            Score = (Get-CimInstance Win32_ReliabilityStabilityMetrics | measure SystemStabilityIndex -Average).Average
            Manufacturer = (Get-CimInstance -ClassName Win32_ComputerSystem).Manufacturer
        }
    } | Select-Object ComputerName,Score,Manufacturer
    
    $Output | Export-CSV $ExportLocation\StabilityResults.csv -Append -Force -Encoding ASCII -NoTypeInformation
    

    我会使用 Invoke-command 来访问远程计算机,因为它会针对 32 台计算机并行发送命令(默认情况下)。可以使用变量 Throttlelimit 增加或减少计算机的数量。

    如果您使用像 foreach 这样的循环来执行此操作,您将按顺序运行它,这显然会更慢。

    我已将Get-ComputerInfo 替换为(Get-CimInstance -ClassName Win32_ComputerSystem).Manufacturer,因为您将获得相同的结果,但速度更快。 Get-ComputerInfo 收集的信息太多,因此速度较慢。

    变量$env:computername 从远程机器返回计算机名称。

    试一试并提供反馈。我希望解释清楚且有用!

    【讨论】:

    • 嘿伊万,太棒了!非常感谢!那成功了。我只需要在调用命令之后更新选择对象以说出“计算机名称、分数、制造商”以匹配任何想要运行它的人的 PSCustomerObject 属性。我还添加了 ThrottleLimit 命令,因为我最终将在超过 32 个节点上运行它,感谢您指出这一点。
    • 感谢您的提示,我已经编辑了答案!很高兴为您服务! :)
    猜你喜欢
    • 2017-05-21
    • 2017-12-30
    • 1970-01-01
    • 2013-05-17
    • 1970-01-01
    • 1970-01-01
    • 2019-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多