none
Azure login issue - trying to create a web to run PowerShell commands RRS feed

  • General discussion

  • Hello

    I'm trying to create an asp.net site to run specific commands for non-techies.

    I can log in to Azure, but after that I try to run a command and I receive this: "Your Azure credentials have not been set up or have expired, please run Login-AzureRMAccount to set up your Azure credentials."

    This is basically how I'm doing it:

    using System;
      using System.Collections.ObjectModel;
      using System.Management.Automation;
      using System.Management.Automation.Runspaces;
      using Microsoft.PowerShell.Commands;
      using PowerShell = System.Management.Automation.PowerShell;
    using System.Management.Automation.Host;
    
    namespace keepPSSessionOpen
    {
        public partial class WebForm2 : System.Web.UI.Page
        {
            static Runspace runspace = RunspaceFactory.CreateRunspace();
            static Boolean loginResult = false;
            protected void Page_Load(object sender, EventArgs e)
            {
                PowerShell powershell = PowerShell.Create();
                powershell.Runspace = runspace;
    
                String loginScript = "(Select-AzureRmProfile -Path 'C:\\Users\\dimas\\Source\\Repos\\runPSsite\\slnSite\\slnSite\\PS\\profile.json').Context.Subscription.SubscriptionName;";
                String getvms5029 = "get-azurermvm -resourcegroupname rg5029; $error;";
    
                powershell.AddScript(loginScript+ getvms5029);
    
                if (runspace.RunspaceStateInfo.State != RunspaceState.Opened)
                    runspace.Open();
    
                Collection< PSObject> getvmsresult = powershell.Invoke();
    
            }
        }
    }


    and getvmsresult is this (from the Immediate window with a breakpoint):

    getvmsresult
    Count = 2
        [0]: {mySubscription}
        [1]: {Your Azure credentials have not been set up or have expired, please run Login-AzureRMAccount to set up your Azure credentials.}
    

    [0] returns what is expected: the subscription name.

    However, the second command in the script is telling me that I am not logged in. Just to be clear, it's like running this in PS:

    (Select-AzureRmProfile -Path 'C:\\Users\\dimas\\Source\\Repos\\runPSsite\\slnSite\\slnSite\\PS\\profile.json').Context.Subscription.SubscriptionName;
    get-azurermvm -resourcegroupname rg5029;
    $error;

    Any suggestions are welcome, like not using runspaces and something else instead. I want to avoid, if possible, having to log in for every command that's issued.

    Thanks

    Dimas

    Thursday, June 8, 2017 2:22 PM

All replies

  • You will need to post your issue in the Web developers forums.

    By nature a web site is stateless.  Each  click of a link or control will re-establish the connection and perform the task.

    Post here to learn how to write "stateful" programs for a web site: http://forums.iis,net


    \_(ツ)_/

    Thursday, June 8, 2017 6:03 PM
  • I understand your point.

    However it doesn't apply here. Before doing this, I tested with the following PS code, with its output tied to a textbox

    "\$a: $a";
    $a = get-date;
    get-date;

    and the first page load would yield something like

    $a:
    06-jun-2017

    and then, on reloads, what was expected:

    $a: 06-jun-2017
    06-jun-2017

    I believe this is so because I made the runspace static, and I'm always running

    powershell.Runspace = runspace;
    in order to use that one and not a new one.

    Thursday, June 8, 2017 8:37 PM
  • Unfortunately this is not a web developer forum.

    -- Bill Stewart [Bill_Stewart]

    Thursday, June 8, 2017 8:55 PM
  • This is about how to use a runspace, or what to replace the runspace with, not about web... Or do you think web devs in general have any idea of what a runspace is?
    Friday, June 9, 2017 8:45 PM
  • This is about how to use a runspace, or what to replace the runspace with, not about web... Or do you think web devs in general have any idea of what a runspace is?

    A good web dev knows how to set up a decent C# aspx page and how to use threading.   A runspace is just an advanced away to create a thread.

    You may also have to learn more about how a web site has to be configured to support what you are trying to accomplish.  There are also many add-ins for web sites that allow PowerShell scripts to be run.

    Here is one example: http://jeffmurr.com/blog/?p=142

    You will also have to fight your way through he restrictions imposed by Azure.  Post in Azure developers forums for help getting the setup correct for a web site hosting PowerShell.  I recommend no using Azure logins but use web logins and a restricted PowerShell session.


    \_(ツ)_/

    Friday, June 9, 2017 8:56 PM