Set-Service

启动、停止和挂起服务并更改服务的属性。

语法

Set-Service
   [-Name] <String>
   [-DisplayName <String>]
   [-Credential <PSCredential>]
   [-Description <String>]
   [-StartupType <ServiceStartupType>]
   [-Status <String>]
   [-SecurityDescriptorSddl <String>]
   [-Force]
   [-PassThru]
   [-WhatIf]
   [-Confirm]
   [<CommonParameters>]
Set-Service
   [-InputObject] <ServiceController>
   [-DisplayName <String>]
   [-Credential <PSCredential>]
   [-Description <String>]
   [-StartupType <ServiceStartupType>]
   [-SecurityDescriptorSddl <String>]
   [-Status <String>]
   [-Force]
   [-PassThru]
   [-WhatIf]
   [-Confirm]
   [<CommonParameters>]

说明

此 cmdlet 仅在 Windows 平台上可用。

cmdlet Set-Service 更改服务的属性,例如 StatusDescriptionDisplayNameStartupTypeSet-Service 可以启动、停止、暂停或暂停服务。 若要标识服务,请输入其服务名称或提交服务对象。 或者,将服务名称或服务对象向下发送到 Set-Service管道。

示例

示例 1:更改显示名称

在此示例中,服务的显示名称已更改。 若要查看原始显示名称,请使用 Get-Service

Set-Service -Name LanmanWorkstation -DisplayName "LanMan Workstation"

Set-Service 使用 Name 参数指定服务的名称 LanmanWorkstationDisplayName 参数指定新的显示名称 LanMan Workstation

示例 2:更改服务的启动类型

此示例演示如何更改服务的启动类型。

Set-Service -Name BITS -StartupType Automatic
Get-Service BITS | Select-Object -Property Name, StartType, Status

Name  StartType   Status
----  ---------   ------
BITS  Automatic  Running

Set-Service 使用 Name 参数指定服务的名称 BITSStartupType 参数将服务设置为 Automatic

Get-Service 使用 Name 参数指定 BITS 服务,并将对象发送到管道。 Select-Object 使用 Property 参数来显示 BITS 服务的状态。

示例 3:更改服务的说明

此示例更改 BITS 服务的说明并显示结果。

使用 Get-CimInstance cmdlet 是因为它返回包含服务的 Description的 Win32_Service 对象。

Get-CimInstance Win32_Service -Filter 'Name = "BITS"'  | Format-List  Name, Description

Name        : BITS
Description : Transfers files in the background using idle network bandwidth. If the service is
              disabled, then any applications that depend on BITS, such as Windows Update or MSN
              Explorer, will be unable to automatically download programs and other information.

Set-Service -Name BITS -Description "Transfers files in the background using idle network bandwidth."
Get-CimInstance Win32_Service -Filter 'Name = "BITS"' | Format-List  Name, Description

Name        : BITS
Description : Transfers files in the background using idle network bandwidth.

Get-CimInstance 将对象向下发送到管道 Format-List ,并显示服务的名称和说明。 出于比较目的,命令在更新说明之前和之后运行。

Set-Service 使用 Name 参数指定 BITS 服务。 Description 参数指定服务说明的更新文本。

示例 4:启动服务

在此示例中,服务已启动。

Set-Service -Name WinRM -Status Running -PassThru

Status   Name               DisplayName
------   ----               -----------
Running  WinRM              Windows Remote Management (WS-Manag...

Set-Service 使用 Name 参数指定服务 WinRMStatus 参数使用值 Running 来启动服务。 PassThru 参数输出显示结果的 ServiceController 对象。

示例 5:挂起服务

此示例使用管道暂停服务。

Get-Service -Name Schedule | Set-Service -Status Paused

Get-Service 使用 Name 参数指定 Schedule 服务,并将对象向下发送管道。 Set-Service 使用 Status 参数将服务设置为 Paused

示例 6:停止服务

此示例使用变量来停止服务。

$S = Get-Service -Name Schedule
Set-Service -InputObject $S -Status Stopped

Get-Service 使用 Name 参数指定服务 Schedule。 对象存储在变量 $S中。 Set-Service 使用 InputObject 参数并指定存储 $S的对象。 Status 参数将服务设置为“已停止”。

示例 7:停止远程系统上的服务

此示例停止远程计算机上的服务。 有关详细信息,请参阅 Invoke-Command

$Cred = Get-Credential
$S = Get-Service -Name Schedule
Invoke-Command -ComputerName server01.contoso.com -Credential $Cred -ScriptBlock {
  Set-Service -InputObject $S -Status Stopped
}

Get-Credential 提示输入用户名和密码,并将凭据存储在 变量中 $CredGet-Service 使用 Name 参数指定 计划 服务。 对象存储在变量 $S中。

Invoke-Command 使用 ComputerName 参数指定远程计算机。 Credential 参数使用 $Cred 变量登录到计算机。 ScriptBlock 调用 Set-ServiceInputObject 参数指定存储$S的服务对象。 Status 参数将服务设置为“已停止”。

示例 8:更改服务的凭据

此示例更改用于管理服务的凭据。

$credential = Get-Credential
Set-Service -Name Schedule -Credential $credential

Get-Credential 提示输入用户名和密码,并将凭据存储在 变量中 $credentialSet-Service 使用 Name 参数指定 计划 服务。 Credential 参数使用 $credential 变量并更新计划服务。

示例 9:更改服务的 SecurityDescriptor

此示例更改服务的 SecurityDescriptor

$SDDL = "D:(A;;CCLCSWRPWPDTLOCRRC;;;SY)(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;BA)(A;;CCLCSWLOCRRC;;;SU)"
Set-Service -Name "BITS" -SecurityDescriptorSddl $SDDL

SecurityDescriptor 存储在 变量中$SDDLSet-Service 使用 Name 参数指定 BITS 服务。 SecurityDescriptorSddl 参数使用 $SDDL 更改 BITS 服务的 SecurityDescriptor

参数

-Confirm

在运行 Set-Service之前提示你进行确认。

Type:SwitchParameter
Aliases:cf
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Credential

指定服务用作 服务登录帐户的帐户

键入用户名,例如 User01Domain01\User01,或输入 PSCredential 对象,例如 cmdlet Get-Credential 生成的对象。 键入用户名时,此 cmdlet 会提示输入密码。

凭据存储在 PSCredential 对象中,密码存储为 SecureString

注意

有关 SecureString 数据保护的详细信息,请参阅 SecureString 的安全性如何?

此参数是在 PowerShell 6.0 中引入的。

Type:PSCredential
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Description

指定服务的新说明。

服务说明显示在 “计算机管理”“服务”中。 Description 不是 ServiceController 对象的属性Get-Service。 若要查看服务说明,请使用 Get-CimInstance 返回表示服务的 Win32_Service 对象。

Type:String
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-DisplayName

指定服务的新显示名称。

Type:String
Aliases:DN
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Force

指定服务的停止模式。 此参数仅在使用 时 -Status Stopped 有效。 如果启用, Set-Service 则会在目标服务停止之前停止依赖服务。 默认情况下,当其他正在运行的服务依赖于目标服务时,将引发异常。

Type:SwitchParameter
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-InputObject

指定一个 ServiceController 对象,该对象表示要更改的服务。 输入包含 对象的变量,或键入获取对象的命令或表达式,例如 Get-Service 命令。 可以使用管道将服务对象发送到 Set-Service

Type:ServiceController
Position:0
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-Name

指定要更改的服务的服务名称。 不允许使用通配符。 可以使用管道将服务名称发送到 Set-Service

Type:String
Aliases:ServiceName, SN
Position:0
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-PassThru

返回一个 ServiceController 对象,该对象表示已更改的服务。 默认情况下, Set-Service 不会生成任何输出。

Type:SwitchParameter
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-SecurityDescriptorSddl

Sddl 格式指定服务的 SecurityDescriptor

Type:String
Aliases:sd
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-StartupType

指定服务的启动模式。

此参数的可接受值如下所示:

  • 自动 - 服务在系统启动时由操作系统启动或启动。 如果一个自动启动的服务依赖于手动启动的服务,则该手动启动的服务也会在系统启动时自动启动。
  • AutomaticDelayedStart - 在系统启动后不久启动。
  • 已禁用 - 服务已禁用,用户或应用程序无法启动。
  • InvalidValue - 不起作用。 cmdlet 不会返回错误,但服务的 StartupType 不会更改。
  • 手动 - 服务仅由用户、使用服务控制管理器或应用程序手动启动。
Type:ServiceStartupType
Aliases:StartMode, SM, ST, StartType
Accepted values:Automatic, AutomaticDelayedStart, Disabled, InvalidValue, Manual
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Status

指定服务的状态。

此参数的可接受值如下所示:

  • 已暂停。 挂起服务。
  • 正在运行。 启动服务。
  • 已停止。 停止服务。
Type:String
Accepted values:Paused, Running, Stopped
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-WhatIf

显示运行后 Set-Service 会发生什么情况。 cmdlet 未运行。

Type:SwitchParameter
Aliases:wi
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

输入

System.ServiceProcess.ServiceController, System.String

可以使用管道将服务对象或包含服务名称的字符串发送到 Set-Service

输出

ServiceController

默认情况下, Set-Service 不返回任何对象。 使用 PassThru 参数输出 ServiceController 对象。

备注

此 cmdlet 仅在 Windows 平台上可用。

Set-Service 需要提升的权限。 使用 “以管理员身份运行” 选项。

Set-Service 仅当当前用户有权管理服务时,才能控制服务。 如果命令无法正常工作,则可能没有所需的权限。

若要查找服务的服务名称或显示名称,请使用 Get-Service。 服务名称位于 “名称” 列中,显示名称位于 DisplayName 列中。