I am trying to create a C# CmdLet that approximates the following Powershell commands: -
$myCred = Get-AutomationPSCredential -Name $Automation_Credentials
$userName = $myCred.UserName
$password = $myCred.GetNetworkCredential().Password
so I can then do the equivalent of
$prep = [System.Text.Encoding]::ASCII.GetBytes(${authpair})
$base64 = [System.Convert]::ToBase64String(${prep})
$basicAuthValue = "Basic $base64"
and then
Invoke-RestMethod
I see to have fallen at the very 1st hurdle of what is the equivalent of the
Get-AutomationPSCredential -Name $Automation_Credentials
in a C# Cmdlet.
The skeleton C# CmdLet looks as follows: -
namespace MyNamespace
{
using System.Management.Automation;
[Cmdlet(VerbsCommunications.Connect, "Ka")]
public class ConnectKa
: Cmdlet
{
/// <summary>
/// The base URL e.g. http://localhost:8081
/// </summary>
[Parameter (Position = 0,
HelpMessage="The base URL e.g. http://localhost:8081",
Mandatory= true)]
public string BaseUrl { get; set; } = null;
/// <summary>
/// The name of the Cluster to authenticate against
/// (not necessarily the cluster we are running the commands against).
/// </summary>
[Parameter (Position = 1,
HelpMessage="The name of the Cluster to authenticate against (not necessarily the cluster " +
"we are running the commands against).",
Mandatory= true)]
public string Cluster_Name { get; set; } = null;
[Parameter (Position = 2,
HelpMessage="The name of the Azure AutomationAccount to log in with.",
Mandatory= true)]
public string Automation_Credentials { get; set; } = null;
/// <summary>
/// Perform Cmdlet processing.
/// </summary>
protected override void ProcessRecord()
{
// Need to perform the equivalent of the following Powershell
// $myCred = Get-AutomationPSCredential -Name $Automation_Credentials
// $userName = $myCred.UserName
// $password = $myCred.GetNetworkCredential().Password
// $authpair = "${userName}:${password}"
}
}
}
The Get-AutomationPSCredential command in Azure Automation is special: its implementation uses platform internals to retrieve secrets properly. You don't want to duplicate this code: it is relatively complicated, and your copy may stop working any time, as it would rely on interfaces between internal components, which can change any time without notice.
If you want to do something on top of what Get-AutomationPSCredential does, a better idea would be to invoke the original Get-AutomationPSCredential command, capture the output, and perform your additional processing.