foreach that finds if Service Account is a memberOf

Welcome Forums General PowerShell Q&A foreach that finds if Service Account is a memberOf

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
3 months, 2 weeks ago.

  • Author
    Posts
  • #105671

    Participant
    Points: 55
    Rank: Member

    I have started a script that finds all user accounts that are service accounts (their password never expire):

    $CorpSvcAccts = Get-ADUser -filter * -Properties PasswordNeverExpires |
        select name,PasswordNeverExpires,MemberOf | Where-Object {$_.PasswordNeverExpires -like "True"}
            
     foreach ($SvcAccts in $CorpSvcAccts)   {
        select memberOf -ExpandProperty memberOf $_. ??
    
     }

    ...but wish to THEN discover if they are part of a Group called ServiceAccts. I want to export those results. Maybe one column that shows the User account that are TRUE (have membership) and another column that shows FALSE.

    I basically want to see each of the user accounts that have pwd set to never expire and if they're in the Security Group or not, exported.

    Thank you

  • #105682

    Participant
    Points: 145
    Helping Hand
    Rank: Participant

    You like to make it harder than needed, don't you? 😉

    $DesiredGroup = Get-ADGroup -Identity 'Name of the Group' | Select-Object -ExpandProperty DistinguishedName
    Get-ADUser -Filter "PasswordNeverExpires -eq '$true'" -Properties PasswordNeverExpires,MemberOf |
        Select-Object -Property Name,PasswordNeverExpires,
        @{
            Name='GroupMember';
            Expression={If($_.MemberOf -contains $DesiredGroup){$True}Else{$false}}
        }
    • #105700

      Participant
      Points: 55
      Rank: Member

      Well, I certainly don't intend to but it may appear that way to you. It's a slow learning curve for me.

The topic ‘foreach that finds if Service Account is a memberOf’ is closed to new replies.