I have an Application Id and a Key that I generated in the Azure portal after registering an Azure Active Directory Application.
With this pair and from my Powershell script, I would like to generate an Access Token that I will use in my script in a HTTP Request.
I am able to get an access token using C# (see below code) but I am not sure how to do something similar in Powershell. Is there anything already built in Powershell? If not, can someone point me to a solution? I am pretty sure I am not the only person with this question.
public static async Task<string> GetAccessTokenAsync(string clientId, string appKey, string resourceId)
{
string aadInstance = "https://login.microsoftonline.com/{0}";
string tenant = "<TENANT>.onmicrosoft.com";
string authority = string.Format(CultureInfo.InvariantCulture, aadInstance, tenant);
AuthenticationContext authContext = new AuthenticationContext(authority);
ClientCredential clientCredential = new ClientCredential(clientId, appKey);
AuthenticationResult authenticationResult = null;
authenticationResult = await authContext.AcquireTokenAsync(resourceId, clientCredential);
return authenticationResult.AccessToken;
}