Delete a service
Description
This example shows how you can use the Service
resource to ensure a service doesn't exist.
With Ensure set to Absent
and Name set to Service1
, the resource removes the Service1
service if it exists. If Service1
is running, the resource stops Service1
before removing it.
With Invoke-DscResource
This script shows how you can use the Service
resource with the Invoke-DscResource
cmdlet to
ensure the Service1
service doesn't exist.
[CmdletBinding()]
param()
begin {
$SharedParameters = @{
Name = 'Service'
ModuleName = 'PSDscResource'
Properties = @{
Name = 'Service1'
Ensure = 'Absent'
}
}
$NonGetProperties = @(
'Ensure'
)
}
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
}
}
With a Configuration
This snippet shows how you can define a Configuration
with a Service
resource block to ensure
the Service1
service doesn't exist.
Configuration Delete {
Import-DscResource -ModuleName 'PSDscResources'
Node localhost {
Service ExampleService {
Name = 'Service1'
Ensure = 'Absent'
}
}
}
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.