Update a service's StartupType

Description

This example shows how you can use the Service resource to ensure a service exists with the correct startup type.

With Ensure set to Present, Name set to Service1, and Path not set, the resource throws an exception if the service doesn't exist.

With StartupType set to Manual, the resource sets the startup type to Manual if the Service1 service exists and has any other startup type.

With State set to Ignore, the resource doesn't start or stop the Service1 service.

With Invoke-DscResource

This script shows how you can use the Service resource with the Invoke-DscResource cmdlet to ensure the Service1 service exists and has the Manual startup type.

[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
    }
}

With a Configuration

This snippet shows how you can define a Configuration with a Service resource block to ensure the Service1 service exists and has the Manual startup type.

Configuration UpdateStartupType {
    Import-DscResource -ModuleName 'PSDscResources'

    Node localhost {
        Service ExampleService {
            Name        = 'Service1'
            Ensure      = 'Present'
            StartupType = 'Manual'
            State       = 'Ignore'
        }
    }
}