0

I have been trying to re-format this command by making it cleaner but I just can't seem to get around the write-output.

Get-QARSOperation -ParentContainer 'somedomain.com/OU1/OU2' -TargetObjectType 'user' |
  Where-Object {$_.Status -eq 'Completed' -and $_.Controls.ID -eq 'OperationReason'} |
  ForEach-Object {Get-QARSApprovalTask -Operation $_.ID} |
  ForEach-Object {
    Write-OutPut ("Target: " + $_.Operation.TargetObjectInfo.DN.Replace("CN=","").Replace("cn=","").Replace("\","").Replace(",","").Replace("OU","").Split('=')[0]);
    Write-OutPut ("Operation ID: "+ $_.Operation.ID);
    Write-OutPut ("Approver: " + $_.CompletedBy.DN.Replace("CN=","").Replace("\","").Replace(",","").Replace("OU","").Split('=')[0]);
    Write-OutPut ("StartedOn: " + $_.Created);
    Write-OutPut ("Completed: " + $_.Completed);
    Write-OutPut ("Comments: " + $_.CompletionReason);
    Write-OutPut ("Operation Type: " + $_.Operation.Type);
    Write-OutPut "" 
  }

Also the format when I export to csv doesn't put the data into columns. What suggestions do you have to make this script look neater?

Thank you!

TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56
nfoman
  • 37
  • 1
  • 5
  • 1
    What, precisely, are you trying to accomplish? – Jeff Zeitlin Feb 27 '18 at 18:32
  • currently the output to csv is not in columns, so I was trying to re-arrange the write-out put commands but i may be over doing it. so the output for example looks like Target: some user Operation ID: 6-87378 Approver: some approver StartedOn: 02/05/2018 14:23:01 Completed: 02/06/2018 11:41:11 Comments: Approve due to term. Operation Type: Deprovision What id like is those Fields but as header items with the data beneath. Doh, for some reason the formatting is not working – nfoman Feb 27 '18 at 18:38
  • 5
    CSVs should not be generated using `Write-Output`; there is a separate `Export-CSV` cmdlet for that. I strongly suggest you look at that cmdlet, and also the `Select-Object` cmdlet, when contemplating CSV generation, especially from ActiveDirectory. – Jeff Zeitlin Feb 27 '18 at 18:42
  • Can't give that comment enough up votes – EBGreen Feb 27 '18 at 19:06

1 Answers1

1

As suggested in the comments the correct thing to do is use Export-Csv to generate a CSV file. As for creating an object that you want to export and making that easy to read in the code you could do something similar to what you have, and use it to create a custom object that could then be piped to Export-Csv. Also, I think your whole .Replace("CN=","").Replace("cn=","").Replace("\","").Replace(",","").Replace("OU","").Split('=')[0] can be simplified to .Split('=,')[1]. The string's .Split() method accepts multiple characters to split on, and it will split on any of the characters provided. Here's what I would suggest, you will need to update the path at the end, and may have to revert to your longer .Replace bit if mine doesn't work for you.

Get-QARSOperation -ParentContainer 'somedomain.com/OU1/OU2' -TargetObjectType 'user' |
    Where-Object {$_.Status -eq 'Completed' -and $_.Controls.ID -eq 'OperationReason'} |
    ForEach-Object {Get-QARSApprovalTask -Operation $_.ID} |
    ForEach-Object {
        [PSCustomObject][Ordered]@{
            "Target" = $_.Operation.TargetObjectInfo.DN.Split('=,')[1]
            "Operation ID" = $_.Operation.ID
            "Approver" = $_.CompletedBy.DN.Split('=,')[1]
            "StartedOn" = $_.Created
            "Completed" = $_.Completed
            "Comments" = $_.CompletionReason
            "Operation Type" = $_.Operation.Type
        }
    } |
    Export-Csv C:\Path\To\File.csv -NoTypeInformation

You could use a Select statement, but I think this looks cleaner for you.

TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56
  • Nicely done; note that you don't need the `[ordered]` before the hashtable that is cast to `[pscustomobject]` (though perhaps you added it for clarity of intent). As an aside re `.Split('=,')`: In PowerShell _Core_ you'd have to use `.Split([char[]] '=,')` (or `.Split('=,'.ToCharArray())` to ensure that the string is treated as individual chars. – mklement0 Feb 28 '18 at 02:44
  • @TheMadTechnician, This is great and pretty much what i wanted to achieve, thank you! One question though, I only get one row out puted to the csv, along with the header where as it should be a bunch more. I can see this if I remove the export-csv. Suggestions? – nfoman Mar 01 '18 at 14:47