服务

摘要

管理 Windows 服务。

语法

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

说明

利用资源 Service ,可以添加、更新和删除服务。

要求

无。

密钥属性

名称

将服务名称指定为字符串。

注意

有时此值不同于显示名称。 可使用 Get-Service cmdlet 获取服务及其当前状态的列表。

Type: System.String

可选属性

BuiltInAccount

指定要将服务作为运行方式的计算机帐户的名称。 该帐户必须有权访问 Path 指定的可执行文件才能启动服务。

不要使用 Credential 属性指定此属性。

Type: System.String
Accepted Values:
  - LocalService
  - LocalSystem
  - NetworkService
Default Value: Null

凭据

指定要运行服务的帐户的凭据。 该帐户必须有权访问 Path 指定的可执行文件才能启动服务。 资源会自动授予此帐户“以服务身份登录”权限。

请勿使用 BuiltInAccount 属性指定此属性。

Type: System.Management.Automation.PSCredential
Default Value: None

依赖项

指定此服务需要作为字符串数组运行的服务的名称。

Type: System.String[]
Default Value: None

说明

将服务的说明指定为字符串。 服务说明将显示在 计算机管理、服务中。

Type: System.String
Default Value: None

DesktopInteract

指定服务是否可以在桌面上创建或与窗口通信。 如果未指定LocalSystem为 BuiltInAccount,则必须$false此属性。

默认值为 $false

Type: System.Boolean
Default Value: false

DisplayName

将服务的用户友好显示名称指定为字符串。

Type: System.String
Default Value: None

Ensure

指定服务是否应存在。 若要添加或更新服务,请将此属性设置为 Present。 若要删除服务,请将此属性设置为 Absent

默认值为 Present

Type: System.String
Accepted Values:
  - Absent
  - Present
Default Value: Present

路径

将服务的可执行文件的路径指定为字符串。 如果服务不存在,则需要此属性。

Type: System.String
Default Value: None

StartupTimeout

指定等待服务开始的时间(以毫秒为单位)。

默认值为 30000

Type: System.UInt32
Default Value: 30000

StartupType

指定服务在系统启动时的行为方式。 该值必须为下列值之一:

  • Automatic - 服务由系统启动时的操作系统启动。 如果自动启动的服务依赖于手动启动的服务,则系统启动时也会自动启动手动启动服务。
  • Disabled - 服务已禁用,用户或应用程序无法启动。
  • Manual - 服务仅由用户使用服务控制管理器或应用程序手动启动。

如果服务不存在,则默认值为 Automatic

Type: System.String
Behavior: Write
Accepted Values:
  - Automatic
  - Disabled
  - Manual
Default Value: None

州省/自治区/直辖市

指定服务是否应运行。 该值必须为下列值之一:

  • Running - 资源启动服务(如果尚未启动)。
  • Stopped - 如果服务正在运行,资源将停止该服务。
  • Ignore - 资源不会启动或停止服务。

默认值为 Running

Type: System.String
Accepted Values:
  - Running
  - Stopped
  - Ignore
Default Value: Running

TerminateTimeout

指定等待服务停止的时间(以毫秒为单位)。 默认值为 30000

Type: System.UInt32
Default Value: 30000

示例