about_DesiredStateConfiguration

適用於: Windows PowerShell 4.0, Windows PowerShell 5.0

主題

about_Desired_State_Configuration

簡短描述

提供 Windows PowerShell® 預期狀態組態 (DSC) 功能的簡介。

詳細描述

DSC 是 Windows PowerShell 的管理平台。它可部署及管理軟體服務的組態資料和這些服務執行所在的環境。DSC 提供一組 Windows PowerShell 語言擴充功能、新的 Cmdlet 與您可以用來以宣告方式指定如何設定軟體環境狀態的資源。

在 Windows PowerShell 4.0 中引進了這些運算子。

如需 DSC 的詳細資訊,請參閱 TechNet 文件庫中的<Windows PowerShell 預期狀態組態>,網址為 https://go.microsoft.com/fwlink/?LinkId=311940。

使用 DSC

若要使用 DSC 設定您的環境,請先使用 Configuration 關鍵字定義 Windows 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
             Requires = "[WindowsFeature]IIS"  # Use Requires for dependencies     
          }
       }
    }

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

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

這會在您指定路徑的每個節點產生 MOF 檔案。這些 MOF 檔案會指定每個節點所需的組態。接下來,請使用下列 Cmdlet 來剖析此組態 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 資源

DSC 提供下列內建資源集,您可以在組態指令碼中使用:Registry、Script、Archive、File、WindowsFeature、Package、Environment、Group、User、Log、Service 和 WindowsProcess。上述範例示範如何使用 File 和 WindowsFeature 資源。若要查看您搭配特定資源可以使用的所有屬性,請在 Windows PowerShell ISE 組態指令碼中,將游標放在資源中的關鍵字 (例如 File),按住 CTRL 鍵,同時按下空格鍵。

另請參閱

<Windows PowerShell 預期狀態組態>

(https://go.microsoft.com/fwlink/?LinkId=311940)