本文介绍了使用C#中的Powershell cmdlet从AzureAD获取特定的用户详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够导入AzureAD模块并能够使用C#中的Connect-AzureAD PowerShell cmdlet连接AzureAD.

I am able to import AzureAD module and able to connect AzureAD using Connect-AzureAD PowerShell cmdlet in C#.

我要列出用户时,只需使用Get-AzureADUser即可获得所有用户.

When I want to list the users I just simply use the Get-AzureADUser got all users.

但是当我想使用Get-AzureADUser -SearchString '[email protected]'列出特定用户时,我无法列出.

But When I want to list the particular user using Get-AzureADUser -SearchString '[email protected]', I am not able to list.

尝试过一次:

    Runspace runspace = RunspaceFactory.CreateRunspace();
    runspace.Open();
    Pipeline pipeline = runspace.CreatePipeline();
    pipeline.Commands.AddScript("Import-Module AzureAD"); //importing

    //passing credentials
    pipeline.Commands.AddScript("$Username = '"+uname+"'");
    pipeline.Commands.AddScript("$Password = ConvertTo-SecureString '"+pwd+"'-AsPlainText -Force");
    pipeline.Commands.AddScript("$LiveCred = New-Object System.Management.Automation.PSCredential $Username, $Password");
    pipeline.Commands.AddScript("Connect-AzureAD -Credential $LiveCred");


    pipeline.Commands.AddScript("Get-AzureADUser -SearchString '[email protected]'");//needed one

    foreach(PSObject info in pipeline.Invoke())
    {
        Console.WriteLine(info.Members["DisplayName"].Value);//not working
    }

实际PowerShell cmdlet:

PS C:\Windows\system32> Get-AzureADUser -SearchString '[email protected]'

ObjectId                             DisplayName   UserPrincipalName                     UserType
--------                             -----------   -----------------                     --------
c36eb0fa-2500-4b3f-9499-1852e9ae44fc username [email protected] Member

如果列出所有用户,我的c#代码可以正常工作.但是对于特定用户,我的c#代码不起作用.我在哪里弄错了?

In case of listing all users, my c# code works fine. But In case of a particular user, my c# code doesn't work. Where I made a mistake?

对于列出所有用户://正常工作

For listing all users: // works fine

          pipeline.Commands.AddScript("Get-AzureADUser");
          var result = pipeline.Invoke();
            for(int i=0;i<result.Count;i++)
            {
                Console.Write(result[i].Members["DisplayName"].Value);
            }

推荐答案

由于您显然是在使用完整用户UPN进行搜索,因此我认为使用 ObjectID 开关甚至尝试使用过滤器选项.

Since you apparently are searching with the full user UPN, i think it would be better to use the ObjectID switch or even try the Filter option.

Get-AzureADUser -ObjectId '[email protected]'

在这里查看: https://docs.microsoft.com/zh-CN/powershell/module/azuread/get-azureaduser?view=azureadps-2.0

这篇关于使用C#中的Powershell cmdlet从AzureAD获取特定的用户详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 04:00