locked
combine cmdlet output into csv file? RRS feed

  • Question

  • Hi there!

    I am trying to combine output from cmdlets into one csv file.

    I want to take the following as an example:

    $users = get-qaduser -searchroot $OU

    get-casmailbox $users|select activesyncenabled
    get-mailbox $users|select database
    get-qaduser $users|select samaccountname, lastlogin
    get-csuser $users|select enabled

    And take the outputs from each of thoose commands and build a csv file with them.

    Ive tried both add-member and tried to build a string of my own values and experimented with tables but i cant get it to work.

    I think my problem is the loop when i go through the users, i cant get it to stick for some reason.

    Any help would be appriciated, i can get get it to work using the add-member feature with only two cmdlets but how would i go about expand this to a third or even forth?

    Wednesday, February 8, 2012 1:24 AM

Answers

  • This is not tested, but it should give you a pattern to follow based on the code you've posted.

    $results = @()
    
    get-qaduser -searchroot $OU |
    foreach { 
    
        $props = @{
            activesyncenabled = (get-casmailbox $_).activesyncenabled
            database = (get-mailbox $_).database
            samaccountname = $_.samaccountname
            lastlogin = $_.lastlogin
            enabled = (get-csuser $_).enabled
            }
    
    $results += new-object psobject -Property $props
    }
    
    $results | export-csv c:\somedir\results.csv -NoTypeInformation

    Anything there you don't understand after reading the help files on it, ask and I (or someone) will try to explain it.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


    • Edited by mjolinor Wednesday, February 8, 2012 2:29 AM
    • Proposed as answer by jrv Wednesday, February 8, 2012 8:59 AM
    • Marked as answer by Klorgas Wednesday, February 8, 2012 11:10 PM
    Wednesday, February 8, 2012 2:26 AM

All replies

  • This is not tested, but it should give you a pattern to follow based on the code you've posted.

    $results = @()
    
    get-qaduser -searchroot $OU |
    foreach { 
    
        $props = @{
            activesyncenabled = (get-casmailbox $_).activesyncenabled
            database = (get-mailbox $_).database
            samaccountname = $_.samaccountname
            lastlogin = $_.lastlogin
            enabled = (get-csuser $_).enabled
            }
    
    $results += new-object psobject -Property $props
    }
    
    $results | export-csv c:\somedir\results.csv -NoTypeInformation

    Anything there you don't understand after reading the help files on it, ask and I (or someone) will try to explain it.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


    • Edited by mjolinor Wednesday, February 8, 2012 2:29 AM
    • Proposed as answer by jrv Wednesday, February 8, 2012 8:59 AM
    • Marked as answer by Klorgas Wednesday, February 8, 2012 11:10 PM
    Wednesday, February 8, 2012 2:26 AM
  • Hi,

    You can capture the outputs you need in variables, and then output a custom object containing the properties and values you want. For example:

    # One way
    $item1 = "Value 1"
    $item2 = "Value 2"
    $item3 = "Value 3"
    
    $outputObject = new-object PSObject
    $outputObject | add-member NoteProperty "Property 1" $item1
    $outputObject | add-member NoteProperty "Property 2" $item2
    $outputObject | add-member NoteProperty "Property 3" $item3
    
    $outputObject
    
    # Alternate technique
    $item1 = "Value 1"
    $item2 = "Value 2"
    $item3 = "Value 3"
    "" | select-object @{Name="Property 1"; Expression={$item1}},
      @{Name="Property 2"; Expression={$item2}},
      @{Name="Property 3"; Expression={$item3}}
    

    HTH,

    Bill

    Wednesday, February 8, 2012 2:33 AM
  • Thank you!

    It worked like a charm, appriciate the help!

    Wednesday, February 8, 2012 11:17 PM