更新服务的 StartupType
说明
此示例演示如何使用 Service
资源来确保存在具有正确启动类型的服务。
将 “确保 ”设置为 Present
“ 名称 ” Service1
和“ 路径 ”未设置时,如果服务不存在,则资源将引发异常。
将 StartupType 设置为Manual
“启动类型”时,资源会将启动类型设置为Manual
如果Service1
服务存在,并且具有任何其他启动类型。
设置为“状态”Ignore
后,资源不会启动或停止Service1
服务。
使用 Invoke-DscResource
此脚本演示如何将 Service
资源与 Invoke-DscResource
cmdlet 配合使用,以确保 Service1
服务存在并具有 Manual
启动类型。
[CmdletBinding()]
param()
begin {
$SharedParameters = @{
Name = 'Service'
ModuleName = 'PSDscResource'
Properties = @{
Name = 'Service1'
Ensure = 'Present'
StartupType = 'Manual'
State = 'Ignore'
}
}
$NonGetProperties = @(
'Ensure'
'StartupType'
'State'
)
}
process {
$TestResult = Invoke-DscResource -Method Test @SharedParameters
if ($TestResult.InDesiredState) {
$QueryParameters = $SharedParameters.Clone()
foreach ($Property in $NonGetProperties) {
$QueryParameters.Properties.Remove($Property)
}
Invoke-DscResource -Method Get @QueryParameters
} else {
Invoke-DscResource -Method Set @SharedParameters
}
}
使用配置
此代码片段演示如何使用资源块定义一 Configuration
个 Service
,以确保 Service1
服务存在并具有 Manual
启动类型。
Configuration UpdateStartupType {
Import-DscResource -ModuleName 'PSDscResources'
Node localhost {
Service ExampleService {
Name = 'Service1'
Ensure = 'Present'
StartupType = 'Manual'
State = 'Ignore'
}
}
}
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈