Powershell 4.0 Array Building and Import/Export CSV -
I am trying to import a CSV file, search and export the serial number against the computer names in the CSV file Computer name and serial number combination in a new CSV file. Even that which I have been successful so far, is not working because I expect it (still new in powers).
$ csv = import-CSV "C: \ Computer_Name_CSV_Generator.csv" $ Resultsarray = @ () $ wmiresults = new Object PSCustomObject $ wmiresults | Add Member Member Type Note Professional -name Computer_Name - Price $ Computer_Name.Computer_Name $ wmearsults | Add-member type NoteProperty -nam serialnumber $ Computer_Name in -Value $ getwmiSN foreach ($ csv) {if ($ Computer_Name.Computer_Name -ne 0) {$ getwmiSN = Get-WmiObject -ComputerName $ Computer_Name.Computer_Name win32_bios | Select the object serial number $ wmiresults.Computer_Name = $ Computer_Name.Computer_Name $ wmearsults.SerialNumber = $ getwmiSN.SerialNumber} $ Prinamare + = $ wmiresults} $ Prinamarere | Export-CSV "C: \ Computer Sseriyl Nughsissis" - there is also the type of information -ankoding UTF-8 Every time that the array update at the end of the exchange, all data Is modified by the final machine / serial number through foreach when it is exported, then i get the CSV completely with the final computer name and serial number, but import CSV The right amount of.
If the statement is to deal with blank rows in the original CSV result in 0.
You need to create an object for each computer, and not modify an object repeatedly. Try:
$ CSV = Import-CSV "C: \ Computer_Name_CSV_Generator.csv" $ resultsarray = @ () foreach (in $ Computer_Name $ 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} $ resultAre = $ wmiresults} } $ ResultsAmerican | Export-Csv "C: \ ComputerSerialNumbers.csv" -naitaip information encoding UTF8 You can also make it easier to use the pipeline support in PowerShell. This will allow you to reduce it:
Import-CSV "C: \ Computer_Name_CSV_Generator.csv" | Foreach-object {if ($ _ Computer_Name -ne 0) {$ getwmiSN = get- WmiObject -ComputerName $ _ Computer_Name new object PSCustomObject -Property @ {"Computer_Name" win32_bios = $ _ Computer_Name "serialnumber" = $ getwmiSN .SerialNumber}}} | Export-Csv "C: \ ComputerSerialNumbers.csv" -NoTypeInformation -Encoding UTF8
Comments
Post a Comment