复合资源:将 DSC 配置用作资源

适用于:Windows PowerShell 4.0、Windows PowerShell 5.0

在实际情况下,配置可能会变得冗长而复杂,调用许多不同的资源并设置大量属性。 为了帮助解决这种复杂性,可以使用 Windows PowerShell 所需状态配置 (DSC) 配置作为其他配置的资源。 这称为复合资源。 复合资源是采用参数的 DSC 配置。 配置的参数充当资源的属性。 配置将保存为带有扩展名的文件 .schema.psm1 。 它取代了典型 DSC 资源中的 MOF 架构和资源脚本。 有关 DSC 资源的详细信息,请参阅 Windows PowerShell 所需状态配置资源

创建复合资源

在我们的示例中,我们创建了一个配置,该配置调用许多现有资源来配置虚拟机。 配置不是指定要在配置块中设置的值,而是采用参数,然后在配置块中使用这些参数。

Configuration xVirtualMachine
{
    param
    (
        # Name of VMs
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [String[]] $VMName,

        # Name of Switch to create
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [String] $SwitchName,

        # Type of Switch to create
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [String] $SwitchType,

        # Source Path for VHD
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [String] $VHDParentPath,

        # Destination path for diff VHD
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [String] $VHDPath,

        # Startup Memory for VM
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [String] $VMStartupMemory,

        # State of the VM
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [String] $VMState
    )

    # Import the module that defines custom resources
    Import-DSCResource -ModuleName xComputerManagement,xHyper-V

    # Install the Hyper-V role
    WindowsFeature HyperV
    {
        Ensure = "Present"
        Name = "Hyper-V"
    }

    # Create the virtual switch
    xVMSwitch $SwitchName
    {
        Ensure = "Present"
        Name = $SwitchName
        Type = $SwitchType
        DependsOn = "[WindowsFeature]HyperV"
    }

    # Check for Parent VHD file
    File ParentVHDFile
    {
        Ensure = "Present"
        DestinationPath = $VHDParentPath
        Type = "File"
        DependsOn = "[WindowsFeature]HyperV"
    }

    # Check the destination VHD folder
    File VHDFolder
    {
        Ensure = "Present"
        DestinationPath = $VHDPath
        Type = "Directory"
        DependsOn = "[File]ParentVHDFile"
    }

    # Create VM specific diff VHD
    foreach ($Name in $VMName)
    {
        xVHD "VHD$Name"
        {
            Ensure = "Present"
            Name = $Name
            Path = $VHDPath
            ParentPath = $VHDParentPath
            DependsOn = @("[WindowsFeature]HyperV",
                          "[File]VHDFolder")
        }
    }

    # Create VM using the above VHD
    foreach($Name in $VMName)
    {
        xVMHyperV "VMachine$Name"
        {
            Ensure = "Present"
            Name = $Name
            VhDPath = (Join-Path -Path $VHDPath -ChildPath $Name)
            SwitchName = $SwitchName
            StartupMemory = $VMStartupMemory
            State = $VMState
            MACAddress = $MACAddress
            WaitForIP = $true
            DependsOn = @("[WindowsFeature]HyperV",
                          "[xVHD]VHD$Name")
        }
    }
}

注释

DSC 目前不支持在复合资源中放置复合资源或嵌套配置。

将配置另存为复合资源

若要将参数化配置用作 DSC 资源,请将其保存在目录结构中,就像任何其他基于 MOF 的资源一样,并使用扩展名命名 .schema.psm1 它。 对于此示例,我们将文件 xVirtualMachine.schema.psm1命名为 . 您还需要创建一个名为 xVirtualMachine.psd1 包含以下行的清单。

RootModule = 'xVirtualMachine.schema.psm1'

注释

这是对文件夹下MyDscResources所有资源的模块清单的补充MyDscResources.psd1

完成后,文件夹结构应如下所示。

$env: psmodulepath
    |- MyDscResources
        |- MyDscResources.psd1
        |- DSCResources
            |- xVirtualMachine
                |- xVirtualMachine.psd1
                |- xVirtualMachine.schema.psm1

现在可以使用 cmdlet 发现 Get-DscResource 资源,并且可以通过该 cmdlet 或使用 Windows PowerShell ISE 中的 Ctrl+Space 自动完成来发现其属性。

使用复合资源

接下来,我们创建一个调用复合资源的配置。 此配置调用 xVirtualMachine 复合资源以创建虚拟机,然后调用 xComputer 资源以重命名它。

configuration RenameVM
{
    Import-DSCResource -ModuleName xVirtualMachine
    Node localhost
    {
        xVirtualMachine VM
        {
            VMName = "Test"
            SwitchName = "Internal"
            SwitchType = "Internal"
            VhdParentPath = "C:\Demo\VHD\RTM.vhd"
            VHDPath = "C:\Demo\VHD"
            VMStartupMemory = 1024MB
            VMState = "Running"
        }
    }

    Node "192.168.10.1"
    {
        xComputer Name
        {
            Name = "SQL01"
            DomainName = "fourthcoffee.com"
        }
    }
}

还可以使用此资源通过将 VM 名称数组传递给 xVirtualMachine 资源来创建多个 VM。

Configuration MultipleVms
{
    Import-DSCResource -ModuleName xVirtualMachine
    Node localhost
    {
        xVirtualMachine VMs
        {
            VMName = "IIS01", "SQL01", "SQL02"
            SwitchName = "Internal"
            SwitchType = "Internal"
            VhdParentPath = "C:\Demo\VHD\RTM.vhd"
            VHDPath = "C:\Demo\VHD"
            VMStartupMemory = 1024MB
            VMState = "Running"
        }
    }
}

支持 PsDscRunAsCredential

注释

PowerShell 5.0 及更高版本支持 PsDscRunAsCredential

PsDscRunAsCredential 属性可用于 DSC 配置资源块,以指定资源应在一组指定的凭据下运行。 有关详细信息,请参阅 使用用户凭据运行 DSC

要从自定义资源中访问用户上下文,可以使用 自动变量 $PsDscContext

例如,以下代码会将运行资源的用户上下文写入详细输出流:

if ($PsDscContext.RunAsUser) {
    Write-Verbose "User: $PsDscContext.RunAsUser";
}

另请参阅

概念