似乎无法合并2个数组,因为我希望将其合并到一个csv文件中

时间:2019-03-25 14:58:53

标签: powershell csv

我有两段代码,我想合并为一个数组以导出到csv文件,目的是我首先从桌面上获取信息(获取品牌,型号,序列号...),放入这样的数组:

$outputArray = @()

foreach($computer in $Desktop){

    $output = [Ordered]@{
        "Merk" = $Desktop.CsManufacturer
        "Model" = $Desktop.CsModel
        "S/n" = $Desktop.BiosSeralNumer
    }
    $outputArray += New-Object PSObject -Property $output

}

第二部分是我想从连接的显示器中检索所有显示器信息到我的设备:

$outputArrayMon = @()

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

        $output = [Ordered]@{
            "Merk /Model" = $Manufacturer
            "Type" = $Name
            "S/n" = $Serial
        }

    $outputArrayMon += New-Object PSObject -Property $output

}

我正在尝试像这样合并它们并将其导出到csv文件

$outputArrayRES = $outputArray + $outputArrayMon

$outputArrayRES | Export-Csv -Path $GL\info.csv -NoTypeInformation

当我导出到文本文件时,我的结果非常正常且清晰,但是我不知道如何使它在csv文件中工作,这是我在csv文件中输出的示例(还有更多示例输出,但只是为了使帖子更清晰):

csv output

问题是我什至没有在文本文件中正确接收到csv文件中的某些值。

我希望它工作的方式是每个值都必须在CSV文件中的同一行上。而不是在4条单独的线上,我需要2 1 =变量和2 =变量的值

EDIT(1):

我的输出文件中的监视值怪异

";""
;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;

EDIT(2):

[PSCustomObject]@{
            "Merk" = $Desktop.CsManufacturer
            "Model" = $Desktop.CsModel
            "S/n" = $Desktop.BiosSeralNumer
            "PC Naam" = $Desktop.CsName
            "CPU" = $processor.Name
            "Memory" = "$RAM GB"
            "OS" = $Desktop.WindowsProductName
            "MAC LAN" = $MACLAN
            "MAC WIFI" = $MACWIFI
            "Office" = $officeVersion
            "Merk /Model" = ($Monitor.ManufacturerName -notmatch 0 | ForEach-Object{[char]$_}) -join ""
            "Type" = ($Monitor.UserFriendlyName         -notmatch 0 | ForEach-Object{[char]$_}) -join ""
            "SerialScherm" = ($Monitor.SerialNumberID         -notmatch 0 | ForEach-Object{[char]$_}) -join ""

}

2 个答案:

答案 0 :(得分:1)

您应该组合两个foreach循环,并创建PSCustomObjectbunzab一样,但要同时具有计算机及其监视器的属性。

# assuming $Desktop is an array of computer names
$Desktop    = @('pc1','pc2','pc3')
$outputFile = '<PATH AND FILENAME FOR THE OUTPUT CSV FILE>'

$info = foreach($computer in $Desktop) {
    # get the info you want for this computer. You didn't state HOW you did that, probably using Get-ComputerInfo.
    # On my Windows 7 machine this still fails with error:
    # Unable to find an entry point named 'GetFirmwareType' in DLL 'kernel32.dll'
    # so I used these commands instead:

    $pcInfo   = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $computer
    $biosInfo = Get-WmiObject -Class Win32_Bios -ComputerName $computer

    # store these properties for later use
    $pcName         = $pcInfo.Name
    $pcManufacturer = $pcInfo.Manufacturer
    $pcModel        = $pcInfo.Model
    $pcBiosSN       = $biosInfo.SerialNumber

    # next get the monitor info for this computer
    $Monitors = Get-WmiObject -Class WmiMonitorID -Namespace root\wmi -ComputerName $computer
    foreach($monitor in $Monitors) {
        # emit a PSCustomObject with all properties combined
        [PSCustomObject]@{
            'Computer Naam'       = $pcName
            'Computer Merk'       = $pcManufacturer
            'Computer Model'      = $pcModel
            'BIOS S/N'            = $pcBiosSN
            "Monitor Merk /Model" = ($Monitor.ManufacturerName -ne 0 | ForEach-Object { [char]$_ } ) -join ''
            "Monitor Naam"        = ($Monitor.UserFriendlyName -ne 0 | ForEach-Object { [char]$_ } ) -join ''
            "Monitor S/N"         = ($Monitor.SerialNumberID   -ne 0 | ForEach-Object { [char]$_ } ) -join ''
        }
    }
} 

$info | Export-Csv -Path $outputFile -NoTypeInformation -Delimiter ';'

希望这会有所帮助

注意:查看屏幕快照,我看到您双击了输出CSV文件以在Excel中打开它,但是您当前的语言环境(NL)随后将所有内容都放在了第一列中。 这就是为什么我添加了-Delimiter ';'。如果您是在同一台计算机上双击输出的CSV文件,也可以使用-UseCulture开关。


编辑


在最近的评论中,您说您希望显示器信息与计算机信息位于同一行。这意味着如果计算机具有多个监视器,则在输出CSV中添加更多列。 如果需要这样做,请在下面使用以下代码:

# assuming $Desktop is an array of computer names
$Desktop    = @('pc1','pc2','pc3')
$outputFile = '<PATH AND FILENAME FOR THE OUTPUT CSV FILE>'

$info = foreach($computer in $Desktop) {
    # get the info you want for this computer. You didn't state HOW you did that, probably using Get-ComputerInfo.
    # On my Windows 7 machine this still fails with error:
    # Unable to find an entry point named 'GetFirmwareType' in DLL 'kernel32.dll'
    # so I used these commands instead:

    $pcInfo   = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $computer
    $biosInfo = Get-WmiObject -Class Win32_Bios -ComputerName $computer

    # create a PSCustomObject with all properties combined
    # first add computer properties. (insert more when needed)
    $obj = [PSCustomObject]@{
        'Computer Naam'  = $pcInfo.Name
        'Computer Merk'  = $pcInfo.Manufacturer
        'Computer Model' = $pcInfo.Model
        'BIOS S/N'       = $biosInfo.SerialNumber
    }
    # next get the monitor info for this computer
    $Monitors = @(Get-WmiObject -Class WmiMonitorID -Namespace root\wmi -ComputerName $computer)
    for ($i = 0; $i -lt $Monitors.Count; $i++) {
        # add monitor properties to the object
        $obj | Add-Member -MemberType NoteProperty -Name "Monitor $($i + 1) Merk /Model" -Value (($Monitors[$i].ManufacturerName -ne 0 | ForEach-Object { [char]$_ } ) -join '')
        $obj | Add-Member -MemberType NoteProperty -Name "Monitor $($i + 1) Naam"        -Value (($Monitors[$i].UserFriendlyName -ne 0 | ForEach-Object { [char]$_ } ) -join '')
        $obj | Add-Member -MemberType NoteProperty -Name "Monitor $($i + 1) Serienummer" -Value (($Monitors[$i].SerialNumberID   -ne 0 | ForEach-Object { [char]$_ } ) -join '')
    }
    # output the object
    $obj
} 

$info | Export-Csv -Path $outputFile -NoTypeInformation -Delimiter ';' -Force

答案 1 :(得分:0)

请改用自定义对象。导出时,它也将为您提供csv中的标题:

$outputArray = @()

foreach($computer in $Desktop){

    $output = [PSCustomObject]@{
        Merk = $Desktop.CsManufacturer
        Model = $Desktop.CsModel
        S/n = $Desktop.BiosSeralNumer
    }
    $outputArray += New-Object PSObject -Property $output

}