powershell export csv localy of remote command

matteu31 502 Reputation points
2020-12-10T09:51:03.87+00:00

Hello,

I would like to launch this command on all domain controller on my domain :
auditpol /backup /file:c:\temp\auditpol.csv

If I launch it locally, it's ok. My csv file is fine.

46883-2020-12-10-10h58-18.png

I try this for remote computers:

foreach ($DC in Get-ADDomainController -Filter * -Server "server1")  
    {  
        invoke-command -computername $DC.hostname -scriptblock {auditpol /backup /file:c:\temp\auditpol.csv} | export-csv c:\temp\test.csv  
    }  

Result is not what I would like....
It's just :

46854-2020-12-10-10h57-20.png

I try to find some explanations on internet but didn't find any.

Thank you for your help.

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Anonymous
    2020-12-10T13:28:34.887+00:00

    Hi,

    The auditpol /backup doesn't output the contents of the csv file. You need to get the contents first.

    foreach ($DC in Get-ADDomainController -Filter * -Server "server1"){  
        Invoke-Command -ComputerName $DC.hostname -ScriptBlock {auditpol /backup /file:c:\temp\auditpol.csv | Out-Null; Get-Content C:\temp\auditpol.csv} |   
        ConvertFrom-Csv | Export-Csv c:\temp\test.csv  
    }  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. matteu31 502 Reputation points
    2020-12-10T13:51:32.58+00:00

    Thank you for your answer.
    It solve 1 issue :p

    I have 2 more issue now :)

    1)
    In my CSV file, it's french language and special character are not correctly writtent.

    "Machine Name","Policy Target","Subcategory","Subcategory GUID","Inclusion Setting","Exclusion Setting","Setting Value"
    "AUDITAD1","System","Autres ?v?nements syst?me","{0CCE9214-69AE-11D9-BED3-505054503030}","Succ?s et ?chec","","3"
    

    There are lot of ? instead of é or è.

    On original file (auditpol.csv generated on all server, encoding is ok).

    => Issue solved by adding -encoding UTF-8 at export-csv commandlet.

    2)
    If I just execute it, I will have all server with c:\temp\test.csv file.
    I need to delete it on all server.
    Is it the best solution ?

     foreach ($DC in Get-ADDomainController -Filter * -Server "Server1"){
         Invoke-Command -ComputerName $DC.hostname -ScriptBlock {auditpol /backup /file:c:\temp\auditpol.csv | Out-Null; Get-Content C:\temp\auditpol.csv} | 
         ConvertFrom-Csv | Export-Csv "c:\temp\auditpol_$($dc.hostname).csv" -Encoding UTF8 -NoTypeInformation
         Remove-Item -Path "\\$($DC.hostname)\c$\temp\auditpol.csv"
     }
    

    I'm learning powershell since sometimes but It's not my main job. Could you be more precise about the convertfrom-csv please ?
    What I understand :
    1 : We execute the command on remote server and pipe on out-null to not have result on screen.
    2 : We read the content of the new generated file and build an object with it with convertfrom-CSV function
    3: Once object is built, we can export it as CSV on local computer with correct encoding option.

    Am I right ?

    => Maybe it's better to just do this :
    Execute auditpol command remotely
    Copy generated file localy with copy-item
    Delete remote file

    no ?


  3. matteu31 502 Reputation points
    2020-12-11T22:59:38.753+00:00

    what do you think ?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.