【问题标题】:Building Module for Azure Automation Account in C#C# 中 Azure 自动化帐户的构建模块
【发布时间】: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


【解决方案1】:

Azure 自动化中的 Get-AutomationPSCredential 命令很特殊:它的实现使用平台内部来正确检索机密。您不想复制此代码:它相对复杂,您的副本可能随时停止工作,因为它依赖于内部组件之间的接口,这些接口可能随时更改,恕不另行通知。

如果您想在 Get-AutomationPSCredential 的基础上做一些事情,更好的办法是调用原始的 Get-AutomationPSCredential 命令,捕获输出,然后执行额外的处理。

【讨论】:

  • 澄清一下,我要问的是,如果 Get-AutomationPSCredential 是 Powershell 命令,那么对我可以调用的预构建 Microsoft C# SDK 的调用的翻译是什么。我不想重建它,我只是在寻找那个翻译,就像用于 API 调用的 Goole Translate 一样。或者我们是说没有公开的方式从 C# CMDLet 使用 Microsoft C# SDK 登录到 AzureAutomation 帐户,而只能通过 CMDLet 调用 Powershell 脚本?
  • @Jock 这正是我的观点:没有这样的翻译。 Get-AutomationPSCredential 实现使用未通过任何已发布 SDK 公开的自动化平台内部结构。理论上,您可以对这个实现进行逆向工程,但话又说回来:它依赖于不属于任何公共合约的内部结构,因此它们可以随时更改而无需通知,从而破坏您的代码。这些内部人员甚至都需要自动化帐户凭据,因为他们已经“知道”他们在哪个上下文中运行。
  • 感谢您的澄清。还解释了为什么在我在网上搜索答案的几个小时内,我没有找到答案:)
猜你喜欢
  • 2020-08-29
  • 1970-01-01
  • 2022-01-07
  • 1970-01-01
  • 1970-01-01
  • 2015-10-29
  • 2021-06-07
  • 1970-01-01
  • 2021-06-21
相关资源
最近更新 更多