2
votes

I am trying to use DSC in PowerShell to deploy a service. According to the Microsoft Documentation, the service resource has the following properties that can be set:

  • Name
  • Ensure
  • BuiltInAccount
  • Credential
  • DependsOn
  • Arguments
  • StartupType
  • State

I have defined a service in my DSC configuration, but I get errors on it.

This is my code:

Configuration ServiceDeployConfig
{
param(
    [string[]]$ComputerName="localhost",

    [Parameter(Mandatory)]
    [ValidateNotNullOrEmpty()]
    [string] $serviceDeployPath,

    [Parameter(Mandatory)]
    [ValidateNotNullOrEmpty()]
    [string] $serviceName,

    [Parameter(Mandatory)]
    [ValidateNotNullOrEmpty()]
    [string] $serviceDisplayName,

    [Parameter(Mandatory)]
    [ValidateNotNullOrEmpty()]
    [string] $serviceExecutable,

    [Parameter(Mandatory)]
    [ValidateNotNullOrEmpty()]
    [string] $serviceUserame,

    [Parameter(Mandatory)]
    [ValidateNotNullOrEmpty()]
    [string] $servicePassword
)

Node $ComputerName
{
    File serviceFiles
    {
        Ensure = "Present"
        SourcePath = "\\Path\to\exe\$serviceExecutable"
        DestinationPath = $serviceDeployPath

    }

    Service serviceInstall 
    {
        Ensure = "Present"
        Name = $serviceName
        Credential = New-Object System.Management.Automation.PSCredential ($serviceUserame, (ConvertTo-SecureString $servicePassword -AsPlainText -Force))
        DependsOn = "[File]serviceFiles"
        Arguments = "-binaryPathName $serviceDeployPath\$serviceExecutable", "-displayName $serviceDisplayName"
        StartupType = Automatic
        Status = Start
    }

}
}

Here are the errors I get:

At line:43 char:13
+             Ensure = "Present"
+             ~~~~~~
The member 'Ensure' is not valid. Valid members are 'DependsOn', 'Name', 'State', 'StartupType', 'BuiltInAccount', 'Credential'. Please update your script and try again.
At line:47 char:13
+             Arguments = "-binaryPathName $serviceDeployPath\$serviceExecutable", ...
+             ~~~~~~~~~
The member 'Arguments' is not valid. Valid members are 'DependsOn', 'Name', 'State', 'StartupType', 'BuiltInAccount', 'Credential'. Please update your script and try again.
At line:49 char:13
+             Status = Start
+             ~~~~~~
The member 'Status' is not valid. Valid members are 'DependsOn', 'Name', 'State', 'StartupType', 'BuiltInAccount', 'Credential'. Please update your script and try again.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : InvalidInstanceProperty

I followed this example over on GitHub (https://gist.github.com/grenade/7677021), so I know I am going down the right path in regards to my code.

Is there some new version of DSC I need to download? How do I get it to work when I use the properties their documentation list, but which don't seem to exist?

3

3 Answers

3
votes

Like Bartek points out, the documentation is buggy! I guess there will update once they finish all DSC related development. It is changing almost every month since PowerShell 4.0 release! :)

Some side notes on Ensure property:

The Ensure property is used in scenarios where you need to "create" a configuration entity. For example, we "ensure" a feature is installed. Which means, we install it if it is not already installed.

Ensure property does not make sense in the built-in service resource. They are not creating a service. They are only looking at whether service is in a specific state and other settings. So, w/o a ensure property, you always look at the state of the current service configuration and enact the new configuration as required.

There is a xService resource that lets you create a service using the Ensure property.

1
votes

Looks like both MSDN documentation and example is not correct. When I check my box I don't see Ensure/ Arguments/ Status on this resource.

You can check available properties on your system using Get-DscResource cmdlet:

Get-DscResource -Name Service | ForEach-Object Properties

I would suggest reporting it as documentation bug on connect page (if its not there yet).

0
votes

Looks like the documentation does not properly apply to both PowerShell 4 and 5.

Powershell 4:

PS C:\> Get-DscResource -Name Service | ForEach-Object Properties

Name                          PropertyType                                    IsMandatory Values
----                          ------------                                    ----------- ------
Name                          [string]                                               True {}
BuiltInAccount                [string]                                              False {LocalService, LocalSystem...
Credential                    [PSCredential]                                        False {}
DependsOn                     [string[]]                                            False {}
StartupType                   [string]                                              False {Automatic, Disabled, Manual}
State                         [string]                                              False {Running, Stopped}


PS C:\> host


Name             : ConsoleHost
Version          : 4.0
InstanceId       : 9351e135-8e86-449c-b5d6-b9259c5d0966
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-US
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace

Powershell 5:

PS C:\> Get-DscResource -Name Service | ForEach-Object Properties

Name                 PropertyType   IsMandatory Values
----                 ------------   ----------- ------
Name                 [string]              True {}
BuiltInAccount       [string]             False {LocalService, LocalSystem, NetworkService}
Credential           [PSCredential]       False {}
Dependencies         [string[]]           False {}
DependsOn            [string[]]           False {}
Description          [string]             False {}
DisplayName          [string]             False {}
Ensure               [string]             False {Absent, Present}
Path                 [string]             False {}
PsDscRunAsCredential [PSCredential]       False {}
StartupType          [string]             False {Automatic, Disabled, Manual}
State                [string]             False {Running, Stopped}


PS C:\> host


Name             : ConsoleHost
Version          : 5.0.10240.17113
InstanceId       : bae26212-ea41-4d6f-a043-fdb1b0766283
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-US
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace