none
Need help with replacing text in an array RRS feed

  • Question

  • I have to export an OU and all of it's sub OUs to a dev domain, so I have the below code.  There are a lot of Sub OUs, so I'm trying to replace the DistinguishedName values of the source domain to the dev domain.

    $OldStrOU = Get-ADOrganizationalUnit -Filter * -SearchBase 'OU=ASDF,DC=Global,DC=Company,DC=Com' | Select-Object name,DistinguishedName,@{n='OUPath';e={$_.distinguishedName -replace '^.+?,(CN|OU.+)','$1'}}
    $NewStrOU = foreach ($OU in $OldStrOU){
        $OU.DistinguishedName -replace 'DC=Global,DC=Company,DC=Com','DC=Global,DC=betaCompany,DC=Com'
    }
    $NewStrOU | Export-Csv OUTree.csv

    However, the resulting array I end up with is missing headers and only contains data associated with the DistinguishedName.  All data related to the Name attribute is completely missing.

    And also, when I export to CSV, the result is just a length attribute with numbers, I guess I'm assuming those are the lengths of the strings in the DistinguishedName attribute.  However, I don't care about the length, so I'm not even sure how that got in there.

    Anyone able to help me find a solution that keeps my Name attribute and DistinguishedName attribute column headers?

    Tuesday, February 9, 2016 7:54 PM

Answers

  • Get-ADOrganizationalUnit -Filter * -SearchBase 'OU=ASDF,DC=Global,DC=Company,DC=Com' | 
        Select-Object Name,@{N='DistinguishedName';E={$_.DistinguishedName.Replace('DC=Global,DC=Company,DC=Com','DC=Global,DC=BetaCompany,DC=Com')}},@{N='OUPath';E={($_.distinguishedName -replace '^.+?,(CN|OU.+)','$1').Replace('DC=Global,DC=Company,DC=Com','DC=Global,DC=BetaCompany,DC=Com')}} |
            Export-Csv .\ouTree.csv -NoTypeInformation


    EDIT: Ah, slow on submit. See above.

    Tuesday, February 9, 2016 8:04 PM

All replies

  • Get-ADOrganizationalUnit -Filter * -SearchBase 'OU=ASDF,DC=Global,DC=Company,DC=Com' |
    	ForEach-Object{
    		[pccustomobject]@{
    			OU=($_.DistinguishedName -replace 'DC=Global,DC=Company,DC=Com', 'DC=Global,DC=betaCompany,DC=Com')
    	    }
    	} | Export-Csv OUTree.csv -NoType


    \_(ツ)_/


    • Edited by jrv Tuesday, February 9, 2016 8:01 PM
    Tuesday, February 9, 2016 8:01 PM
  • Get-ADOrganizationalUnit -Filter * -SearchBase 'OU=ASDF,DC=Global,DC=Company,DC=Com' | 
        Select-Object Name,@{N='DistinguishedName';E={$_.DistinguishedName.Replace('DC=Global,DC=Company,DC=Com','DC=Global,DC=BetaCompany,DC=Com')}},@{N='OUPath';E={($_.distinguishedName -replace '^.+?,(CN|OU.+)','$1').Replace('DC=Global,DC=Company,DC=Com','DC=Global,DC=BetaCompany,DC=Com')}} |
            Export-Csv .\ouTree.csv -NoTypeInformation


    EDIT: Ah, slow on submit. See above.

    Tuesday, February 9, 2016 8:04 PM
  • Thank you for the reply both of you.  I haven't tested it out yet, but I'm sure it works. Now I'm curious why both of you included the switch -NoTypeInformation. In my education, I was taught that you do want the type information unless you're providing it for some kind of report to someone. Because the Type Information is helpful for when that data has to get consumed again, it helps for the powershell engine to know what that type information is.  Is that off base?
    Tuesday, February 9, 2016 8:22 PM
  • In reality it serves no purpose and will prevent loading into Excel or other tools.

    \_(ツ)_/

    Tuesday, February 9, 2016 8:38 PM
  • Ditto to what jrv has already said. I always use -NoTypeInformation, I'd prefer if it was on by default.

    Tuesday, February 9, 2016 8:54 PM
  • Mike yours worked. Jrv, for some reason I got an error.

    Unable to find type [pccustomobject]

    Tuesday, February 9, 2016 9:59 PM