【问题标题】:Retrieving users from Azure AD with Powershell and a csv file使用 Powershell 和 csv 文件从 Azure AD 检索用户
【发布时间】:2021-10-04 05:50:45
【问题描述】:

我想从 azure 获取存储在 csv 文件中的用户

这是我迄今为止尝试过的,但我没有得到任何结果,也没有错误

Connect-AzureAD
$usercsv= Import-Csv -Path C:\Victor\csv.csv -Delimiter "," | Select-Object userPrincipalName
if ($usercsv){
    foreach($user in $usercsv){
        try{
            Get-AzureADUser -All $True | Where-Object {$_.userPrincipalName -like $user}
            return;
            }
        catch { 
            Write-Output "error"
            Write-Error $_.Exception.Message
            }
    }

}
else{
"No user data stored"
}

【问题讨论】:

    标签: azure powershell azure-active-directory powershell-3.0


    【解决方案1】:

    如果 CSV 确实有 UserPrincipalNames,这应该会给你至少一些预期的结果:

    Connect-AzureAD
    $usercsv = Import-Csv -Path C:\Victor\csv.csv #  -Delimiter "," > Is default for this cmdlet
    foreach($user in $usercsv)
    {
        $upn = $user.UserPrincipalName.Trim()
        $azUser = Get-AzureADUser -Filter "userPrincipalName eq '$upn'"
    
        if(-not $azUser)
        {
            Write-Warning "$upn could not be found in AzureAD"
            continue
        }
    
        $azUser
    }
    

    【讨论】:

    • 我在这里遇到错误$azUser = Get-AzureADUser -Filter "userPrincipalName eq '$upn'"
    • @victorsionado 很高兴知道,是的,当我们使用-Filter 时,如果它找不到用户而不是给你一个错误,它不应该返回任何东西,所以这很奇怪。它可能返回错误的唯一方法是使用空值,例如:Get-AzureADUser -Filter "userPrincipalName eq '$null'"
    猜你喜欢
    • 1970-01-01
    • 2018-05-14
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    • 2021-12-23
    • 2020-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多