Asked by:
Trouble with a CSV Import

Question
-
Hello,I am wondering if there are any <g class="gr_ gr_19 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="19" id="19">powershell</g> gurus out there that can give me a hand.I am importing a CSV file into a <g class="gr_ gr_30 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="30" id="30">PScustom</g> object (that acts as an array), with 4 columns and need to add entries to the object further <g class="gr_ gr_31 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" data-gr-id="31" id="31">along in</g> the script. When I try to do this and put the data in columns, I get the pleasant response of Cannot find an overload for "Add" and the argument count: "4".A simplified version of the code:
$Localadmins = import-csv "c:\admins.csv" $Local Admins Scope Admin Server Type ----- ----- ------ ---- Domain John.Smith SRV-SQL01 LocalAdmin Local svc_sql SRV-SQL01 LocalAdmin $LocalAdmins.Add("Domain", "Frank.Zappa", "SRV-SQL01", "LocalAdmin") Cannot find an overload for "Add" and the argument count: "4". At line:1 char:16
I have spent a couple hours researching this and am a bit stumped, happy to provide more info.
Thanks,
- Edited by Jester80 Tuesday, November 21, 2017 1:35 PM
- Moved by Bill_Stewart Friday, March 9, 2018 7:38 PM Unreadable
Tuesday, November 21, 2017 1:34 PM
All replies
-
1. When you paste text here please make sure there are no crappy html codes included plaese. Your post is almost unreadable.
2. When you want to "export" something to a CSV file you can use Export-CSV to do so … that could be one way
[PSCustomObject]@{ Scope = "Domain"; Admin = "Frank.Zappa"; Server = "SRV-SQL01"; Type = "LocalAdmin"; } | Export-Csv -Path <path to your csv file> -Delimiter ',' -NoTypeInformation -Append
Best regards,
(79,108,97,102|%{[char]$_})-join''
- Edited by BOfH-666 Tuesday, November 21, 2017 2:12 PM
Tuesday, November 21, 2017 2:12 PM -
Except for appending , as advised by BOfH_666, you can also use calculated properties:
$LocalAdmins | select *, @{n="Scope";e={"Domain"}}, @{n="Admin";e={"Frank.Zappa"}}, @{n="Server";e={"SRV-SQL01"}}, @{n="Type";e={"LocalAdmin"}}
The "n" (name) part of each calculated property reflects your column name from the csv, whereas the "e" (expression) part reflects its value. It's a useful method if you're looping through a number of objects with foreach, for example; you can then store your expression in a variable and call that variable while calculating properties.
- Proposed as answer by Paweł Balczunas Tuesday, November 21, 2017 4:30 PM
Tuesday, November 21, 2017 4:29 PM -
The question, if the OP would fix the crappy post, seems to be how to add a row to an existing CSV.
The answer is to clone a row and modify it then add it to the original. Do you know how to "clone"?
\_(ツ)_/
Tuesday, November 21, 2017 5:02 PM -
As an alternative we can use ConvertTo-DataTable to build an object that can be extended using and array.
$dt = $csv | ConvertTo-Datatable $dt.Rows.Add((@( .... values ...)) $dt | Export-Csv ...
\_(ツ)_/
- Proposed as answer by Richard MuellerMVP, Banned Tuesday, November 28, 2017 9:43 AM
Tuesday, November 21, 2017 5:22 PM