Hi Team,
I am tryign to login to azure using powershell via c#. but i get below error
Inner Exception 1:
CmdletInvocationException: File C:\Program Files (x86)\WindowsPowerShell\Modules\AzureRM.Profile\4.6.0\AzureRM.Profile.psm1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
Inner Exception 2:
PSSecurityException: File C:\Program Files (x86)\WindowsPowerShell\Modules\AzureRM.Profile\4.6.0\AzureRM.Profile.psm1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
Inner Exception 3:
UnauthorizedAccessException: File C:\Program Files (x86)\WindowsPowerShell\Modules\AzureRM.Profile\4.6.0\AzureRM.Profile.psm1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
Below is the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Collections.ObjectModel;
using Microsoft.PowerShell.Commands;
using Newtonsoft.Json;
namespace powershelltest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private string RunScript (string Script)
{
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
//pipeline.Invoke("Set-ExecutionPolicy Unrestricted");
pipeline.Commands.AddScript(Script);
pipeline.Commands.Add("Out-String");
Collection results = pipeline.Invoke();
runspace.Close();
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject pSObject in results)
stringBuilder.AppendLine(pSObject.ToString());
return stringBuilder.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
textBox2.Clear();
textBox2.Text = RunScript(textBox1.Text);
}
}
}
1 Reply
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Ashutosh GuptaPosted Nov 27, 2019, 10:40 PM
privatestaticasyncTask<string> GetAccessToken(stringtenantId,stringclientId,stringclientKey){WriteLine("Begin GetAccessToken");stringauthContextURL ="https://login.windows.net/"+ tenantId;varauthenticationContext =newAuthenticationContext(authContextURL);varcredential =newClientCredential(clientId, clientKey);varresult =awaitauthenticationContext.AcquireTokenAsync("https://management.azure.com/", credential);if(result ==null){thrownewInvalidOperationException("Failed to obtain the JWT token");}stringtoken = result.AccessToken;returntoken;}You need to include the “Microsoft.IdentityModel.Clients.ActiveDirectory” package into the project. It is installable via NuGet…love that NuGet stuff BTW. This method receives the tenant id which I mentioned earlier, I.e. the Azure Active Directory directory id, the application id and the password key of the service principle. The method returns the token. Then you can run the REST API, which I did like this.
privatestaticasyncTask<string> RestartWebApp(stringtoken){WriteLine("Begin RestartWebApp");stringURL ="https://management.azure.com/subscriptions/{subscriptionId}"+"/resourceGroups/{RGName}/providers/Microsoft.Web/sites/"+"{siteName}/restart?softRestart=false&synchronous=false?"+"api-version=2016-08-01";httpClient.DefaultRequestHeaders.Remove("Authorization");httpClient.DefaultRequestHeaders.Add("Authorization","Bearer "+ token);HttpResponseMessage response =awaithttpClient.PostAsync(URL,null);returnresponse.StatusCode.ToString();}