简体   繁体   English

Powershell 4.0阵列构建和导入/导出CSV

[英]Powershell 4.0 Array Building and Import/Export CSV

I'm attempting to import a csv file, run a serial number search against the computer names in the csv file, and export the computer name and serial number combinations to a new csv file. 我正在尝试导入一个csv文件,对csv文件中的计算机名称运行序列号搜索,并将计算机名称和序列号组合导出到一个新的csv文件中。 Here's what I've managed to make so far that is not acting as I would expect it(still new to powershell). 到目前为止,这是我设法无法达到预期效果的功能(仍然是Powershell的新功能)。

$csv = Import-CSV "C:\Computer_Name_CSV_Generator.csv"
$resultsarray = @()

$wmiresults = New-Object PSCustomObject
$wmiresults | Add-Member -type NoteProperty -Name Computer_Name -Value   $Computer_Name.Computer_Name
$wmiresults | Add-Member -type NoteProperty -Name SerialNumber -Value $getwmiSN   

Foreach ($Computer_Name in $csv)
{
If ($Computer_Name.Computer_Name -ne 0)
    {
    $getwmiSN = Get-WmiObject -ComputerName $Computer_Name.Computer_Name win32_bios | Select-object SerialNumber
    $wmiresults.Computer_Name = $Computer_Name.Computer_Name
    $wmiresults.SerialNumber = $getwmiSN.SerialNumber
    }
$resultsarray += $wmiresults
}
$resultsarray | Export-Csv "C:\ComputerSerialNumbers.csv" -NoTypeInformation -Encoding UTF8

What's happening is every time the array is updated at the end of the foreach, all the data to is modified to that of the last machine/serial number run through the foreach. 发生的情况是,每次在foreach末尾更新阵列时,所有要修改的数据都将修改为遍历foreach的最后一个机器/序列号的数据。 When it's exported, I receive a csv entirely composed of the last computer name and serial number, but in the right amount of the import csv. 导出后,我收到的csv完全由最后的计算机名称和序列号组成,但是导入的csv数量合适。

The If statement is for dealing with blank lines in the original csv that result in a 0. If语句用于处理原始csv中的空行,结果为0。

You need to create an object for each computer, not just modify that one object over and over again. 您需要为每台计算机创建一个对象,而不仅仅是一遍又一遍地修改该对象。 Try: 尝试:

$csv = Import-CSV "C:\Computer_Name_CSV_Generator.csv"
$resultsarray = @()


Foreach ($Computer_Name in $csv)
{
    If ($Computer_Name.Computer_Name -ne 0)
    {

        $getwmiSN = Get-WmiObject -ComputerName $Computer_Name.Computer_Name win32_bios

        $wmiresults = New-Object PSCustomObject -Property @{
            "Computer_Name" = $Computer_Name.Computer_Name
            "SerialNumber" = $getwmiSN.SerialNumber
        }

        $resultsarray += $wmiresults

    }

}

$resultsarray | Export-Csv "C:\ComputerSerialNumbers.csv" -NoTypeInformation -Encoding UTF8

You could also simplify it to use the pipeline-support in PowerShell. 您还可以简化它以使用PowerShell中的管道支持。 That would allow you to shorten it to something like: 这样可以将其缩短为:

Import-CSV "C:\Computer_Name_CSV_Generator.csv" |
ForEach-Object {

    if($_.Computer_Name -ne 0) {
        $getwmiSN = Get-WmiObject -ComputerName $_.Computer_Name win32_bios

        New-Object PSCustomObject -Property @{
                "Computer_Name" = $_.Computer_Name
                "SerialNumber" = $getwmiSN.SerialNumber
        }   
    }

} | Export-Csv "C:\ComputerSerialNumbers.csv" -NoTypeInformation -Encoding UTF8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM