![](/img/trans.png)
[英]Is there a way to use more elements than display in an Out-GridView array
[英]Comparing array of strings and display the result when matched to Out-GridView?
如何获得结果,以便已在 AllowedSenderDomains 中列出的用户电子邮件地址显示在 UserEmailDomainAlreadyRegistered 数组中,以便我可以输出到 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')
上面的预期结果是:
'User.Name@domain.com'
'User2.Name2@domain.net'
'User3.Name3@domain.net'
更新代码:
$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
上面两个OGV都显示空内容?
$AllowedSenderDomains
Domain1.com
Domain2.net
Domain3.org
$AllowedSenders
User1@domain1.com
User2@domain2.net
User3@Domain3.org
只需使用where-object
commandlet 过滤掉您不想要的域:
# ~> $AllowedSenders | Where-Object { $_.Substring($_.LastIndexOf('@')+1) -in $AllowedSenderDomains}
User.Name@domain.com
User2.Name2@domain.net
User3.Name3@domain.net
另一种方法是使用正则表达式,尽管只有两个允许的域,这可能是矫枉过正:
$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 }
您也可以使用-split
作为另一种选择,如下所示:
$AllowedSenders | Where-Object { $AllowedSenderDomains -contains ($_ -split '@')[-1] }
结果
User.Name@domain.com
User2.Name2@domain.net
User3.Name3@domain.net
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.