question

PrashuDA avatar image
0 Votes"
PrashuDA asked KonstantinTchoumak-8488 commented

Calling Azure Powershell commands from C#

I am unable to successfully execute following piece of code. Whenever I try to call Azure commands through C#, it threw an exception ""The term 'Connect-AzureADUser' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.". Is there a way to execute Azure powershell commands through C# ?

In addition to it, I should be able to see the output in the results variable.

PSDataCollection<PSObject> myOutPut = new PSDataCollection<PSObject>();

             InitialSessionState initialState = InitialSessionState.CreateDefault();
             initialState.ExecutionPolicy = Microsoft.PowerShell.ExecutionPolicy.Bypass;
             initialState.AuthorizationManager = new System.Management.Automation.AuthorizationManager("O365");
             initialState.LanguageMode = System.Management.Automation.PSLanguageMode.FullLanguage;
             initialState.ImportPSModule(new string[] { "AzureADPreview" });
             Runspace runspace = RunspaceFactory.CreateRunspace(initialState);

                
             runspace.Open();
             Pipeline pipeline = runspace.CreatePipeline();
             
             pipeline.Commands.AddScript("Connect-AzureAD");
              pipeline.Commands.AddScript("Get-AzureADUser");
              
             var result = pipeline.Invoke();
azure-active-directory
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello, PrashuDA. How are you?

I am making an application to study with this same resource of invoking azure and msol in powershell through C #. Could you share your application / source code so I can see it? It will help me a lot. Hugs and see you soon!

0 Votes 0 ·
MarileeTurscak-MSFT avatar image
0 Votes"
MarileeTurscak-MSFT answered KonstantinTchoumak-8488 commented

This isn't really Azure Active Directory related (as it is tagged), but my suspicion is that you are missing the right assembly reference since you appear to be calling the AddScript() and AddCommand() methods correctly. You will need the latest Nuget package not the old System.Management.Automation one that might be referenced in a some of the documentation.

https://www.nuget.org/packages/Microsoft.PowerShell.5.ReferenceAssemblies/



· 7
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks @MarileeTurscak . I had added this package earlier and got an error . "The term 'Connect-AzureADUser' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.."

Hence I removed mentioned package and was giving a try. Either case, I wasnt able to execute commands successfully.

Am I missing something ?

0 Votes 0 ·

Where are you getting the command "ConnectAzureADUser"? I believe you need to use Connect-AzureAD https://docs.microsoft.com/en-us/powershell/module/azuread/connect-azuread?view=azureadps-2.0

1 Vote 1 ·
PrashuDA avatar image PrashuDA MarileeTurscak-MSFT ·

My bad. It is working now. Thanks @MarileeTurscak .

I am trying to create service principle as per following link https://docs.microsoft.com/en-us/powershell/azure/active-directory/signing-in-service-principal?view=azureadps-2.0
and it is failing. Shouldn't the document be updated ?

New-AzureADApplicationKeyCredential : Error occurred while executing SetApplication
Code: Request_BadRequest
Message: Key credential end date is invalid.
RequestId: 7e96b295-4c08-47ab-b35b-bd6fe4583a23
DateTimeStamp: Wed, 06 May 2020 15:13:39 GMT
Details: PropertyName - keyCredentials.endDate, PropertyErrorCode - InvalidKeyEndDate
HttpStatusCode: BadRequest
HttpStatusDescription: Bad Request
HttpResponseStatus: Completed

0 Votes 0 ·

Really! Helpful

Thanks!

0 Votes 0 ·
PrashuDA avatar image PrashuDA MarileeTurscak-MSFT ·

Hi @MarileeTurscak This is not working in .net core 3.1 . This package https://www.nuget.org/packages/Microsoft.PowerShell.5.ReferenceAssemblies/ supports only .net Framework.

I still get the error '{"The term 'Connect-AzureAD' is not recognized as the name of a cmdlet, function, script file, or operable program.\r\nCheck the spelling of the name, or if a path was included, verify that the path is correct and try again.' on .net core 3.1.

What package do I need to install to make it work on .net core 3.1 ?

0 Votes 0 ·
Show more comments
PrashuDA avatar image
1 Vote"
PrashuDA answered PrashuDA edited

It may be helpful for others. I added $now = [System.DateTime]::Now New-AzureADApplicationKeyCredential -ObjectId $application.ObjectId -CustomKeyIdentifier "Test123" -Type AsymmetricX509Cert -Usage Verify -Value $keyValue -StartDate $now -EndDate $cert.GetExpirationDateString()

This has fixed Key credential end date is invalid error.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

**What is End Date there*

first need the end date then further what we should do ? we discuss and resolve it.*

0 Votes 0 ·
PrashuDA avatar image PrashuDA RishabhPandey-1240 ·

As per the link https://docs.microsoft.com/en-us/powershell/azure/active-directory/signing-in-service-principal?view=azureadps-2.0

$notAfter = (Get-Date).AddMonths(6)

This date loads to the certificate. you can customize it. In my case End Date would be 11/6/20020 (6 Months From today)

For some reason $cert.GetEffectiveDateString() is not working , hence I converted start date as $now = [System.DateTime]::Now

0 Votes 0 ·
DanielBielski-9025 avatar image
0 Votes"
DanielBielski-9025 answered PrashuDA commented

Hello, PrashuDA. How are you?

I am making an application to study with this same resource of invoking azure and msol in powershell through C #. Could you share your application / source code so I can see it? It will help me a lot. Hugs and see you soon!

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Share your email details

0 Votes 0 ·

Love to see the community collaboration on the Microsoft Q&A! I would suggest creating a gist @PrashuDA and sharing the URL for @DanielBielski-9025 to avoid sharing an email address.

0 Votes 0 ·

@ryanchill I can surely do it. But the package doesnt support .net core 3.1. I believe Microsoft forgot to update the reference https://www.nuget.org/packages/Microsoft.PowerShell.5.ReferenceAssemblies/

0 Votes 0 ·