How can I get the result so the user email address that already listed in the AllowedSenderDomains is displayed in UserEmailDomainAlreadyRegistered array so I can output to OGV?
$UserEmailDomainAlreadyRegistered = # If the user email domain is found in the $AllowedSenderDomains, then display it in OGV
$AllowedSenders = @('User.Name@domain.com', 'User2.Name2@domain.net', 'User3.Name3@domain.net','unique.user@email.com')
$AllowedSenderDomains = @('domain.net', 'domain.com')
The expected result fo the above is:
'User.Name@domain.com'
'User2.Name2@domain.net'
'User3.Name3@domain.net'
Updated Code:
$policyName = '*'
$policy = Get-HostedContentFilterPolicy -Identity $PolicyName
$AllowedSenderDomains = $Policy.'AllowedSenderDomains' | Select-Object -ExpandProperty Domain -Unique
$AllowedSenders = $Policy.'AllowedSenders' | Select-Object Sender
$allowedDomains = '@({0})$' -f (($AllowedSenderDomains | ForEach-Object { [regex]::Escape($_) }) -join '|')
$AllowedSenders | Where-Object { $_ -match $allowedDomains } | OGV
$AllowedSenders | Where-Object { $AllowedSenderDomains -contains ($_ -split '@')[-1] } | OGV
Both of the OGV above shows empty content?
$AllowedSenderDomains
Domain1.com
Domain2.net
Domain3.org
$AllowedSenders
User1@domain1.com
User2@domain2.net
User3@Domain3.org
Just use the where-object
commandlet to filter out the domains you don't want:
# ~> $AllowedSenders | Where-Object { $_.Substring($_.LastIndexOf('@')+1) -in $AllowedSenderDomains}
User.Name@domain.com
User2.Name2@domain.net
User3.Name3@domain.net
Another approach can be to use regex, although with just two allowed domains this is probably overkill:
$AllowedSenderDomains = 'domain.net', 'domain.com'
$AllowedSenders = 'User.Name@domain.com', 'User2.Name2@domain.net', 'User3.Name3@domain.net','unique.user@email.com'
# create a regex from the allowed domains by joining them with OR (|)
# the regex is preceeded by the AT sign (@) and anchored to the end of the string with '$'
$allowedDomains = '@({0})$' -f (($AllowedSenderDomains | ForEach-Object { [regex]::Escape($_)}) -join '|')
# next, use -match to get only the emailaddresses that match the regex
$AllowedSenders | Where-Object { $_ -match $allowedDomains }
You could also use -split
as another alternative like this:
$AllowedSenders | Where-Object { $AllowedSenderDomains -contains ($_ -split '@')[-1] }
Result
User.Name@domain.com
User2.Name2@domain.net
User3.Name3@domain.net
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.