Author Posts

October 7, 2015 at 2:25 am

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: (:) [], 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: (:) [], CimException
+ FullyQualifiedErrorId : NonTerminatingErrorFromProvider
+ PSComputerName : ********

October 7, 2015 at 3:42 am

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

October 7, 2015 at 4:17 am

@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.

October 7, 2015 at 5:37 am

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.

October 7, 2015 at 6:40 am

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.

October 7, 2015 at 6:56 am

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

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

October 7, 2015 at 8:47 am

Tried that too, not working.

October 7, 2015 at 9:41 am

What error does it give you now Shelly?

October 7, 2015 at 8:51 pm

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

October 7, 2015 at 9:09 pm

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

October 7, 2015 at 10:03 pm

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

I am not able to enable a service now.

October 8, 2015 at 2:55 am

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.

October 8, 2015 at 8:30 pm

Yes Tim, I figured.
Thank you!