我正在尝试创建一个C#CmdLet,它近似于以下Powershell命令:-
$myCred = Get-AutomationPSCredential -Name $Automation_Credentials
$userName = $myCred.UserName
$password = $myCred.GetNetworkCredential().Password
所以我可以做与
等效的操作$prep = [System.Text.Encoding]::ASCII.GetBytes(${authpair})
$base64 = [System.Convert]::ToBase64String(${prep})
$basicAuthValue = "Basic $base64"
然后
Invoke-RestMethod
我认为这已经是
的第一个障碍了Get-AutomationPSCredential -Name $Automation_Credentials
在C#Cmdlet中。
最基本的C#CmdLet如下所示:-
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}"
}
}
}
答案 0 :(得分:0)
Azure自动化中的Get-AutomationPSCredential命令是特殊的:其实现使用平台内部结构来正确检索机密。您不想重复此代码:它相对复杂,并且您的副本可能随时停止工作,因为它依赖于内部组件之间的接口,该接口可以随时更改而无需通知。
如果您想在Get-AutomationPSCredential的基础上做一些事情,一个更好的主意是调用原始的Get-AutomationPSCredential命令,捕获输出,并执行其他处理。