创建新的 Windows 服务。

语法

New-Service [-Name] <string> [-BinaryPathName] <string> [-Credential <PSCredential>] [-DependsOn <string[]>] [-Description <string>] [-DisplayName <string>] [-StartupType {<Automatic> | <Manual> | <Disabled>}] [-Confirm] [-WhatIf] [<CommonParameters>]

说明

New-Service cmdlet 在注册表和服务数据库中为 Windows 服务创建一个新条目。新服务需要一个在该服务运行期间执行的可执行文件。

此 cmdlet 的参数用于设置该服务的显示名称、说明、启动类型和依赖项。

参数

-BinaryPathName <string>

指定此服务的可执行文件的路径。此参数是必需的。

是否为必需?

true

位置?

2

默认值

是否接受管道输入?

false

是否接受通配符?

false

-Credential <PSCredential>

指定有权执行此操作的用户帐户。键入用户名,例如“User01”或“Domain01\User01”。或者,输入 PSCredential 对象,例如来自 Get-Credential cmdlet 的对象。如果键入用户名,则将提示您输入密码。

是否为必需?

false

位置?

named

默认值

是否接受管道输入?

false

是否接受通配符?

false

-DependsOn <string[]>

指定新服务所依赖的其他服务的名称。要输入多个服务名称,请使用逗号分隔这些名称。

是否为必需?

false

位置?

named

默认值

是否接受管道输入?

false

是否接受通配符?

false

-Description <string>

指定服务的描述。

是否为必需?

false

位置?

named

默认值

是否接受管道输入?

false

是否接受通配符?

false

-DisplayName <string>

指定服务的显示名称。

是否为必需?

false

位置?

named

默认值

是否接受管道输入?

false

是否接受通配符?

false

-Name <string>

指定服务的名称。此参数是必需的。

是否为必需?

true

位置?

1

默认值

是否接受管道输入?

false

是否接受通配符?

false

-StartupType <ServiceStartMode>

设置服务的启动类型。默认值为“Automatic”。

有效值包括:

-- Manual:服务只能由用户(使用服务控制管理器)或应用程序手动启动。

-- Automatic:服务将由操作系统在系统启动时启动或在系统启动时已启动。如果自动启动的服务依赖于手动启动的服务,则手动启动的服务也会在系统启动时自动启动。

-- Disabled:服务被禁用,不能由用户或应用程序启动。

是否为必需?

false

位置?

named

默认值

Automatic

是否接受管道输入?

false

是否接受通配符?

false

-Confirm

在执行命令之前提示您进行确认。

是否为必需?

false

位置?

named

默认值

是否接受管道输入?

false

是否接受通配符?

false

-WhatIf

描述如果执行该命令会发生什么情况(无需实际执行该命令)。

是否为必需?

false

位置?

named

默认值

是否接受管道输入?

false

是否接受通配符?

false

<CommonParameters>

此 cmdlet 支持通用参数:-Verbose、-Debug、-ErrorAction、-ErrorVariable、-OutBuffer 和 -OutVariable。有关详细信息,请参阅 about_Commonparameters.

输入和输出

输入类型是指可通过管道传递给 cmdlet 的对象的类型。返回类型是指 Cmdlet 所返回对象的类型。

输入

None

不能通过管道将输入传递给此 cmdlet。

输出

System.ServiceProcess.ServiceController

New-Service 返回一个表示新服务的对象。

说明

要在 Windows Vista 以及 Windows 的更高版本上运行此 cmdlet,必须使用“以管理员身份运行”选项启动 Windows PowerShell。

示例 1

C:\PS>new-service -name TestService -binaryPathName "C:\WINDOWS\System32\svchost.exe -k netsvcs"

说明
-----------
此命令创建一个名为“TestService”的新服务。






示例 2

C:\PS>new-service -name TestService -path "C:\WINDOWS\System32\svchost.exe -k netsvcs" -dependson NetLogon -displayName "Test Service" -StartupType Manual -Description "This is a test service."

说明
-----------
此命令创建一个名为“TestService”的新服务。该命令使用 New-Service cmdlet 的参数指定新服务的说明、启动类型和显示名称。

为指定 BinaryPathName 参数,该命令使用了 Path 参数别名。您也可以使用“-bpn”。






示例 3

C:\PS>get-wmiobject win32_service -filter "name='testservice'"

ExitCode  : 0
Name      : testservice
ProcessId : 0
StartMode : Auto
State     : Stopped
Status    : OK

说明
-----------
此命令使用 Get-WmiObject cmdlet 为新服务获取 Win32_Service 对象。此对象包含启动模式和服务说明。






另请参阅




目录