【发布时间】:2020-10-02 05:27:37
【问题描述】:
我正在尝试创建一个近似于以下 Powershell 命令的 C# CmdLet:-
$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}"
}
}
}
【问题讨论】:
-
"fallen" 不是问题陈述,您遇到什么错误?
$Automation_Credentials来自哪里? -
我没有收到任何错误,因为我正在尝试编写 C# 等效项。我的 Powershell 代码运行良好,$Automation_Credentials 是 Powershell 函数的 [Parameter]。将此代码转换为等效的 C# Azure SDK 是我的挑战,因为我无法弄清楚如何使用帐户名称获取 AutomationAccount 的凭据。
-
我们无法在没有看到您的 c# 的情况下帮助您修复您的 c#。
标签: c# azure-automation azure-authentication