0

I am trying to call a power-shell script that is using New-AzSubscriptionAlias(that is creating a new EA subscription) from C#.net-core3.1.

I am not using SP for calling that power-shell script in C# and below error is coming... enter image description here

Any solutions , in C#.net how to call a Powershell-script without SP? Below Error msg as "Connect-AzAccount: Username + Password authentication is not supported in PowerShell Core. Please use device code authentication for interactive log in, or Service Principal authentication for script log in."

Many Thanks,

  • 2
    Maybe [this](https://stackoverflow.com/questions/54890630/connect-azaccount-how-to-avoid-azure-device-authentication) can help? – Daniel Feb 07 '21 at 18:03

1 Answers1

0

You can refer this tutorial that explains you the process step by step. And, this is the GitHub source code link.

To include a summary, here is the runspace pool:

using System;
using System.Collections.Generic;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Threading.Tasks;
 
namespace CustomRunspaceStarter
{
  /// <summary>
  /// Contains functionality for executing PowerShell scripts.
  /// </summary>
  public class HostedRunspace
  {
    /// <summary>
    /// The PowerShell runspace pool.
    /// </summary>
    private RunspacePool RsPool { get; set; }
     
    /// <summary>
    /// Initialize the runspace pool.
    /// </summary>
    /// <param name="minRunspaces"></param>
    /// <param name="maxRunspaces"></param>
    public void InitializeRunspaces(int minRunspaces, int maxRunspaces, string[] modulesToLoad)
    {
      // create the default session state.
      // session state can be used to set things like execution policy, language constraints, etc.
      // optionally load any modules (by name) that were supplied.
       
      var defaultSessionState = InitialSessionState.CreateDefault();
      defaultSessionState.ExecutionPolicy = Microsoft.PowerShell.ExecutionPolicy.Unrestricted;
       
      foreach (var moduleName in modulesToLoad)
      {
        defaultSessionState.ImportPSModule(moduleName);
      }
       
      // use the runspace factory to create a pool of runspaces
      // with a minimum and maximum number of runspaces to maintain.
       
      RsPool = RunspaceFactory.CreateRunspacePool(defaultSessionState);
      RsPool.SetMinRunspaces(minRunspaces);
      RsPool.SetMaxRunspaces(maxRunspaces);
       
      // set the pool options for thread use.
      // we can throw away or re-use the threads depending on the usage scenario.
       
      RsPool.ThreadOptions = PSThreadOptions.UseNewThread;
       
      // open the pool. 
      // this will start by initializing the minimum number of runspaces.
       
      RsPool.Open();
    }
     
    /// <summary>
    /// Runs a PowerShell script with parameters and prints the resulting pipeline objects to the console output. 
    /// </summary>
    /// <param name="scriptContents">The script file contents.</param>
    /// <param name="scriptParameters">A dictionary of parameter names and parameter values.</param>
    public async Task RunScript(string scriptContents, Dictionary<string, object> scriptParameters)
    {
      if (RsPool == null)
      {
        throw new ApplicationException("Runspace Pool must be initialized before calling RunScript().");
      }
       
      // create a new hosted PowerShell instance using a custom runspace.
      // wrap in a using statement to ensure resources are cleaned up.
       
      using (PowerShell ps = PowerShell.Create())
      {
        // use the runspace pool.
        ps.RunspacePool = RsPool;
         
        // specify the script code to run.
        ps.AddScript(scriptContents);
         
        // specify the parameters to pass into the script.
        ps.AddParameters(scriptParameters);
         
        // subscribe to events from some of the streams
        ps.Streams.Error.DataAdded += Error_DataAdded;
        ps.Streams.Warning.DataAdded += Warning_DataAdded;
        ps.Streams.Information.DataAdded += Information_DataAdded;
         
        // execute the script and await the result.
        var pipelineObjects = await ps.InvokeAsync().ConfigureAwait(false);
         
        // print the resulting pipeline objects to the console.
        Console.WriteLine("----- Pipeline Output below this point -----");
        foreach (var item in pipelineObjects)
        {
          Console.WriteLine(item.BaseObject.ToString());
        }
      }
    }
     
    /// <summary>
    /// Handles data-added events for the information stream.
    /// </summary>
    /// <remarks>
    /// Note: Write-Host and Write-Information messages will end up in the information stream.
    /// </remarks>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Information_DataAdded(object sender, DataAddedEventArgs e)
    {
      var streamObjectsReceived = sender as PSDataCollection<InformationRecord>;
      var currentStreamRecord = streamObjectsReceived[e.Index];
       
      Console.WriteLine($"InfoStreamEvent: {currentStreamRecord.MessageData}");
    }
     
    /// <summary>
    /// Handles data-added events for the warning stream.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Warning_DataAdded(object sender, DataAddedEventArgs e)
    {
      var streamObjectsReceived = sender as PSDataCollection<WarningRecord>;
      var currentStreamRecord = streamObjectsReceived[e.Index];
       
      Console.WriteLine($"WarningStreamEvent: {currentStreamRecord.Message}");
    }
     
    /// <summary>
    /// Handles data-added events for the error stream.
    /// </summary>
    /// <remarks>
    /// Note: Uncaught terminating errors will stop the pipeline completely.
    /// Non-terminating errors will be written to this stream and execution will continue.
    /// </remarks>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Error_DataAdded(object sender, DataAddedEventArgs e)
    {
      var streamObjectsReceived = sender as PSDataCollection<ErrorRecord>;
      var currentStreamRecord = streamObjectsReceived[e.Index];
       
      Console.WriteLine($"ErrorStreamEvent: {currentStreamRecord.Exception}");
    }
  }
}
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Harshita Singh
  • 4,590
  • 1
  • 10
  • 13
  • Thanks @singhh-msft for replay. Above ,shared C#.net -core code for calling a power-shell script with and it is using of service-principal of azure cloud.it is working.. I want to know that if possible to call a power-shell script with not using of service-principal of azure cloud?. – Anirban Goswami Feb 08 '21 at 10:09
  • If you want to use it that way, you will have to authenticate yourself - which will require browser. FUrther, there is no extra cost associated with this approach and it is a safe way AFAIK to call scripts using C#. – Harshita Singh Feb 08 '21 at 10:33
  • Thank @ singhh-msft, Did not get " AFAIK to call scripts using C#. – singhh-msft 2 hours ago ".. – Anirban Goswami Feb 08 '21 at 12:34
  • Cannot edit previous comment, hence clarifying here. AFAIK: As far as I know. – Harshita Singh Feb 08 '21 at 12:52
  • Does this help? – Harshita Singh Feb 09 '21 at 07:35
  • Thank @ singhh-msft We have already written .net-core-C# code which was calling a power-shell script(.ps1) for a restful api type app. But we are using userID+Password of azure as parameters of script not using a SP for calling power-shell script(.ps1). Our qtns is like "is it possible to call a power-shell script(.ps1) with userID+Password of azure as parameters of script"? – Anirban Goswami Feb 09 '21 at 13:02
  • 1
    The answer is no, without user interaction, this is not possible. – Harshita Singh Feb 09 '21 at 13:21