我有以下代码:
$HardCatUsers = Import-Csv z:\Scripts\Powershell\Hardcat_People_Export.csv
$ADUsers = Get-ADUser -Filter {enabled -eq $true -and surname -like "*" -and GivenName -like "*" -and Name -notlike "AD*" -and Name -notlike "LRTM*"} -SearchBase "ou=Production, dc=diti, dc=lr, dc=net" | Sort-Object Surname
foreach ($ADUser in $ADUsers)
{
if ($ADUser.Name.length -le 8)
{
$ADSearch = "$($ADUser.Surname), $($ADUser.GivenName) - $($ADUser.Name.ToUpper())"
if ($Code = $HardCatUsers | Where {$_.Description -eq $ADSearch})
{
#"$($ADSearch) $($Code)"
} else {
"$($ADSearch) NEW"
}
}
}
$ hardCatUsers对象只有两列数据,如:
Description Code
----------- ----
# No Assigned User - ACTIVE # ASSET MANAGEMENT
# No Assigned User - SPARE # NO USER SPARE
我正在做的是拉回AD用户的过滤列表并格式化三个属性,然后在$ hardcatusers中比较/查找此字符串,如果没有找到只输出字符串。
但是当前的实现在每次查找时似乎都非常慢,即5秒。有没有更优雅的方式来搜索列表中的匹配?
由于
答案 0 :(得分:0)
我发现使用$ hardcatusers的foreach循环比where
快得多即:
foreach ($ADUser in $ADUsers)
{
if ($ADUser.Name.length -le 8)
{
$found = $false
$ADSearch = "$($ADUser.DisplayName) - $($ADUser.Name.ToUpper())"
foreach ($HardcatUser in $HardCatUsers)
{
if ($HardcatUser.Description -eq $ADSearch)
{
$found = $true
}
}
if ($found -ne $true)
{
$Results = New-Object PSObject -Property (@{
Description = $ADSearch
Code = "PE" + $NextCode.ToString("000000")
})
$Result += $Results
$NextCode = $NextCode + 1
}
}
}