1

我正在为我们的开发机器获取 DSC 配置设置。我遇到的要么是我如何处理复合资源的错误,要么是我看不到的东西。

我可以毫无问题地运行 DevMachineConfig.ps1,但是当我使用 创建 MOF 文件时DeveloperMachine -ConfigurationData $ConfigurationData,出现以下错误:

PSDesiredStateConfiguration\Configuration : The argument is null. Provide a valid value for 
the argument, and then try running the command again.
At C:\Program Files\WindowsPowerShell\Modules\DeveloperConfig\DSCResources\CreateDomainController\CreateDomainController.schema.psm1:1 char:1
+ Configuration CreateDomainController
+ ~~~~~~~~~~~~~
+ CategoryInfo          : MetadataError: (:) [Configuration], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ArgumentIsNull,Configuration

我只是不清楚为什么Configuration关键字上会出现空值,因为这似乎是内置于 DSC 中的。

所以我的问题是这是我的错误还是 DSC 的错误?

以下是文件和相关信息

我的“项目”布局:

\DevMachineConfig.ps1
\Modules\DeveloperConfig\DeveloperConfg.psd1
\Modules\DeveloperConfig\DSCResources\GetDscResources\GetDscResources.schema.psm1
\Modules\DeveloperConfig\DSCResources\CreateDomainController\CreateDomainController.schema.psm1

*Modules 文件夹连接到 $env:ProgramFiles\WindowsPowershell\Modules

开发机器配置.ps1

$ConfigurationData = @{
AllNodes = @(
    @{ 
        NodeName = "localhost"; 
        DomainName = "dev.loc";
        PSDscAllowPlainTextPassword = $true;            
        DomainUsers = @(
            @{ Name = 'web-app'; DomainAdmin = $False }               
        );
     }
)
}

Configuration DeveloperMachine 
{
   $installPassword = ConvertTo-SecureString "password!!" -AsPlainText -Force
   $credentials = New-Object 
             System.Management.Automation.PSCredential("deltaadmin" $installPassword)

Import-DscResource -ModuleName DeveloperConfig, xSystemSecurity

Node "localhost"
{
    LocalConfigurationManager 
    {
        DebugMode = "All"
        ActionAfterReboot = "ContinueConfiguration"
    }

    GetDscResources DSCResources { }

    CreateDomainController DevDomain {
        DomainName = $Node.DomainName
        DomainUsers = $Node.DomainUsers
        DependsOn = "[SetWindowsFeatures]Features"
    }
}

获取DscResources.schema.psm1

Configuration GetDscResources 
{
    param(
        [string] $WorkingDir = "c:\temp\dsc\", 
        [string] $DownloadUrl = "https://gallery.technet.microsoft.com/scriptcenter/DSC-Resource-Kit-All-c449312d/file/131371/1/DSC%20Resource%20Kit%20Wave%209%2012172014.zip"
    )

    $archivePath = Join-Path $WorkingDir "dsc-wave9.zip"
    $resourcesPath = Join-Path $WorkingDir "All Resources"
    $powerShellModule = Join-Path $env:ProgramFiles "WindowsPowerShell\Modules"

    File DeltaDscFolder {
        Ensure = "Present"        
        Type = "Directory"
        DestinationPath = $WorkingDir
    }

    Script DownloadDscResources 
    {
        TestScript = {
            Write-Verbose -Verbose "Checking to see if the path [$using:WorkingDir] exists"
            Write-Verbose -Verbose "Found the Path: $(Test-Path $using:WorkingDir)"
            return !(Test-Path $using:WorkingDir)
        }
        GetScript = { 
            if((Test-Path $(Join-Path $using:powerShellModule "xActiveDirectory")))
            {
                $result = "DSC Resources have been downloaded."
            } else {
                $result = "DSC Resources have not been downloaded."
            }

            return @{ 
                Result = $result
                GetScript = $GetScript
                SetScript = $SetScript
                TestScript = $TestScript
            }
        }
        SetScript = {
            Write-Verbose -Verbose "Downloading the wave9 resources from $using:DownloadUrl to $using:archivePath"
            (New-Object System.Net.WebClient).DownloadFile($using:DownloadUrl, $using:archivePath)
            Write-Verbose -Verbose "Download complete"
        }
        DependsOn = "[File]DeltaDscFolder"
    }

    Archive DscResourceWave9 {
        Destination = $WorkingDir
        Path = $archivePath
        Ensure = "Present"
        DependsOn = "[Script]DownloadDscResources"
    }

    File MoveDscTo {
        Ensure = "Present"
        Type = "Directory"
        Recurse = $true
        SourcePath = $resourcesPath
        DestinationPath = $powerShellModule
        DependsOn = "[Archive]DscResourceWave9"
    }
}

CreateDomainController.schema.psm1

Configuration CreateDomainController 
{
    Param(
        [string] $DomainName = "dev.loc",
        [Parameter(Mandatory)]
        [Hashtable[]] $DomainUsers = $null
    )

    Import-Module -Name ActiveDirectory 
    Import-DscResource -ModuleName xActiveDirectory

    $password = ConvertTo-SecureString "password1!!" -AsPlainText -Force
    $credentials = New-Object System.Management.Automation.PSCredential("admin", $password)

        xADDomain DevelopmentDomain
        {
            DomainName = $DomainName
            DomainAdministratorCredential = $credentials
            SafemodeAdministratorPassword = $credentials
        }

        xWaitForADDomain ADForestWait
        {
            DomainName = $DomainName
            DomainUserCredential = $credentials
            RetryCount = 20
            RetryIntervalSec = 30
            DependsOn = "[xADDomain]DevelopmentDomain"
        }

        <#
            Generate the domain users.
        #>

        foreach($domainUser in $DomainUsers) 
        {
            xADUser $domainUser.Name
            {
                DomainName = $DomainName
                DomainAdministratorCredential = $credentials
                UserName = $domainUser.Name
                Password = $credentials
                Ensure = "Present"
                DependsOn = "[xWaitForADDomain]ADForestWait"            
            }

            if($domainUser.DomainAdmin -eq $True)
            {
                Add-ADPrincipalGroupMembership -Identity $adminUser.Name -MemberOf "Domain Admins"               
            }

            if($domainUser.DelegateAccount -eq $True)
            {
                $rootDse = [ADSI]"LDAP://RootDSE"
                $principal = New-Object Security.Principal.NTAccount("DEV\$($domainUser.Name)")   
                DSACLS "$($rootDse.defaultNamingContext)" /G "$($principal):CA;Replicating Directory Changes"
                DSACLS "$($rootDse.configurationNamingContext)" /G "$($principal):CA;Replicating Directory Changes"
            }
        }
}
4

1 回答 1

1
Param(
    [string] $DomainName = "dev.loc",
    [Parameter(Mandatory)]
    [Hashtable[]] $DomainUsers = $null
)

这比 DSC 更像是一个 Powershell 问题。您有一个带有默认值的强制参数。您应该选择其中之一。

Mandatory 表示必须提供参数。真正应该使用默认值,Mandatory=$false因为这样它的行为更有意义(如果调用者不提供默认值,则使用此值)。

于 2015-03-11T20:25:30.390 回答