คำตอบ Append Column to CSV

  • 02 Mei 2012 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.ExportCsvCommand

    I 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!

Semua Balasan

  • 03 Mei 2012 0:26
     
     Jawab

    Look at Add-Member.

    HELP Add-Member -full


    ¯\_(ツ)_/¯

  • 05 Mei 2012 0:09
    Moderator
     
     Jawab Memiliki Kode

    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
  • 07 Mei 2012 22:32
    Moderator
     
     

    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!

  • 08 Mei 2012 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 ...
    $obj

    At 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.


    ¯\_(ツ)_/¯