Disabling a service in DSC

This topic contains 12 replies, has 3 voices, and was last updated by Profile photo of Shelly Tomar Shelly Tomar 1 year, 5 months ago.

  • Author
    Posts
  • #30484
    Profile photo of Shelly Tomar
    Shelly Tomar
    Participant

    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 : ********

  • #30486
    Profile photo of Tim E Lane
    Tim E Lane
    Participant

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

  • #30487
    Profile photo of Shelly Tomar
    Shelly Tomar
    Participant

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

  • #30492
    Profile photo of Tim E Lane
    Tim E Lane
    Participant

    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.

  • #30494
    Profile photo of Shelly Tomar
    Shelly Tomar
    Participant

    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.

  • #30495
    Profile photo of Tim E Lane
    Tim E Lane
    Participant

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

    Service $name
            {
               Name = $service
               State = 'Stopped'
               StartupType = 'Disabled'
            }
    
  • #30502
    Profile photo of Shelly Tomar
    Shelly Tomar
    Participant

    Tried that too, not working.

  • #30506
    Profile photo of Tim E Lane
    Tim E Lane
    Participant

    What error does it give you now Shelly?

  • #30539
    Profile photo of Shelly Tomar
    Shelly Tomar
    Participant

    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'
            }
    
  • #30541
    Profile photo of Sebastian Neumann
    Sebastian Neumann
    Participant

    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 }]
    }
    
  • #30545
    Profile photo of Shelly Tomar
    Shelly Tomar
    Participant

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

    I am not able to enable a service now.

  • #30547
    Profile photo of Tim E Lane
    Tim E Lane
    Participant

    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.

  • #30587
    Profile photo of Shelly Tomar
    Shelly Tomar
    Participant

    Yes Tim, I figured.
    Thank you!

You must be logged in to reply to this topic.