【问题标题】:GET Azure ADusers through powershell通过 powershell 获取 Azure ADusers
【发布时间】:2021-01-26 17:10:31
【问题描述】:

我能够在 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。

【问题讨论】:

  • 您好,请参考我下面提供的解决方案。如果它对您的问题有帮助,请accept它作为答案(单击我的答案旁边的复选标记,将其从灰色切换为已填充)。先谢谢了~
  • 如果还有什么问题,请告诉我。

标签: .net powershell active-directory azure-active-directory azure-powershell


【解决方案1】:

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

通过在 powershell 中运行以下命令来安装 AzureAD 模块:Install-Module AzureAD。可以参考这个document

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

然后就可以通过Get-AzureADUser命令获取AD用户了,这个命令只能响应每个用户的几个字段。如果您想要每个用户的更多字段,您可以运行Get-AzureADUser | fl

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-22
    • 1970-01-01
    • 2022-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-21
    相关资源
    最近更新 更多