locked
How can I use export-csv from invoke-command to get correct data format RRS feed

  • Question

  • Hi expert:

                  Below is my code,when I use output to screen,the output format  like this

    Username          Ipaddress             Port              Server Name                 Time                            
    --------                 --------                 --------          ----------------                   --------
    yyccmm                                                                       yy-mm                  2014/3/21                    

                

      But when I export to csv(invoke-command -computername xx,yy,zz -scriptblock $command -ArgumentList $name | export-csv),  the data format  chnage to

    "PSComputerName","RunspaceId","PSShowComputerName","ClassId2e4f51ef21dd47e99d3c952918aff9cd","pageHeaderEntry","pageFooterEntry","autosizeInfo","shapeInfo","groupingEntry"
    "yy-mm","72a287e5-3c96-47d2-9d98-5444c9b5f792","True","033ecb2bc07a4d43b5ef94ed5a35d280",,,,"Microsoft.PowerShell.Commands.Internal.Format.TableHeaderInfo",
    "yy-mm","72a287e5-3c96-47d2-9d98-5444c9b5f792","True","9e210fe47d09416682b841769c78b8a3",,,,,

               

      How can I export data format like output to screen fromat~  thanks~

    $command={param($name)
      $xmlfilter=[xml]@"
    
    <QueryList>
      <Query Id="0" Path="Security">
        <Select Path="Security">
                     *[EventData[Data[@Name='targetUserName'] and (Data='$name')   ] ]
                    
                      and
                     *[System[(EventID='4624')]]
    
                   </Select>
      </Query>
    </QueryList>
    "@
     
    $napevents=get-winevent -filterxml $xmlfilter
    $napevents  | ft  `
    @{Name="Username";expression={$_.properties[5].value}},`
    @{Name="Ipaddress";expression={$_.properties[18].value}},
    @{Name="Port";expression={$_.properties[19].value}},
    @{Name="Server Name";expression={$_.Machinename}} ,
    @{Name="Time";Expression={$_.TimeCreated.ToString('g')}} | write-output""
    }
    invoke-command -computername xx,yy,zz -scriptblock $command -ArgumentList $name




    • Edited by Edison0206 Friday, March 21, 2014 1:13 AM
    Friday, March 21, 2014 1:06 AM

Answers

  • $napevents  |
         Select @{Name="Username";expression={$_.properties[5].value}},
                @{Name="Ipaddress";expression={$_.properties[18].value}},
                @{Name="Port";expression={$_.properties[19].value}},
                @{Name="Server Name";expression={$_.Machinename}} ,
                @{Name="Time";Expression={$_.TimeCreated.ToString('g')}} |
          Export-CSv <file>
    


    ¯\_(ツ)_/¯

    • Marked as answer by Edison0206 Friday, March 21, 2014 2:11 AM
    Friday, March 21, 2014 1:20 AM
  • I got it to work this way:

    $command={param($name)
      $xmlfilter=[xml]@"
    
    <QueryList>
      <Query Id="0" Path="Security">
        <Select Path="Security">
                     *[EventData[Data[@Name='targetUserName'] and (Data='$name')   ] ]
                    
                      and
                     *[System[(EventID='4624')]]
    
                   </Select>
      </Query>
    </QueryList>
    "@
     
    $Results = @()
    $napevents=get-winevent -filterxml $xmlfilter
    $napevents  | %{new-object psobject -property @{
       Username=$_.properties[5].value;
       Ipaddress=$_.properties[18].value;
       Port=$_.properties[19].value;
       'Server Name'=$_.Machinename;
       Time=$_.TimeCreated.ToString('g')
       }
      } | select Username,IPaddress,Port,'Server Name',Time
    } 
    invoke-command -computername xx,yy,zz -scriptblock $command -ArgumentList $name  | export-csv c:\temp\secevent.csv -notype
    

    • Marked as answer by Edison0206 Friday, March 21, 2014 2:58 AM
    Friday, March 21, 2014 2:01 AM

All replies

  • Don't use FT. It is not compatible with Export-Csv


    ¯\_(ツ)_/¯

    Friday, March 21, 2014 1:17 AM
  • $napevents  |
         Select @{Name="Username";expression={$_.properties[5].value}},
                @{Name="Ipaddress";expression={$_.properties[18].value}},
                @{Name="Port";expression={$_.properties[19].value}},
                @{Name="Server Name";expression={$_.Machinename}} ,
                @{Name="Time";Expression={$_.TimeCreated.ToString('g')}} |
          Export-CSv <file>
    


    ¯\_(ツ)_/¯

    • Marked as answer by Edison0206 Friday, March 21, 2014 2:11 AM
    Friday, March 21, 2014 1:20 AM
  • I got it to work this way:

    $command={param($name)
      $xmlfilter=[xml]@"
    
    <QueryList>
      <Query Id="0" Path="Security">
        <Select Path="Security">
                     *[EventData[Data[@Name='targetUserName'] and (Data='$name')   ] ]
                    
                      and
                     *[System[(EventID='4624')]]
    
                   </Select>
      </Query>
    </QueryList>
    "@
     
    $Results = @()
    $napevents=get-winevent -filterxml $xmlfilter
    $napevents  | %{new-object psobject -property @{
       Username=$_.properties[5].value;
       Ipaddress=$_.properties[18].value;
       Port=$_.properties[19].value;
       'Server Name'=$_.Machinename;
       Time=$_.TimeCreated.ToString('g')
       }
      } | select Username,IPaddress,Port,'Server Name',Time
    } 
    invoke-command -computername xx,yy,zz -scriptblock $command -ArgumentList $name  | export-csv c:\temp\secevent.csv -notype
    

    • Marked as answer by Edison0206 Friday, March 21, 2014 2:58 AM
    Friday, March 21, 2014 2:01 AM
  • Always a simpler way...
    Friday, March 21, 2014 2:02 AM
  • Like I said.  Remove the FT.  What I did and you did are virtually identical.


    ¯\_(ツ)_/¯

    Friday, March 21, 2014 2:14 AM
  • I see we are in Chinese today.


    ¯\_(ツ)_/¯

    Friday, March 21, 2014 2:15 AM
  • Hi Jrv:         another question , how can I exprot excel to match cloum and to set type alignment, not like txt date file ??  thanks~
    Friday, March 21, 2014 2:19 AM
  • What is cloum?  what do you mean by "type alignment'.  A CSV file is only text.  It cannot be anything else.

    I see you are speaking in some Chinese now.  Can we return to English.  I am having problems with my Chinese.


    ¯\_(ツ)_/¯

    Friday, March 21, 2014 2:25 AM
  • sorry Jrv, my English is poor, Describe the problem is not well,I fix the problem already. thank you~
    Friday, March 21, 2014 3:03 AM
  • Like I said.  Remove the FT.  What I did and you did are virtually identical.


    ¯\_(ツ)_/¯

    I created new PSObjects, probably not as efficient.  Contributing to these threads and seeing other ways to do things has helped me learn much faster than just plugging along with the powershell I 'have' to use for my job.  It's also very humbling at times...
    Friday, March 21, 2014 3:12 AM
  • Like I said.  Remove the FT.  What I did and you did are virtually identical.


    ¯\_(ツ)_/¯

    I created new PSObjects, probably not as efficient.  Contributing to these threads and seeing other ways to do things has helped me learn much faster than just plugging along with the powershell I 'have' to use for my job.  It's also very humbling at times...

    Not your English.  THe forum is being delivered in Chinese chatacters and the URLs are all in Chinese.

    See: http://social.technet.microsoft.com/Forums/scriptcenter/zh-TW/5f072868-af49-4ee5-945e-a ....

    The "zh-TW" is the link for the Chines (Taiwanese) version of the web page.


    ¯\_(ツ)_/¯

    Friday, March 21, 2014 5:05 AM