撰寫複合 DSC 資源
在真實世界的情況下,DSC 設定可能會變得很長且複雜,呼叫數個不同的 DSC 資源並設定數十個屬性。 為了協助解決這種複雜性,您可以使用 DSC 設定作為其他 DSC 組態的 DSC 資源。 這稱為複合 DSC 資源。 複合 DSC 資源是採用參數的 DSC 組態。 DSC 組態的參數可作為 DSC 資源的屬性。 DSC 組態會儲存為擴展名為的 .schema.psm1
檔案。 如需 DSC 資源的詳細資訊,請參閱 DSC 資源。
重要
複合 DSC 資源不適用於 Invoke-DscResource
。 在 DSC 2.0 和更新版本中,它們僅支援與 Azure Automanage 的電腦設定功能搭配使用。
建立複合資源
在我們的範例中,我們會建立可叫用數個現有資源的組態來設定虛擬機。 不指定要在設定區塊中設定的值,而是讓設定接受之後在設定區塊中使用的參數。
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 資源區塊放在複合 DSC 資源定義中。
將 DSC 設定儲存為複合 DSC 資源
若要使用參數化的 DSC 組態作為 DSC 資源,請將它儲存在目錄結構中,例如 MOF 型 DSC 資源,並以延伸模組命名 .schema.psm1
。 針對此範例,我們會將檔案命名為 xVirtualMachine.schema.psm1
。 您還必須建立名為 xVirtualMachine.psd1
且包含下列行的資訊清單。
RootModule = 'xVirtualMachine.schema.psm1'
注意
這與 MyDscResources.psd1
,這是資料夾下 MyDscResources
所有 DSC 資源的模組指令清單。
完成之後,資料夾結構應如下。
$env: psmodulepath
|- MyDscResources
|- MyDscResources.psd1
|- DSCResources
|- xVirtualMachine
|- xVirtualMachine.psd1
|- xVirtualMachine.schema.psm1
DSC 資源現在可以使用 Get-DscResource
Cmdlet 來探索,而且其屬性可由該 Cmdlet 或 VS Code 中的 Ctrl空格鍵+自動完成來探索。
使用複合資源
接下來,我們會建立會呼叫複合 DSC 資源的 DSC 組態。 此 DSC 組態會 xVirtualMachine
呼叫複合 DSC 資源來建立虛擬機。
Configuration CreateVM {
Import-DSCResource -ModuleName xVirtualMachine
xVirtualMachine VM {
VMName = "Test"
SwitchName = "Internal"
SwitchType = "Internal"
VhdParentPath = "C:\Demo\VHD\RTM.vhd"
VHDPath = "C:\Demo\VHD"
VMStartupMemory = 1024MB
VMState = "Running"
}
}
您也可以使用此複合 DSC 資源來建立多個 VM,方法是傳入複合 DSC 資源 之 VMName 屬性的 VM 名稱陣列。
Configuration MultipleVms {
Import-DSCResource -ModuleName xVirtualMachine
xVirtualMachine VMs {
VMName = @(
"IIS01"
"SQL01"
"SQL02"
)
SwitchName = "Internal"
SwitchType = "Internal"
VhdParentPath = "C:\Demo\VHD\RTM.vhd"
VHDPath = "C:\Demo\VHD"
VMStartupMemory = 1024MB
VMState = "Running"
}
}