locked
Question about Output Powershell RRS feed

  • Question

  • Hi all,

    I have created a PS script. The script works good, but i need a little more output, can anyone help me.

    • i need to get a output of the ou name, groups in it and there users

    i got these 2 scripts:

    script 1 give the Ou name and script 2 give the groups and there users.

    1. Get-ADOrganizationalUnit -LDAPFilter '(name=*)' -SearchBase "ou=AA,dc=BBB,dc=CC" -SearchScope OneLevel| ft Name
    
    2. Get-ADgroup -Filter 'GroupCategory -eq "Security" -and GroupScope -eq "DomainLocal"' -SearchBase "ou=AA,dc=BBB,dc=CC" -Properties members,description,groupscope |
      select name,samaccountname,groupscope,GroupCategory,@{Name="Members"; Expression ={(($_.Members).split(",") | where-object {$_.contains("CN=")}).replace("CN=","")-join ','}},description |
      Sort-Object -Property Name | Export-csv -path C:\temp\TestGroups.csv -NoTypeInformation

    The output is a csv file with the groupname, samaccountname,groupscope, groupcategory and members.

    I also want a column with the OU name (from script 1) where the groups belongs to.

    anyone an idea how i can join these 2 scripts to get it works?

    Regards,

    Wednesday, March 25, 2020 10:56 AM

Answers

  • I think you're overcomplicating this. You already have the OU name in the distinguished name of each group. This should be enough:
    Get-ADgroup -Filter 'GroupCategory -eq "Security" -and GroupScope -eq "DomainLocal"' -SearchBase "ou=AA,dc=BBB,dc=CC" -Properties members,description,groupscope |
      Select-Object -Property  name,samaccountname,groupscope,GroupCategory,
        @{Name="Members"; Expression ={(($_.Members).split(",") |   where-object {$_.contains("CN=")}).replace("CN=","")-join ','}},
        @{Name='OU';Expression = {($_.DistinguishedName.split(',')[1]).split('=')[1]}},
        description |
            Sort-Object -Property Name | 
            Export-csv -path C:\temp\TestGroups.csv -NoTypeInformation


    Live long and prosper!

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

    • Marked as answer by rickje35 Wednesday, March 25, 2020 1:23 PM
    • Edited by BOfH-666 Wednesday, March 25, 2020 1:53 PM
    Wednesday, March 25, 2020 12:05 PM

All replies

  • I think you're overcomplicating this. You already have the OU name in the distinguished name of each group. This should be enough:
    Get-ADgroup -Filter 'GroupCategory -eq "Security" -and GroupScope -eq "DomainLocal"' -SearchBase "ou=AA,dc=BBB,dc=CC" -Properties members,description,groupscope |
      Select-Object -Property  name,samaccountname,groupscope,GroupCategory,
        @{Name="Members"; Expression ={(($_.Members).split(",") |   where-object {$_.contains("CN=")}).replace("CN=","")-join ','}},
        @{Name='OU';Expression = {($_.DistinguishedName.split(',')[1]).split('=')[1]}},
        description |
            Sort-Object -Property Name | 
            Export-csv -path C:\temp\TestGroups.csv -NoTypeInformation


    Live long and prosper!

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

    • Marked as answer by rickje35 Wednesday, March 25, 2020 1:23 PM
    • Edited by BOfH-666 Wednesday, March 25, 2020 1:53 PM
    Wednesday, March 25, 2020 12:05 PM
  • Thank you very much this did the trick for me. 

    Only there should be a comma before description.

    Wednesday, March 25, 2020 1:26 PM
  • Only there should be a comma before description.

    Good catch ... fixed in my original post. Thanks

    Live long and prosper!

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

    Wednesday, March 25, 2020 1:52 PM