使用C#中的Powershell cmdlet从AzureAD获取特定的用户详细信息 [英] Getting particular user details from AzureAD using Powershell cmdlets in C#

查看:96
本文介绍了使用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 'user@domain.com'列出特定用户时,我无法列出.

But When I want to list the particular user using Get-AzureADUser -SearchString 'user@domain.com', 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 'user@domain.com'");//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 'user@domain.com'

ObjectId                             DisplayName   UserPrincipalName                     UserType
--------                             -----------   -----------------                     --------
c36eb0fa-2500-4b3f-9499-1852e9ae44fc username user@domain.com 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 'user@domain.com'

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆