共用方式為


DSC Service 資源

適用於:Windows PowerShell 4.0、Windows PowerShell 5.x

Windows PowerShell 預期狀態設定 (DSC) 的 Service 資源提供了管理目標節點服務的機制。

注意

此 DSC 資源的本檔涵蓋 PowerShell 7.2 版之前隨附的版本。 PSDscResources模組包含 Microsoft 正式支援的全新和更新 DSC 資源。 PSDscResources 模組可從 PowerShell 資源庫中取得。

如需詳細資訊和更新的檔,請參閱 PSDscResources 參考檔

語法

Service [string] #ResourceName
{
    Name = [string]
    [ BuiltInAccount = [string] { LocalService | LocalSystem | NetworkService }  ]
    [ Credential = [PSCredential] ]
    [ StartupType = [string] { Automatic | Disabled | Manual }  ]
    [ State = [string] { Running | Stopped }  ]
    [ Dependencies = [string[]] ]
    [ Description = [string] ]
    [ DisplayName = [string] ]
    [ Path = [string] ]
    [ DependsOn = [string[]] ]
    [ Ensure = [string] { Absent | Present } ]
    [ PsDscRunAsCredential = [PSCredential] ]
}

屬性

屬性 描述
名稱 表示服務名稱。 請注意,有時候和顯示名稱不同。 您可以使用 Get-Service Cmdlet 取得服務清單及其目前狀態。
BuiltInAccount 表示用於服務的登入帳戶。 這個屬性所允許的值為:LocalServiceLocalSystemNetworkService
認證 表示執行服務的帳戶認證。 這個屬性與 BuiltinAccount 屬性不能同時使用。
StartupType 表示服務的啟動類型。 這個屬性所允許的值為:AutomaticDisabledManual
State 表示您要確保的服務狀態。 值如下:RunningStopped
相依性 服務應具備的相依性名稱陣列。
描述 表示目標服務的描述。
DisplayName 表示目標服務的顯示名稱。
路徑 表示新服務的二進位檔路徑。

通用屬性

屬性 描述
DependsOn 表示必須先執行另一個資源的設定,再設定這個資源。 例如,如果第一個想要執行的資源設定指令碼區塊識別碼是 ResourceName,而其類型是 ResourceType,則使用這個屬性的語法就是 DependsOn = "[ResourceType]ResourceName"
Ensure 表示系統上是否有目標服務。 請將此屬性設定為Absent 以確保目標服務不存在。 屬性設定為 Present,可確保目標服務存在。 預設值為 Present
PsDscRunAsCredential 設定用於執行整個資源的認證。

注意

已在 WMF 5.0 中新增 PsDscRunAsCredential 通用屬性,以允許在其他認證的內容中執行任何 DSC 資源。 如需詳細資訊,請參閱搭配 DSC 資源使用認證

範例

configuration ServiceTest
{
    Import-DscResource -ModuleName PSDesiredStateConfiguration

    Node localhost
    {

        Service ServiceExample
        {
            Name        = "TermService"
            StartupType = "Manual"
            State       = "Running"
        }
    }
}