Disabling a service in DSC

How do I disable a service in DSC?
I have written the following Configuration.

Configuration DisServiceMult
{
 param(
        [string[]]$ServiceNames
    )
 Import-DscResource -ModuleName PSDesiredStateConfiguration   
    Node $env:COMPUTERNAME
    {
    $ServiceName=$ServiceNames.Split(",")
    foreach($service in $ServiceName)
    {
    $name=$service+"DISABLE"
       Service $name
        {
           Name = $service
           StartupType = 'Disabled'
        }
    }
    }
}  
DisServiceMult -ServiceNames "Adobe Acrobat Update Service,Application Host Helper Service" -OutputPath "C:\Windows\System32\DisServiceMult" 
Start-DscConfiguration -Path "C:\WINDOWS\system32\DisServiceMult"  -Wait -Verbose -Force

Following errors occured-
#The following exception occurred while retrieving the type name hierarchy: "Not found ".
+ CategoryInfo : NotSpecified: (:slight_smile: , CimException
+ FullyQualifiedErrorId : CatchFromBaseGetTypeNameHierarchy
+ PSComputerName : *******
#The PowerShell DSC resource MSFT_ServiceResource threw one or more non-terminating errors while running the Test-TargetResource functionality. These
errors are logged to the ETW channel called Microsoft-Windows-DSC/Operational. Refer to this channel for more details.
+ CategoryInfo : InvalidOperation: (:slight_smile: , CimException
+ FullyQualifiedErrorId : NonTerminatingErrorFromProvider
+ PSComputerName : ********

Jumped in without realising what you were doing here, my bad!

@Tim Lane I am using the built in DSC Service resource only.
Since I have to disable 2 services that is why I have used different names for both.

Hey Shelly,

Once I looked properly I thought your code looked good. So I tested and it works for me as long as I reference the services by their proper name, not their Display Name. Try using “AppHostSvc” and “armsSvc” should be correct for the Adobe one.

Hey Tim,
Thank you for replying.
But it still doesnt work for me, and I get the following error-
PowerShell DSC resource MSFT_ServiceResource failed to execute Test-TargetResource functionality with error message: Cannot start and disable a
service.
The SendConfigurationApply function did not succeed.

Hmmmm, probably because the service is already running. Try specifying the state as ‘Stopped’.

Service $name
        {
           Name = $service
           State = 'Stopped'
           StartupType = 'Disabled'
        }

Tried that too, not working.

What error does it give you now Shelly?

It is working now. Thanks a lot.
I am trying to enable a service now, It is not getting enabled.

 Service $name
        {
           Name = $service
           State='Stopped'
           StartupType = 'Automatic'
        }

I don’t understand your problem, just look at the resource definition:

PS C:\Users\nem> Get-DscResource -Name service -Syntax
Service [String] #ResourceName
{
    Name = [string]
    [BuiltInAccount = [string]{ LocalService | LocalSystem | NetworkService }]
    [Credential = [PSCredential]]
    [Dependencies = [string[]]]
    [DependsOn = [string[]]]
    [Description = [string]]
    [DisplayName = [string]]
    [Ensure = [string]{ Absent | Present }]
    [Path = [string]]
    [PsDscRunAsCredential = [PSCredential]]
    [StartupType = [string]{ Automatic | Disabled | Manual }]
    [State = [string]{ Running | Stopped }]
}

@Sebastian I have seen the resource definition.
The problem is solved now.

I am not able to enable a service now.

Hi Shelly,
DSC won’t allow you to set a service to start automatically and leave it in the stopped state, it insists on starting the service. So if you specify State = ‘Stopped’ it will throw an error. If you omit this line it will enable the service and start it as well. You can enable a service and set it to start Manually, this will allow you to leave it in the stopped state if that’s what you want.

Yes Tim, I figured.
Thank you!