locked
Export to csv RRS feed

  • Question

  • I am trying to run a script and export the results to csv. The script runs fine onscreen but when the output is sent to the csv file the results for msDS-PrimaryComputer all return "Microsoft.ActiveDirectory.Management.ADPropertyValueCollection".

    Here is the script I am running:

    Get-ADUser -Filter * -Properties * | Select Name,sAMAccountName,msDS-PrimaryComputer | Export-Csv 'c:\Temp\ADUserPrimaryComputer.csv' -NoTypeInformation
    Get-Content "C:\Temp\ADUserPrimaryComputer.csv"

    The output is fine on screen but not in the csv file. From my research I gather that this is due to msDS-PrimaryComputer containing a list of values but not being familiar with scripting, I haven't been able to figure out how to make it work. Here is the code I used with -Expand

    Get-ADUser -Filter * -Properties * |
        Select -Expand msDS-PrimaryComputer
    Get-ADUser -Filter * -Properties * |
        Name,sAMAccountName,@{N='msDS-PrimaryComputer';E={$_.msDS-PrimaryComputer -join '|'}} |
        Export-Csv 'c:\Temp\ADUserPrimaryComputer.csv' -NoTypeInformation
    Get-Content "C:\Temp\ADUserPrimaryComputer.csv"

    Thanks in advance for any help you can provide.

    Tuesday, August 20, 2019 5:40 PM

Answers

  • Thank you. Now it ran but still returns "Microsoft.ActiveDirectory.Management.ADPropertyValueCollection". Any idea what I have left out?

    I suspect the value is null so the type is being returned.

    @{n = 'msDS-PrimaryComputer'; e = {$_.'msDS-PrimaryComputer'}}


    \_(ツ)_/

    • Marked as answer by John Larimer Tuesday, August 20, 2019 7:39 PM
    Tuesday, August 20, 2019 7:09 PM

All replies

  • Because it is a collection and that has to be converted to a single string using "-join"


    \_(ツ)_/

    Tuesday, August 20, 2019 6:33 PM
  • See: https://blogs.technet.microsoft.com/askds/2012/10/23/digging-a-little-deeper-into-windows-8-primary-computer/

    If it is not set or the attribute is set wrong then you will get that error.


    \_(ツ)_/

    Tuesday, August 20, 2019 6:37 PM
  • I have tried using -join:

    Get-ADUser -Filter * -Properties * |
        Select Name,
            sAMAccountName,
            @{Name='msDS-PrimaryComputer';Expression={[string]::join(";",($_.msDS-PrimaryComputer))}} |
        Export-Csv 'c:\Temp\ADUserPrimaryComputer.csv' -NoTypeInformation
    Get-Content "C:\Temp\ADUserPrimaryComputer.csv"

    but it throws an error:

    At line:4 char:78
    + ... mputer';Expression={[string]::join(";",($_.msDS-PrimaryComputer))}} |
    +                                                    ~~~~~~~~~~~~~~~~
    Unexpected token '-PrimaryComputer' in expression or statement.
        + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : UnexpectedToken

    I'm just having trouble getting it right.

    Tuesday, August 20, 2019 6:44 PM
  • Because the attribute msDS-PrimaryComputer had a dash character, it must be quoted after "$_.".

    It should be:

    $_."msDS-PrimaryComputer"


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)


    • Edited by Richard MuellerMVP Tuesday, August 20, 2019 6:59 PM
    • Proposed as answer by jrv Tuesday, August 20, 2019 7:04 PM
    Tuesday, August 20, 2019 6:53 PM
  • Thank you. Now it ran but still returns "Microsoft.ActiveDirectory.Management.ADPropertyValueCollection". Any idea what I have left out?

    Tuesday, August 20, 2019 7:04 PM
  • The attribute is a string of the distinguished name.  When you expand it on the command line the dashes do not matter without wthout quotes.  As a property of an object, quotes are required.  Richard saw your issue.  No need to join.


    \_(ツ)_/

    Tuesday, August 20, 2019 7:06 PM
  • Thank you. Now it ran but still returns "Microsoft.ActiveDirectory.Management.ADPropertyValueCollection". Any idea what I have left out?

    I suspect the value is null so the type is being returned.

    @{n = 'msDS-PrimaryComputer'; e = {$_.'msDS-PrimaryComputer'}}


    \_(ツ)_/

    • Marked as answer by John Larimer Tuesday, August 20, 2019 7:39 PM
    Tuesday, August 20, 2019 7:09 PM
  • Awesome! It worked!

    Thank you very much!

    Tuesday, August 20, 2019 7:39 PM