Append Column to CSV
-
2 พฤษภาคม 2555 23:12
I posted this question, got a little traction, but then the interest apparently waned. Hoping that one of you gurus can assist.
I'm trying to get-activesyncdevicestatistics and have it include the user's mailbox. I'm able to get the device info and have it in a csv. I also have the user's display name (Last, First) in the file. I need to run a get-casmailbox | select servername and have those results inserted into the original file as a column called something like mailboxserver. The devices file is delimited with a semicolon. Here is what I've got so far:
$CSVObjects = Import-Csv "C:\temp\devices.csv" -Delimiter ';'
$NewCSVObject = @()
#$File | Select-Object DisplayName
foreach ($item in $CSVObjects)
{
$newvalue= get-casmailbox -identity $item.DisplayName |select servername
write-output $newvalue
$NewCSVObject += $item | Add-Member -name "mailboxserver" -value $newvalue -MemberType NoteProperty
}
$NewCSVObject | export-csv "c:\temp\devices.csv" -noType -encoding "unicode"When I run it, the devices.csv file gets nulled and I get this error:
Export-Csv : Cannot bind argument to parameter 'InputObject' because it is null.
At C:\EAS-Scripts\NewGetDeviceInfo1.3.ps1:22 char:27
+ $NewCSVObject | export-csv <<<< "c:\temp\devices.csv" -noType -encoding "unicode"
+ CategoryInfo : InvalidData: (:) [Export-Csv], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCsvCommandI think the culprit is this line: $NewCSVObject += $item | Add-Member -name "mailboxserver" -value $newvalue -MemberType NoteProperty
I know that $newvalue contains the mailbox server name, but it's not getting appended to the object. Any ideas?
Thanks!
ตอบทั้งหมด
-
3 พฤษภาคม 2555 0:26
Look at Add-Member.
HELP Add-Member -full
¯\_(ツ)_/¯
- ทำเครื่องหมายเป็นคำตอบโดย Cruz_DanielModerator 7 พฤษภาคม 2555 22:29
-
5 พฤษภาคม 2555 0:09ผู้ดูแล
The issue here is that Add-* won't return (by default) anything. In effect:
# because...: ($foo | Add-Member -Name Bar -Value 'Value' -MemberType NoteProperty) -eq $null # ... line where you try to add "update" object does that: $NewCSVObject += ($null) # to avoid it, you need to force Add- to Pass objects Thru. $NewCSVObject += $item | Add-Member ... -PassThru
HTH
Bartek- ทำเครื่องหมายเป็นคำตอบโดย Cruz_DanielModerator 7 พฤษภาคม 2555 22:27
-
7 พฤษภาคม 2555 22:32ผู้ดูแล
Both are good answers. Bartek pointed out the specific problem, and you can follow JRV's advice to learn more about the -PassThru param and see some examples of Add-Member in action.
Good Luck!
-Dan
Please click "Vote as Helpful" if this post was helpful to you. Thanks!
-
8 พฤษภาคม 2555 0:32
What the OP and others should rty to understand is that his struture in the pipeline in unnecessary.
In a pipeline we would mostly like to do this construct:
$result=func1|func2|func3....
To get this to happen we must do this for each object at each stage of the pipe:
Either:
New-Object PSObject ....OR
$obj|add-member ... -passthru.
We can also do this.
$obj|add-member ...
$obj|add-member ...
$objAt each step we add a member then send the object to the pipeline or we can just place "-passthru" on the last 'add-member' statement.
The big take-away here is that the pipeline will aggregate all of the objects that we output and create our collection. No need for $obj+=... kind of laborious construct. It will create very curious failures under many circumstances.
¯\_(ツ)_/¯