如何获取Azure DSC配置以读取名称为参数的自动化帐户变量?

时间:2018-05-15 15:48:50

标签: powershell azure dsc powershell-dsc

我尝试使用模板和DSC在Azure中自动构建Windows VM。我的同事熟悉模板但不熟悉PowerShell,我希望尽可能少地与DSC交互或运行cmdlet。如果我无法使用模板执行所有操作,我想使用Azure门户。

我很难找到一种非PowerShell方式将用户和组添加到本地Administrators组。我认为最简单的过程是:

  1. 模板有一个名为membersOfLocalAdmins的参数。它的值将是一个单独的字符串,如mydomain\username,mydomain\groupname...

  2. 模板在自动化帐户中创建一个名为membersOfLocalAdmins-*New VM Name*的变量。它的值是管理员字符串。

  3. DSC配置需要读取此变量。它目前有一个VM名称的参数。在Azure门户中,我们将转到自动化帐户,选择配置并单击“编译”。我们看到VM名称的提示并键入它,单击“确定”。

  4. DSC配置必须读取名为membersOfLocalAdmins-*New VM Name*的自动化帐户变量。然后将变量的值拆分为一个数组,并将其转换为其Group资源。我已尝试过类似下面的代码,但它不会编译,我认为配置中我可以使用PowerShell的唯一位置是脚本资源。有没有办法做到这一点,或者更好的方式?

    我还遇到了Azure门户中的编译DSC配置表格无法接受数组的问题。它显示为mydomain\username,mydomain\groupname... 作为单个字符串。如果我可以使用PowerShell在它到达Group资源之前拆分它,那也没关系。但是我会在配置中放置这个PowerShell吗?

    我想我将不得不使用脚本资源,将字符串拆分为数组并自行添加到管理员。凌乱。

    $DomainUserName = Get-AutomationVariable -Name 'Internal_Domain_Username'
    $DomainUserPassword = Get-AutomationVariable -Name 'Internal_Domain_Password'
    $DomainCredential = New-Object -TypeName System.Management.Automation.PSCredential($DomainUserName, (ConvertTo-SecureString $DomainUserPassword -AsPlainText -Force))
    
    Configuration TestConfiguration1Win10 {
        param(
            [string[]]$ComputerName = "localhost"
            ,
            [string[]]$membersOfLocalAdmins
        )
    
        # This if statement here causes the compile to fail. Is there anywhere else in the configuration I could put it?
        if (!$membersOfLocalAdmins){
            $membersOfLocalAdmins = $(Get-AutomationVariable -Name "membersOfLocalAdmins-@($ComputerName)[0]").Split(',')
        }
    
        Import-DscResource –ModuleName 'PSDesiredStateConfiguration'
        Import-DSCResource -Module xNetworking -Name xFirewallProfile
        Import-DSCResource -Module xSystemSecurity -Name xIEEsc
        Import-DscResource -ModuleName DeleteDscTmpFile
    
        Node $ComputerName {
            Group AddToLocalAdmins {
                GroupName ='Administrators'
                Ensure = 'Present'
                MembersToInclude = $membersOfLocalAdmins
                Credential = $DomainCredential
            }
    

1 个答案:

答案 0 :(得分:1)

删除$ membersOfLocalAdmins参数并更改组资源,如下所示。

     Group AddToLocalAdmins {
        GroupName ='Administrators'
        Ensure = 'Present'
        MembersToInclude = $((Get-AutomationVariable -Name "membersOfLocalAdmins-$ComputerName").Split(',').Trim())
        Credential = $DomainCredential
    }
相关问题