適用於: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+空格自動 完成,可以探索其屬性。
使用複合資源
接下來,我們建立呼叫複合資源的組態。 此組態會呼叫 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";
}