DSC 資源
適用於 Windows PowerShell 4.0 與更新版本。
概觀
預期狀態設定 (DSC) 資源提供 DSC 設定的建置組塊。 資源會公開可以設定的屬性 (結構描述),而且這些屬性包含本機設定管理員 (LCM) 稱之為「make it so (讓它變成這樣)」的 PowerShell 指令碼函式。
資源可以建立某些物件的模型,像檔案一樣平常或如 IIS 伺服器設定一樣專用。 類似資源的群組會併入 DSC 模組,將所有必要的檔案組織到可攜式結構中,而且包含中繼資料以識別資源的原訂用法。
每個資源都有一個*結構描述,可判斷在設定中使用資源所需的語法。 您可以透過下列方式定義資源的結構描述:
Schema.Mof
檔案:大部分的資源都會使用受控物件格式,在schema.mof
檔案中定義其「結構描述」。<Resource Name>.schema.psm1
檔案:複合資源會使用參數區塊,在<ResourceName>.schema.psm1
檔案中定義其「結構描述」。<Resource Name>.psm1
檔案:以類別為基礎的 DSC 資源會在類別定義中定義其「結構描述」。 語法項目會標示為 Class 屬性。 如需詳細資訊,請參閱 about_Classes。
若要擷取 DSC 資源的語法,請使用 Get-DSCResource Cmdlet 加上 Syntax 參數。 此用法類似使用 Get-Command 加上 Syntax 參數,以取得 Cmdlet 語法。 您看到的輸出將顯示針對您指定之資源的資源區塊所使用的範本。
Get-DscResource -Syntax Service
雖然此資源的語法未來可能變更,但您看到的輸出應該類似下列輸出。 如同 Cmdlet 語法,方括弧中的「索引鍵」均為選擇性。 類型會指定每個索引鍵所預期的資料類型。
注意
Ensure 索引鍵是選擇性的,因為它會預設為 "Present"。
Service [String] #ResourceName
{
Name = [string]
[BuiltInAccount = [string]{ LocalService | LocalSystem | NetworkService }]
[Credential = [PSCredential]]
[Dependencies = [string[]]]
[DependsOn = [string[]]]
[Description = [string]]
[DisplayName = [string]]
[Ensure = [string]{ Absent | Present }]
[Path = [string]]
[PsDscRunAsCredential = [PSCredential]]
[StartupType = [string]{ Automatic | Disabled | Manual }]
[State = [string]{ Running | Stopped }]
}
注意
在 7.0 版以前的 PowerShell 中,Get-DscResource
不會尋找類別型的 DSC 資源。
在設定內部,服務資源區塊可能看起來像這樣,以確保多工緩衝處理器服務正在執行。
注意
在設定中使用資源之前,您必須先使用 Import-DSCResource 來匯入該資源。
Configuration TestConfig
{
# It is best practice to always directly import resources, even if the
# resource is a built-in resource.
Import-DSCResource -Name Service
Node localhost
{
# The name of this resource block, can be anything you choose, as l
# ong as it is of type [String] as indicated by the schema.
Service "Spooler - Running"
{
Name = "Spooler"
State = "Running"
}
}
}
設定可以包含相同資源類型的多個執行個體。 每個執行個體都必須具有唯一名稱。 在下列範例中,會新增第二個服務資源區塊來設定 "DHCP" 服務。
Configuration TestConfig
{
# It is best practice to always directly import resources, even if the
# resource is a built-in resource.
Import-DSCResource -Name Service
Node localhost
{
# The name of this resource block, can be anything you choose, as
# long as it is of type [String] as indicated by the schema.
Service "Spooler - Running"
{
Name = "Spooler"
State = "Running"
}
# To configure a second service resource block, add another Service
# resource block and use a unique name.
Service "DHCP - Running"
{
Name = "DHCP"
State = "Running"
}
}
}
注意
從 PowerShell 5.0 開始,已針對 DSC 新增 IntelliSense。 此新功能可讓您使用 TAB 與 Ctr+空格鍵來自動完成機碼名稱。
資源類型
Windows 具有內建資源,而 Linux 則具有 OS 特定資源。 有適用於 跨節點相依性、套件管理資源,以及 社群擁有和維護資源的資源。 您可以使用上述步驟來判斷這些資源的語法以及其使用方式。 提供這些資源的頁面都已封存於參考下方。
Windows 內建資源
- 封存資源
- 環境資源
- 檔案資源
- 群組資源
- GroupSet 資源
- 記錄檔資源
- 封裝資源
- ProcessSet 資源
- 登錄資源
- 指令碼資源
- 服務資源
- ServiceSet 資源
- 使用者資源
- WindowsFeature 資源
- WindowsFeatureSet 資源
- WindowsOptionalFeature 資源
- WindowsOptionalFeatureSet 資源
- WindowsPackageCabResource 資源
- WindowsProcess 資源
跨節點相依性資源
套件管理資源
Linux 資源
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應