通过Powershell获取Azure ADusers

问题描述

我能够在C#控制台应用程序中吸引AD用户,但是我无法使Powershell具有AD用户。 下面是C#代码

string responseString = null;
using (var client=new HttpClient())
{
    var data = new Dictionary<string,string>
    {
        { "grant_type","password" },{ "client_id","client_id" },{ "client_secret","client_secret" },{ "scope","https://graph.microsoft.com/.default" },{ "userName",Your username here },{ "password",Your password here }
    };
    var content = new FormUrlEncodedContent(data);
    var response = await client.PostAsync("https://login.microsoftonline.com/id/oauth2/v2.0/token",content);
    responseString = await response.Content.ReadAsstringAsync();
}
var token= JsonSerializer.Deserialize<TokenClass>(responseString);
            
var graphapistring = "https://graph.microsoft.com/v1.0/users/";
using (var client1 = new HttpClient())
{
    client1.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer",token.access_token);

    var response = await client1.GetAsync(graphapistring);
    responseString = await response.Content.ReadAsstringAsync();
}
var user= JsonSerializer.Deserialize<userdetails>(responseString);

我需要帮助将上面的代码转换为powershell。

解决方法

根据您的要求,您只需要在Powershell中使用 AzureAD 模块即可。

通过在Powershell Install-Module AzureAD中运行以下命令,

安装 AzureAD 模块。您可以参考此document

安装模块后,我们需要先连接到azure广告。运行Connect-AzureAD命令进行连接。

然后,您可以通过运行Get-AzureADUser命令来获得AD用户,此命令仅可以响应每个用户的几个字段。如果需要每个用户的更多字段,则可以运行Get-AzureADUser | fl