我正在撰寫一個 PowerShell 腳本,它需要檢查陣列 $arrayEmail 中的專案是否在陣列 $empListEmail 中,并將這些值扔到另一個陣列 C 中。陣列 A 有 9,500 多個專案,而陣列 B 沒有很多。令人驚訝的是,我還沒有看到執行此操作的示例。我已經在 Google 上搜索了兩天。這是我現在所擁有的,但比較根本不像它應該的那樣作業。
function MatchUsers {
$array = Get-Content -Raw -Path PassDataOut.json | ConvertFrom-Json
Import-Module ActiveDirectory # Load the Active Directory module
Set-Location AD: # Change into Active Directory
set-location "DC=graytv,DC=corp" # Sets the location to the Gray TV Corporate directory
$empList = Get-ADUser -filter 'Enabled -eq "False"' -searchbase "OU=domain Users,DC=graytv,DC=corp"
$arrayTemp = $array.Email
$arrayEmail = $arrayTemp.trim()
$empListEmail = $empList.UserPrincipalName
$NotInList = @($arrayEmail) -notin $empListEmail
Write-Host $NotInList
uj5u.com熱心網友回復:
當谷歌搜索時,您可能會得到一個選項Compare-Object
,但使用-notin
運算子也可以。問題來自試圖將整個串列與另一個串列進行比較。您必須遍歷內容以檢查串列:
$arrayEmail.Where{$_ -notin $empListEmail}
uj5u.com熱心網友回復:
將第二個串列變成一個HashSet<string>
- 搜索會比陣列快得多:
$empListEmail = [System.Collections.Generic.HashSet[string]]::new([string[]]$empList.UserPrincipalName, [StringComparer]::OrdinalIgnoreCase)
$NotInList = $arrayEmail |Where-Object { -not $empListEmail.Contains($_) }
uj5u.com熱心網友回復:
如果您不介意在其中使用獨特的電子郵件,$arrayEmail
這與Mathias 的答案類似,但顛倒了順序HashSet<T>
并使用它的過濾.ExceptWith
方法。
$arrayEmail = [System.Collections.Generic.HashSet[string]]::new(
[string[]] $array.Email.Trim(),
[StringComparer]::OrdinalIgnoreCase
)
$arrayEmail.ExceptWith([string[]] $empList.UserPrincipalName)
$arrayEmail # => updated to only values not in `$empList.UserPrincipalName`
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/530766.html