I am trying to compare members is a list with the following script:
$Guid = "59041b96-c71d-436c-8297-7af5fcf4e22a"
$Members = Get-RetentionCompliancePolicy -identity $guid -DistributionDetail | Select -ExpandProperty OneDriveLocation | select name,displayname | sort displayname
$User = "Humbert, Jason"
$ODPolicy = $members.displayname | Select-String -Pattern "Humbert, Jason"
if($User -like $ODPolicy){
Write-host "USer $($USer.primarysmtpaddress) is in"
#$OD.RetentionSet = $True
}Else{
Write-Warning "User $($USer.primarysmtpaddress) not in Policy"}
The output for $ODPolicy returns 2 names because there are two mailboxes (Active, and inactive)
PS C:\Users\XYZ> $ODPolicy
Humbert, Jason Humbert, Jason
When running the if($User -like $ODPolicy)
the script doesn't seem to be able to handle that there are two objects that are returned and it returns the Else{ Write-Warning "User $($USer.primarysmtpaddress) not in Policy"}
How can I modify this so that the if($User -like $ODPolicy)
sees it as true and returns the Write-host "USer $($USer.primarysmtpaddress)
is in"?
CodePudding user response:
The -like
operator is not useful here because you want an exact match and you want to verify if a collection of items $members
contains a specific single item $User
.
So you should switch to the -contains
operator, e.g.:
if ($members.displayname -contains "Humbert, Jason"){}