【问题标题】:Deploying an Azure VM and Users with an ARM template and DSC使用 ARM 模板和 DSC 部署 Azure VM 和用户
【发布时间】:2020-02-26 22:44:50
【问题描述】:

我第一次尝试创建一个 DSC(期望状态配置)以配合 ARM(Azure 资源管理器)模板来部署 Windows Server 2016 和其他本地用户帐户。到目前为止,ARM 模板工作正常,对于 DSC 文件,我正在使用简单的示例来测试功能。在我尝试传递用户名/密码以便创建本地 Windows 用户帐户之前,部署工作正常。我似乎根本无法使此功能正常工作(请参阅下面的错误消息)。

我的问题是,如何使用 ARM 模板将凭据(密码)传递给 DSC (mof) 文件,以便创建用户而无需明确允许纯文本密码(这不是一个好习惯)?

这是我尝试过的:

DSC 文件

Configuration xUser_CreateUserConfig {
[CmdletBinding()]

Param (

    [Parameter(Mandatory = $true)]
    [string]
    $nodeName,

    [Parameter(Mandatory = $true)]
    [System.Management.Automation.PSCredential]
    [System.Management.Automation.Credential()]
    $Credential
)

Import-DscResource -ModuleName xPSDesiredStateConfiguration

Node $nodeName {
    xUser 'CreateUserAccount' {
        Ensure = 'Present'
        UserName = Split-Path -Path $Credential.UserName -Leaf
        Password = $Credential
    }
}

}

Azure ARM 模板片段第一种方法

"resources": [
      {
          "apiVersion": "2016-03-30",
          "type": "extensions",
          "name": "Microsoft.Powershell.DSC",
          "location": "[parameters('location')]",
          "tags": {
            "DisplayName": "DSC",
            "Dept": "[resourceGroup().tags['Dept']]",
            "Created By": "[parameters('createdBy')]"
          },
          "dependsOn": [
            "[resourceId('Microsoft.Compute/virtualMachines', concat(variables('vmNamePrefix'), copyIndex(1)))]"
          ],
          "properties": {
            "publisher": "Microsoft.Powershell",
            "type": "DSC",
            "typeHandlerVersion": "2.19",
            "autoUpgradeMinorVersion": true,
            "settings": {
              "wmfVersion": "latest",
              "modulesUrl": "[concat(variables('_artifactslocation'), '/', variables('dscArchiveFolder'), '/', variables('dscArchiveFileName'))]",
              "configurationFunction": "xCreateUserDsc.ps1\\xUser_CreateUserConfig",
              "properties": {
                  "nodeName": "[concat(variables('vmNamePrefix'), copyIndex(1))]",
                    "Credential": {
                        "UserName": "[parameters('noneAdminUsername')]",
                        "Password": "PrivateSettingsRef:UserPassword"
                    }
              }
            },
            "protectedSettings": {
              "Items": {
                  "UserPassword": "[parameters('noneAdminUserPassword')]"
              }
            }
          }
        }
    ]

错误信息 资源操作完成,终端配置状态为“失败”。处理扩展“Microsoft.Powershell.DSC”时,VM 报告失败。错误消息:\\“DSC 扩展收到不正确的输入:处理配置“xUser_CreateUserConfig”时发生编译错误。请查看错误流中报告的错误并适当修改配置代码。System.InvalidOperationException 错误处理属性“密码”类型'xUser':不建议将加密密码转换为纯文本并存储。有关保护 MOF 文件中凭据的更多信息,请参阅 MSDN 博客:http://go.microsoft.com/fwlink/?LinkId=393729

此错误消息没有帮助

Azure ARM 模板 sn-p 第二种方法

"resources": [
    {
        "apiVersion": "2018-10-01",
        "type": "extensions",
        "name": "Microsoft.Powershell.DSC",
        "location": "[parameters('location')]",
        "tags": {
          "DisplayName": "DSC",
          "Dept": "[resourceGroup().tags['Dept']]",
          "Created By": "[parameters('createdBy')]"
        },
        "dependsOn": [
          "[resourceId('Microsoft.Compute/virtualMachines', concat(variables('vmNamePrefix'), copyIndex(1)))]"
        ],
        "properties": {
          "publisher": "Microsoft.Powershell",
          "type": "DSC",
          "typeHandlerVersion": "2.9",
          "autoUpgradeMinorVersion": true,
          "settings": {
            "wmfVersion": "latest",
            "configuration": {
              "url": "[concat(variables('_artifactslocation'), '/', variables('dscArchiveFolder'), '/', variables('dscArchiveFileName'))]",
              "script": "xCreateUserDsc.ps1",
              "function": "xUser_CreateUserConfig"
            },
            "configurationArguments": {
              "nodeName": "[concat(variables('vmNamePrefix'), copyIndex(1))]"
            },
            "privacy": {
              "dataCollection": "Disable"
            }
          },
          "protectedSettings": {
            "configurationArguments": {
              "Credential": {
                "UserName": "[parameters('noneAdminUsername')]",
                "Password": "[parameters('noneAdminUserPassword')]"
              }
            }
          }
        }
      }
  ]

错误信息

VM 在处理扩展“Microsoft.Powershell.DSC”时报告失败。错误消息:“DSC 扩展收到不正确的输入:找不到与参数名称 '$credential.Password' 匹配的参数。另一个常见错误是指定类型为 PSCredential 的参数而没有显式类型。请务必使用类型化的DSC Configuration 中的参数,例如:configuration Example param([PSCredential] $UserAccount)。请更正输入并重试执行扩展。有关故障排除的更多信息,请访问https://aka.ms/VMExtensionDSCWindowsTroubleshoot

这没有帮助!

这几天我一直在尝试解决这个错误。我已经用谷歌搜索了其他示例,但只能找到部署 Web 服务器的人的示例,而 Microsoft 的文档没有帮助,因为它告诉您使用上述两种方法。当方法 1 是旧方法时(根据 Microsoft)。因此,任何帮助将不胜感激。

【问题讨论】:

    标签: powershell arm-template dsc


    【解决方案1】:

    这就是我在配置中设置参数的方式:

    # Credentials
    [Parameter(Mandatory)]
    [System.Management.Automation.PSCredential]$Admincreds,
    

    然后在模板中:

    "properties": {
        "publisher": "Microsoft.Powershell",
        "type": "DSC",
        "typeHandlerVersion": "2.19",
        "autoUpgradeMinorVersion": true,
        "settings": {
            "configuration": xxx // doesn't matter for this question
            "configurationArguments": yyy // doesn't matter for this question
        },
        "protectedSettings": {
            "configurationArguments": {
                "adminCreds": {
                    "userName": "someValue",
                    "password": "someOtherValue"
                }
            }
        }
    }
    

    工作内容的链接:
    https://github.com/Cloudneeti/PCI_Reference_Architecture/blob/master/templates/resources/AD/azuredeploy.json#L261
    https://github.com/Cloudneeti/PCI_Reference_Architecture/blob/master/artifacts/configurationscripts/ad-domain.ps1#L11

    ps。你可能还需要做this。老实说,我不记得了;)

    【讨论】:

      猜你喜欢
      • 2019-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-01
      • 2019-02-11
      • 1970-01-01
      • 2017-12-19
      • 2019-10-01
      相关资源
      最近更新 更多