locked
cleaning the output on pscustomo using regex RRS feed

  • Question

  • I have a script that get the members of an AD group, im quite new in using PScustomobject and when calling the members property it displays the account's full Distinguished Name. SO i use regex (^CN=\\|,.*S) to replace it. Some or the accounts got cleaned up and only shows userid however other accounts still have the prefix CN= 

    is my regex wrong?

    $path = Get-Content "<path>\file.txt"
    $server = "servername"
    $arr_main = @()
    $arr_ad_grp = @()
    
    
    foreach ($line in $path){
        $arr_ad_grp = Get-ADGroup -Identity $line -Server $server -Properties * 
        foreach ($dl_name in $arr_ad_grp){
                $ht = [PSCustomObject]@{
                        Name = $dl_name.SamAccountName
                        Owner = $dl_name.ManagedBy -replace '^CN=\\|,.*$'
                        Members = $dl_name.Members  -replace '^CN=\\|,.*$' -join ';' #-replace ';' , '`n'
                        
                        
                } 
        
        $arr_main += $ht
        }
    }
    
    $arr_main | ft -Wrap


    PoSH newbie, BaSH Oldie

    Tuesday, October 8, 2019 10:19 AM

Answers

  • You are allowed to do your own research and use a search engine from time to time. ;-)

    VOID has the same effect like piping something to Out-Null ... it suppresses the output ... in this case the output of the -match operator since we do not want to have the $true's - we just want the $Matches.


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Edited by BOfH-666 Wednesday, October 9, 2019 7:46 AM
    • Marked as answer by navarro_aries Thursday, October 10, 2019 4:26 AM
    Wednesday, October 9, 2019 7:45 AM

All replies

  • Something like this could be a good starting point for you:
    $GroupList = Get-Content '<path>\file.txt'
    $server = 'servername'
    $Result = foreach ($Group in $GroupList) {
        $GroupInfo = Get-ADGroup -Identity $Group -Properties ManagedBy,Members -Server $server
        [PSCustomObject]@{
            Name = $GroupInfo.sAMAccountName
            Owner = $([VOID]( $GroupInfo.managedby  -match '(?<=CN=).+?(?=,)' ); $Matches.Values)
            Members = Foreach ($Member in  $TestGRP.Members ){ [VOID]($Member -match '(?<=CN=).+?(?=,)');$Matches.Values}
        }
    }
    $Result

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Tuesday, October 8, 2019 12:22 PM
  • wow totally looks alien to me, but still thanks for this . WOuld you mind breaking down that line below. First i didnt know i can do a Foreach on an object, what does VOID do? andbasically got totally confused the line.

    Members = Foreach ($Member in  $TestGRP.Members ){ [VOID]($Member -match '(?<=CN=).+?(?=,)');$Matches.Values}


    PoSH newbie, BaSH Oldie

    Wednesday, October 9, 2019 4:36 AM
  • You are allowed to do your own research and use a search engine from time to time. ;-)

    VOID has the same effect like piping something to Out-Null ... it suppresses the output ... in this case the output of the -match operator since we do not want to have the $true's - we just want the $Matches.


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Edited by BOfH-666 Wednesday, October 9, 2019 7:46 AM
    • Marked as answer by navarro_aries Thursday, October 10, 2019 4:26 AM
    Wednesday, October 9, 2019 7:45 AM
  • Hi,

    Thanks for your question.

    $path = Get-Content "<path>\file.txt"
    $server = "servername"
    $arr_main = @()
    $arr_ad_grp = @()
    foreach ($line in $path){
    $arr_ad_grp = Get-ADGroup -Identity administrators  -Properties * 
        foreach ($dl_name in $arr_ad_grp)
        {
                $ht = [PSCustomObject]@{
                        Name = $dl_name.SamAccountName
                        Owner = $dl_name.ManagedBy | %{$_ -replace '^CN=|,.*$'}
                        Members = $dl_name.Members | %{ $_ -replace '^CN=|,.*$' } 
                         } 
                          $arr_main += $ht
        }
     }
     $arr_main | ft -Wrap

    Best regards.

    Lee


    Just do it.

    Wednesday, October 9, 2019 8:54 AM