如何在csv文件中的计算机信息后面附加多个监视器信息?

时间:2019-03-27 10:16:01

标签: powershell csv

每次我运行powershell脚本时,我的输出都会输出到一个csv文件中,但我希望它全部显示在一行上,现在它是如何工作的,因为对于每台显示器,我的计算机信息都位于每个显示器信息的前面,如下所示: Current State

但是我只希望第N个monitorinfo像这样在第二个后面附加: The state i want it to be in

我使用的代码:

$output = foreach($computer in $Desktop){

  ForEach ($Monitor in $Monitors){

    $Manufacturer = ($Monitor.ManufacturerName -notmatch 0 | ForEach-Object{[char]$_}) -join ""
    $Name = ($Monitor.UserFriendlyName         -notmatch 0 | ForEach-Object{[char]$_}) -join ""
    $Serial = ($Monitor.SerialNumberID         -notmatch 0 | ForEach-Object{[char]$_}) -join ""

        [PSCustomObject]@{
            "Merk" = $Desktop.CsManufacturer
            "Model" = $Desktop.CsModel
            "S/n" = $Desktop | Select-Object -expand BiosSeralNumber
            "PC Naam" = $Desktop.CsName
            "CPU" = $processor.Name
            "Memory" = "$RAM GB"
            "OS" = $Desktop.WindowsProductName
            "MAC LAN" = $MACLAN
            "MAC WIFI" = $MACWIFI
            "Office" = $officeVersion
            "Merk /Model" = $Manufacturer
            "Type" = $Name
            "SerialScherm" = $Serial
        }

  }

}

$output | Export-Csv -Path $GL\info.csv -NoTypeInformation -Delimiter ';'

因此,如果我有3个监视器,则第3个监视器必须在第2个监视器之后追加到同一行

1 个答案:

答案 0 :(得分:0)

以下内容可能会满足您的要求:

$output = foreach($computer in $Desktop){
$Hash = [ordered]@{  "Merk" = $Desktop.CsManufacturer
            "Model" = $Desktop.CsModel
            "S/n" = $Desktop | Select-Object -expand BiosSeralNumber
            "PC Naam" = $Desktop.CsName
            "CPU" = $processor.Name
            "Memory" = "$RAM GB"
            "OS" = $Desktop.WindowsProductName
            "MAC LAN" = $MACLAN
            "MAC WIFI" = $MACWIFI
            "Office" = $officeVersion
    }
$MonitorNumbers = @($null)
$MonitorNumbers += (2..$Monitors.count)
$MonitorIndex = 0
  ForEach ($Monitor in $Monitors){

    $Manufacturer = ($Monitor.ManufacturerName -notmatch 0 | ForEach-Object{[char]$_}) -join ""
    $Name = ($Monitor.UserFriendlyName         -notmatch 0 | ForEach-Object{[char]$_}) -join ""
    $Serial = ($Monitor.SerialNumberID         -notmatch 0 | ForEach-Object{[char]$_}) -join ""

    $Hash.add("Merk /Model $($MonitorNumbers[$MonitorIndex])", $Manufacturer)
    $Hash.add("Type $($MonitorNumbers[$MonitorIndex])", $Name)
    $Hash.add("SerialScherm $($MonitorNumbers[$MonitorIndex])",$Serial)
    $MonitorIndex++
        }
[PSCustomObject]$Hash
  }

}