如何在powershell中连接多个地址

how to concatenate multiple address in powershell

我有一个用户列表,但我不知道每个用户的电子邮件域,所以我只是想知道如何将 $alldomans 中的所有域一一连接起来并尝试找到他们的 employeeId.

例如,我现在就是这样做的,这是可行的,但我会有多个电子邮件域,所以我只是想知道如何从我的列表中逐一传递域并尝试查找他们的员工 ID?

 $EmployeeId=  $policy.name | foreach { (Get-AzureADUser -ObjectId "$($_)@gmail.com").ExtensionProperty.employeeId}

我已经试过了,但由于某种原因仍然无法正常工作

$allDomains = @(
   "gmail.com",
   "yahoo.com",
   "outlook.com"
   "hotmail.com"
)

 $EmployeeId=  $policy.name | foreach { (Get-AzureADUser -ObjectId "$($_)@$allDomains").ExtensionProperty.employeeId}

您需要域的内部循环:

$allDomains = @(
   "gmail.com",
   "yahoo.com",
   "outlook.com"
   "hotmail.com"
)

$EmployeeId = $policy.name | ForEach-Object {
    foreach($domain in $allDomains) {
        try {
            $azUsr = Get-AzureADUser -ObjectId "$_@$domain" -ErrorAction Stop
            # using `return` here so that if `$azUsr` could be found,
            # we stop this inner loop and go to the next user
            return $azUsr.ExtensionProperty.employeeId
        }
        catch {
            Write-Warning $_.Exception.Message
        }
    }
}