pipe result to a .csv or .txt files

  • Hi All,
    i find it hard to pick up powershell...need some help here.... .
    i have some result as below:

    PS C:\Users\xxx> $strReference = Get-Content "C:\temp\cep\book1.csv"
    PS C:\Users\xxx> $strDifference = Get-Content "C:\temp\cep\book2.csv"
    PS C:\Users\xx> Compare-Object $strReference $strDifference -PassThru

    server1\instance1,LOGPp,Hegg_Role_USR,SQL_USER,SELECT,Tables,HR.departments
    server1\instance1,LOGPp,Herr_Role_USR,SQL_USER,DELETE,Tables,HR.departments

    Qns: i need to pipe the result into a csv or a txt files in the format as below:


    severname           dbname   role                      type             permission   objectype   tablename
    server1\instance1 LOGPp    Hegg_Role_USR  SQL_USER  SELECT     Tables        HR.departments
    server1\instance1 LOGPp   Herr_Role_USR    SQL_USER  DELETE     Tables        HR.departments

    not too sure how to really pipe it using power shell .. . please advice.

  • To get a file of the differences then you can do the following:

    PS C:\Users\xx> Compare-Object $strReference $strDifference -PassThru | Out-File "C:\temp\cep\result.txt"

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

  • Gary Varga - Monday, January 23, 2017 4:56 AM

    To get a file of the differences then you can do the following:

    PS C:\Users\xx> Compare-Object $strReference $strDifference -PassThru | Out-File "C:\temp\cep\result.txt"

    have decided to compare 2 txt files as the source

    serverInstance databaseName loginName Prividges Object
    ---------------- ------------- ---------- ---------- --------
    CHWDB01\FEGG  FEGG    FEGG_USER  SELECT  Buller.Trainer
    CHWDB01\FEGG  FEGG    FEGG_USER  DELECT  Buller.Trainer

    serverInstance databaseName loginName Prividges Object
    ---------------- ------------- ---------- ---------- --------
    CHWDB01\FEGG  FEGG    FEGG_USER  SELECT  Buller.Trainer
    CHWDB01\FEGG  FEGG    FEGG_USER  DELECT  Buller.Trainer
    CHWDB01\FEGG  FEGG    FEGG_USER  EXECUTE  Buller.Trainer

    clear-host
    $strReference = Get-Content "C:\temp\cep\test1.rpt"
    $strDifference = Get-Content "C:\temp\cep\test2.rpt"
    Compare-Object $strReference $strDifference | Format-Table -AutoSize | out-file C:\temp\cep\yourfile.txt

    but the output is missing the heading. Can someone advice?

    result:

    InputObject                     SideIndicator
    -----------                     -------------
    CHWDB01\FEGG  FEGG    FEGG_USER  EXECUTE  Buller.Trainer =>  

  • You could try the Export-CSV cmdlet if Out-File doesn't give the flexibility you need.

    John

  • Or just take the 1st (and optionally 2nd) line(s) from one source file.

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply