共用方式為


about_DesiredStateConfiguration

簡短描述

提供 PowerShell Desired 狀態設定 (DSC) 功能的簡短簡介。

詳細描述

DSC 是 PowerShell 中的管理平臺,可讓您部署和管理軟體服務的設定數據,以及管理這些服務執行的環境。

DSC 提供一組 PowerShell 語言延伸模組、新的 Cmdlet 和資源,可用來以宣告方式指定您要如何設定軟體環境的狀態。 它也提供維護及管理現有組態的方法。

DSC 是在 PowerShell 4.0 中引進的。

如需 DSC 的詳細資訊,請參閱 PowerShell Desired 狀態設定 Overview

使用類別開發 DSC 資源

從 PowerShell 5.0 開始,您可以使用類別來開發 DSC 資源。 如需詳細資訊,請參閱使用PowerShell類別about_Classes和撰寫自定義 DSC 資源。

使用 DSC

若要使用 DSC 來設定您的環境,請先使用 Configuration 關鍵詞來定義 PowerShell 腳本區塊,後面接著標識符,後面接著以大括弧分隔區塊的配對。 在組態區塊內,您可以定義節點區塊,以指定環境中每個節點(計算機)所需的組態狀態。 節點區塊開頭為 Node 關鍵詞,後面接著目標計算機的名稱,可以是變數。 在計算機名稱之後,請以大括弧分隔節點區塊。 在節點區塊內,您可以定義資源區塊來設定特定資源。 資源區塊會以資源的類型名稱開頭,後面接著您要為該區塊指定的標識符,後面接著分隔區塊的大括弧,如下列範例所示。

Configuration MyWebConfig {
    # Parameters are optional
    param ($MachineName, $WebsiteFilePath)
    # A Configuration block can have one or more Node blocks
    Node $MachineName
    {
        # Next, specify one or more resource blocks
        # WindowsFeature is one of the resources you can use in a Node block
        # This example ensures the Web Server (IIS) role is installed
        WindowsFeature IIS
        {
            # To ensure that the role is not installed, set Ensure to "Absent"
            Ensure = "Present"
            Name = "Web-Server" # Use the Name property from Get-WindowsFeature
        }

        # You can use the File resource to create files and folders
        # "WebDirectory" is the name you want to use to refer to this instance
        File WebDirectory
        {
            Ensure = "Present"  # You can also set Ensure to "Absent"
            Type = "Directory" # Default is "File"
            Recurse = $true
            SourcePath = $WebsiteFilePath
            DestinationPath = "C:\inetpub\wwwroot"

            # Ensure that the IIS block is successfully run first before
            # configuring this resource
            DependsOn = "[WindowsFeature]IIS"  # Use for dependencies
        }
    }
}

若要建立組態,請叫用 Configuration 區塊,就像叫用 PowerShell 函式一樣,傳入您可能已定義的任何預期參數(上述範例中有兩個)。 例如,在此情況下:

MyWebConfig -MachineName "TestMachine" -WebsiteFilePath `
  "\\filesrv\WebFiles" -OutputPath "C:\Windows\system32\temp"
# OutputPath is optional

這會在您指定的路徑上為每個節點產生MOF檔案。 這些MOF檔案會為每個節點指定所需的組態。 接下來,使用下列 Cmdlet 來剖析組態MOF檔案、傳送每個節點其對應的組態,並制定這些設定。 請注意,您不需要為類別型 DSC 資源建立個別的 MOF 檔案。

Start-DscConfiguration -Verbose -Wait -Path "C:\Windows\system32\temp"

使用 DSC 維護設定狀態

使用 DSC 時,組態是等冪的。 這表示如果您使用 DSC 多次制定相同的組態,產生的設定狀態一律會相同。 因此,如果您懷疑環境中的任何節點可能已偏離所需的設定狀態,您可以再次制定相同的 DSC 設定,使其回到所需的狀態。 您不需要修改組態腳本,只處理狀態偏離所需狀態的資源。

下列範例示範如何確認指定節點上設定的實際狀態是否偏離節點上頒布的最後一個 DSC 設定。 在此範例中,我們會檢查本機計算機的組態。

$session = New-CimSession -ComputerName "localhost"
Test-DscConfiguration -CimSession $session

內建 DSC 資源

您可以在群組態文稿中使用下列內建資源:

名稱 屬性
檔案 {DestinationPath, 屬性, 總和檢查碼, 內容...}
封存 {Destination, Path, Checksum, Credential...}
Environment {Name, DependsOn, Ensure, Path...}
群組 {GroupName, Credential, DependsOn, Description...}
記錄 {Message, DependsOn, PsDscRunAsCredential}
套件 {Name, Path, ProductId, Arguments...}
登錄 {Key, ValueName, DependsOn, Ensure...}
指令碼 {GetScript, SetScript, TestScript, Credential...}
服務 {Name, BuiltInAccount, Credential, Dependencies...}
User {UserName, DependsOn, Description, Disabled...}
WaitForAll {NodeName、ResourceName、DependsOn、PsDscRunAsC...}
WaitForAny {NodeName、ResourceName、DependsOn、PsDscRunAsC...}
WaitForSome {NodeCount, NodeName, ResourceName, DependsOn...}
WindowsFeature {Name, Credential, DependsOn, Ensure...}
WindowsOptionalFeature {Name, DependsOn, Ensure, LogLevel...}
WindowsProcess {Arguments, Path, Credential, DependsOn...}

若要取得系統上可用的 DSC 資源清單,請執行 Get-DscResource Cmdlet。

注意

在低於 7.0 的 PowerShell 版本中, Get-DscResource 找不到以類別為基礎的 DSC 資源。

本主題中的範例示範如何使用 File 和 WindowsFeature 資源。 若要查看您可以搭配資源使用的所有屬性,請在PowerShell ISE 的組態腳本內,將游標插入資源關鍵詞 (例如File)中,按住 CTRL+空格鍵

尋找更多資源

您可以下載、安裝及瞭解 PowerShell 和 DSC 使用者社群和 Microsoft 所建立的許多其他可用 DSC 資源。 請流覽 PowerShell 資源庫 來瀏覽並瞭解可用的 DSC 資源。

另請參閱