New-Service
建立新的 Windows 服務。
語法
New-Service
[-Name] <String>
[-BinaryPathName] <String>
[-DisplayName <String>]
[-Description <String>]
[-SecurityDescriptorSddl <String>]
[-StartupType <ServiceStartupType>]
[-Credential <PSCredential>]
[-DependsOn <String[]>]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Description
此 Cmdlet 只能在 Windows 平臺上使用。
New-Service
Cmdlet 會在登錄和服務資料庫中建立 Windows 服務的新專案。 新的服務需要一個在服務期間執行的可執行檔。
此 Cmdlet 的參數可讓您設定服務的顯示名稱、描述、啟動類型和相依性。
範例
範例 1:建立服務
New-Service -Name "TestService" -BinaryPathName 'C:\WINDOWS\System32\svchost.exe -k netsvcs'
此命令會建立名為TestService的服務。
範例 2:建立包含描述、啟動類型和顯示名稱的服務
$params = @{
Name = "TestService"
BinaryPathName = 'C:\WINDOWS\System32\svchost.exe -k netsvcs'
DependsOn = "NetLogon"
DisplayName = "Test Service"
StartupType = "Manual"
Description = "This is a test service."
}
New-Service @params
此命令會建立名為TestService的服務。 它會使用 New-Service
的參數來指定新服務的描述、啟動類型和顯示名稱。
範例 3:檢視新的服務
Get-CimInstance -ClassName Win32_Service -Filter "Name='testservice'"
ExitCode : 0
Name : testservice
ProcessId : 0
StartMode : Auto
State : Stopped
Status : OK
此命令會使用 Get-CimInstance
取得新服務的 Win32_Service 物件。 此物件包含啟動模式和服務描述。
範例 4:在建立時設定服務的 SecurityDescriptor。
本範例會新增所建立服務的 SecurityDescriptor。
$SDDL = "D:(A;;CCLCSWRPWPDTLOCRRC;;;SY)(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;BA)(A;;CCLCSWLOCRRC;;;SU)"
$params = @{
BinaryPathName = 'C:\WINDOWS\System32\svchost.exe -k netsvcs'
DependsOn = "NetLogon"
DisplayName = "Test Service"
StartupType = "Manual"
Description = "This is a test service."
SecurityDescriptorSddl = $SDDL
}
New-Service @params
SecurityDescriptor 會儲存在 $SDDLToSet
變數中。
SecurityDescriptorSddl 參數會使用 $SDDL
來設定新服務的 SecurityDescriptor。
參數
-BinaryPathName
指定服務的可執行檔案路徑。 這是必要參數。
服務二進位檔的完整路徑。 如果路徑包含空格,則必須加上引號,才能正確解譯。 例如,d:\my share\myservice.exe
應指定為 '"d:\my share\myservice.exe"'
。
路徑也可以包含自動啟動服務的自變數。 例如,'"d:\my share\myservice.exe" arg1 arg2'
。 這些自變數會傳遞至服務進入點。
如需詳細資訊,請參閱 CreateServiceW API 的 lpBinaryPathName 參數。
類型: | String |
別名: | Path |
Position: | 1 |
預設值: | None |
必要: | True |
接受管線輸入: | False |
接受萬用字元: | False |
-Confirm
在執行 Cmdlet 之前,提示您進行確認。
類型: | SwitchParameter |
別名: | cf |
Position: | Named |
預設值: | False |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
-Credential
指定服務使用的帳戶做為 服務登入帳戶。
輸入用戶名稱,例如 User01 或 Domain01\User01,或輸入 PSCredential 物件,例如 Get-Credential
Cmdlet 所產生的用戶名稱。 如果您輸入使用者名稱,此 Cmdlet 會提示您輸入密碼。
認證會儲存在 PSCredential 物件中,密碼會儲存為 secureString 。
注意
如需 SecureString 數據保護的詳細資訊,請參閱 SecureString 有多安全?。
類型: | PSCredential |
Position: | Named |
預設值: | None |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
-DependsOn
指定新服務相依的其他服務名稱。 若要輸入多個服務名稱,請使用逗號來分隔名稱。
類型: | String[] |
Position: | Named |
預設值: | None |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
-Description
指定服務的描述。
類型: | String |
Position: | Named |
預設值: | None |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
-DisplayName
指定服務的顯示名稱。
類型: | String |
Position: | Named |
預設值: | None |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
-Name
指定服務的名稱。 這是必要參數。
類型: | String |
別名: | ServiceName |
Position: | 0 |
預設值: | None |
必要: | True |
接受管線輸入: | False |
接受萬用字元: | False |
-SecurityDescriptorSddl
以 Sddl 格式指定服務的 SecurityDescriptor。
類型: | String |
別名: | sd |
Position: | Named |
預設值: | None |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
-StartupType
設定服務的啟動類型。 此參數可接受的值為:
- 自動 - 服務是在系統啟動時由作系統啟動或啟動。 如果自動啟動的服務相依於手動啟動的服務,系統啟動時也會自動啟動手動啟動服務。
- AutomaticDelayedStart - 在系統開機后不久啟動。
- 停用 - 服務已停用,且無法由使用者或應用程式啟動。
- InvalidValue - 不支援此值。 使用此值會導致錯誤。
- 手動 - 服務只會由使用者使用服務控制管理員或應用程式手動啟動。
預設值 自動。
類型: | ServiceStartupType |
接受的值: | Automatic, Manual, Disabled, AutomaticDelayedStart, InvalidValue |
Position: | Named |
預設值: | Automatic |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
-WhatIf
顯示 Cmdlet 執行時會發生什麼事。 Cmdlet 未執行。
類型: | SwitchParameter |
別名: | wi |
Position: | Named |
預設值: | False |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
輸入
None
您無法使用管線將物件傳送至此 Cmdlet。
輸出
這個 Cmdlet 會傳回代表新服務的物件。
備註
此 Cmdlet 僅適用於 Windows 平臺。
若要執行此 Cmdlet,請使用 [以系統管理員身分執行] 選項 啟動 PowerShell。