![](/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.