Get "something" as a result in second column

This topic contains 5 replies, has 3 voices, and was last updated by  Adam Bertram 1 week, 4 days ago.

  • Author
    Posts
  • #84838

    Radu Dinut
    Participant

    Hello,
    I've just started using PS (I know...) and already stuck. Need your help with some user list I'm trying to get.

    I have a list of users, with each user member of different AD groups – Marketing, HR, Printing, etc. A certain "type" of group I'm interested in starts with the same words, so I can filter by that. BUT, some of the users in that list aren't part of any of these groups. Like so:
    Users Special Group
    U1 Spe.Grp.1
    U2 Spe.Grp.2
    U3 Spe.Grp.1
    U4 Spe.Grp.3
    U5 Not in any group that matches the filter
    U6 Spe.Grp.Test.1
    U7 Not in any group that matches the filter

    The problem is all the special groups are randomly named (after the Spe.Grp.*), all the users are spread inside AD in different OU's and there are around 300 users I need this for.

    I have this so far:

    Import-Csv .\file.txt |% {Get-aduser $_.ppl |select samaccountname } |% {$smth=$_.samaccountname;Write-Output $_;} |% {Get-ADPrincipalGroupMembership $_.samaccountname |? {$_.name -like "Spe.Grp.*"}} |Select-Object @{n='samaccountname';e={$smth}},Name > C:\mm-u-g.txt

    The result is ok, except that for the users U5 and U7 it skips them entirely and I need the output with all the rows. For U5 and U7 I'd like it to say "User doesn't belong to any Spe.Grp."

    SamAcctN Name
    U1 Spe.Grp.1
    U2 Spe.Grp.2
    U3 Spe.Grp.1
    U4 Spe.Grp.3
    U5 User doesn't belong to any Spe.Grp.
    U6 Spe.Grp.Test.1
    U7 User doesn't belong to any Spe.Grp.

    Also, an error to be put in when a User isn't found would be nice. But that's for another post perhaps 🙂

    Thank you,
    Radu

  • #84841

    postanote
    Participant

    This is a question similar to one recently posted that I responded to.
    See this thread....

    Show User and AD group membership
    'powershell.org/forums/topic/get-adgroup-with-poershell-want-to-displayexport-output-one-user-per-line'

    ... to determine if you can leverage what I posted to get you to where you need to go.

  • #84856

    Radu Dinut
    Participant

    ... to determine if you can leverage what I posted to get you to where you need to go.

    It's EXACTLY what I needed.
    Thank you so much.

    Radu

    • #84857

      Radu Dinut
      Participant

      This is what it ended up like:

      Import-Csv .\file.txt |% {Get-aduser $_.ppl -Properties Name, MemberOf |Select-Object Name, @{Label="Memberof";expression={($_.memberof | Get-ADGroup |? {$_.name -like "Spe.Grp.*"}| Select-Object -ExpandProperty Name) -Join ","}}} > C:\mm-final.txt

      It looks awesome and it does just what I need.

      Thanks again <3

      Radu

  • #84913

    postanote
    Participant

    No worries

  • #85211

    Adam Bertram
    Moderator

    Moving to correct forum.

You must be logged in to reply to this topic.