Show output when there are no results of if statement in foreach loop

Tagged: , ,

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Paul Johnston Paul Johnston 6 months, 3 weeks ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #36234
    Profile photo of Paul Johnston
    Paul Johnston
    Participant

    Hi Guys,

    I have the below code that I'm using to compare the SID attribute of a user from one domain to the SIDHistory attribute of users in another domain. If the SID matches then I know an account already exists for that user in the new domain. My problem is that if the account doesn't exist – i.e. nothing returns true in the below if statement – how do I show output to say "No account exists for this user. Moving on to next user". Hope that makes sense. Thanks!

    $OldDomainUserSIDs = Get-ADGroupMember -Identity 'OldDomainUsers' -Server OldDC.OldDomain.Com | select name, SID
    $NewDomainUsersSIDHistory = Get-ADUser -Filter * -Properties SIDHistory - Server newDC.NewDomain.Local | select name, SIDHistory
    
    foreach($OldDomainUserSID in $OldDomainUserSIDs){
        foreach($NewDomainUserSIDHistory in $NewDomainUserSIDHistory){
            if($OldDomainUserSID.SID -eq $NewDomainUserSIDHistory.SIDHistory){
                Write-Host "A user account already exists for $($OldDomainUserSID.Name) - Username is $($NewDomainUserSIDHistory.Name)"
            }
        }
    }
    
    #36236
    Profile photo of Jonathan Warnken
    Jonathan Warnken
    Participant

    A else statement added to your if statement that compares the SID will do that

    $OldDomainUserSIDs = Get-ADGroupMember -Identity 'OldDomainUsers' -Server OldDC.OldDomain.Com | select name, SID
    $NewDomainUsersSIDHistory = Get-ADUser -Filter * -Properties SIDHistory - Server newDC.NewDomain.Local | select name, SIDHistory
    
    foreach($OldDomainUserSID in $OldDomainUserSIDs){
        foreach($NewDomainUserSIDHistory in $NewDomainUserSIDHistory){
            if($OldDomainUserSID.SID -eq $NewDomainUserSIDHistory.SIDHistory){
                Write-Host "A user account already exists for $($OldDomainUserSID.Name) - Username is $($NewDomainUserSIDHistory.Name)"
            }else{Write-Host "No account exists for this user. Moving on to next user"}
        }
    }
    
    #36238
    Profile photo of Paul Johnston
    Paul Johnston
    Participant

    Hi Jonathan – I think that will tell me that the user's SID doesn't match the SIDHistory of the object currently being evaluated. What I need is some output if the user's SID doesn't match the SIDHistory attribute of any object.

    #36241
    Profile photo of Jeremy Murrah
    Jeremy Murrah
    Participant

    Try setting a "default" output message indicating nothing is found, then overwriting it if a user is found.

    foreach($OldDomainUserSID in $OldDomainUserSIDs){
        $output = "No Account exists for this users. Moving on to next user"
        foreach($NewDomainUserSIDHistory in $NewDomainUserSIDHistory){
            if($OldDomainUserSID.SID -eq $NewDomainUserSIDHistory.SIDHistory){
                $output =  "A user account already exists for $($OldDomainUserSID.Name) - Username is $($NewDomainUserSIDHistory.Name)"
            }
        }
        Write-Output $output
    }
    
    #36247
    Profile photo of Paul Johnston
    Paul Johnston
    Participant

    Simple as that – excellent! Thanks for the help, Jeremy.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.