locked
Sending results to a Txt or CSV file RRS feed

  • Question

  • Ran the below Script, not having much luck in getting all the output seen on the Screen into a File.  Any help, greatly appreciated.  Thanks.

    $OUS = Get-ADOrganizationalUnit -Filter {name -like "*"} | select -expandproperty distinguishedname
    foreach ($OU in $OUS){
    $counter = 0
    $gpos = Get-GPInheritance -target "$OU" |select -ExpandProperty inheritedgpolinks
    foreach ($gpo in $gpos){
    $counter++ 
    $gponame = $gpo.displayname
    "$counter","$gponame","$OU"
    }
    }

    Wednesday, February 27, 2019 9:14 PM

Answers

  • OK, I may see the problem now. I thought the output from your script was 3 comma delimited strings, each quoted. The $OU must be quoted in a CSV, and maybe $gponame as well. But when I tested the values were not quoted. The line in the script that outputs the values should be.

    """$counter"",""$gponame"",""$OU"""

    The entire string is quoted, but within the string the embedded quotes are doubled. The two embedded quote characters will resolve into one in the quoted string. The string will output on one line with the 3 values each quoted.


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    • Proposed as answer by jrv Friday, March 1, 2019 12:36 AM
    • Marked as answer by Richard MuellerMVP Wednesday, March 6, 2019 11:36 AM
    Wednesday, February 27, 2019 10:30 PM
  • To avoid quoting issues:

    '"{0}","{1}","{2}"' -f $counter,$gponame,$OU


    \_(ツ)_/

    Thursday, February 28, 2019 2:24 AM

All replies

  • For scripts like that I redirect the output to a file. In this case it appears to be comma delimited, so it can be a CSV file. Save the script in a file with *.ps1 extension, say GetOUs.ps1, then at a PowerShell prompt, in the folder where the *.ps1 is saved, run the following.

    .\GetOUs.ps1 > .\Report.csv

    The ">" character redirects all output to the specified file in the same folder. In this case, in the file Report.csv.


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)


    Wednesday, February 27, 2019 9:32 PM
  • OK, I may see the problem now. I thought the output from your script was 3 comma delimited strings, each quoted. The $OU must be quoted in a CSV, and maybe $gponame as well. But when I tested the values were not quoted. The line in the script that outputs the values should be.

    """$counter"",""$gponame"",""$OU"""

    The entire string is quoted, but within the string the embedded quotes are doubled. The two embedded quote characters will resolve into one in the quoted string. The string will output on one line with the 3 values each quoted.


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    • Proposed as answer by jrv Friday, March 1, 2019 12:36 AM
    • Marked as answer by Richard MuellerMVP Wednesday, March 6, 2019 11:36 AM
    Wednesday, February 27, 2019 10:30 PM
  • Looks like a scripting question.  Can I move it to the scripting forum? 

    Santhosh Sivarajan | Houston, TX | www.sivarajan.com
    ITIL,MCITP,MCTS,MCSE (W2K3/W2K/NT4),MCSA(W2K3/W2K/MSG),Network+,CCNA

    My Books: | Windows Server Security | Windows Server 2012

    Blogs | Twitter | LinkedIn | Facebook|

    This posting is provided AS IS with no warranties, and confers no rights.

    Wednesday, February 27, 2019 11:39 PM
  • To avoid quoting issues:

    '"{0}","{1}","{2}"' -f $counter,$gponame,$OU


    \_(ツ)_/

    Thursday, February 28, 2019 2:24 AM
  • This is the complete script for you. 

    I tested and it works. 

    Let me know if you have any questions

    ~John

    <# FilePrep
     Change this to reflect where you want your working files at.
    #>
    $dir = "c:\temp\OU"
    <#Directory Creation
    Once the script is used it populates a temp directory. 
    This deletes the entries from that directory
    and creates a fresh directory for each subsequent use.
    #>
            if ((test-path $dir))
           {
    Remove-Item $dir -Force -Recurse   
           }   
    else {
    New-Item -ItemType Directory -Force -path $dir
    New-Item -Path $dir -name OUInfo.csv -ItemType file
    Add-content -path $dir\OUInfo.csv -Value "Counter,GPOName,OU"
    $OUs = Get-ADOrganizationalUnit -Filter * | select -expandproperty distinguishedname
    foreach ($OU in $OUS){
    $counter = 0
    $gpos = Get-GPInheritance -target "$OU" |select -ExpandProperty inheritedgpolinks
    foreach ($gpo in $gpos){
    $counter++ 
    $gponame = $gpo.displayname
    Add-content -Path $dir\OUInfo.csv """$counter"",""$gponame"",""$OU"""
    }
    }
    }
    
    <#
    Exported CSV file will be in the following format:
    Counter,GPOName,OU
    "1","PolicyName","OU=Domain Controllers,DC=DC,DC=local"
    "2","PolicyName","OU=Domain Controllers,DC=DC,DC=local"
    "3","PolicyName","OU=Domain Controllers,DC=DC,DC=local"
    #>


    • Edited by johnbljr Friday, March 1, 2019 12:35 AM
    Friday, March 1, 2019 12:33 AM
  • Here is how to this kind of gathering in PowerShell:

    Get-ADOrganizationalUnit -Filter *  -PipelineVariable ou|
        ForEach-Object{
            $counter = 0
            Get-GPInheritance -target $_.DistinguishedName |
                ForEach-Object{
                    $counter++
                    [pscustomobject]@{
                        Counter = $counter
                        GPOName = $_.displayname
                        OUPath = $ou.DistinguishedName
                    }
            }
        } |
        Export-Csv gpobyou.csv

    You can change the export to generate any format output you need.  This technique eliminates long, drawn out, spaghetti code methods.


    \_(ツ)_/


    • Edited by jrv Friday, March 1, 2019 12:45 AM
    Friday, March 1, 2019 12:44 AM