问题
I am able to import AzureAD module and able to connect AzureAD using Connect-AzureAD PowerShell cmdlet in C#.
When I want to list the users I just simply use the Get-AzureADUser got all users.
But When I want to list the particular user using Get-AzureADUser -SearchString 'user@domain.com', I am not able to list.
Tried one:
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
}
Actual 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
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);
}
回答1:
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'
Have a look here: https://docs.microsoft.com/en-us/powershell/module/azuread/get-azureaduser?view=azureadps-2.0
来源:https://stackoverflow.com/questions/51172767/getting-particular-user-details-from-azuread-using-powershell-cmdlets-in-c-sharp