2

我正在尝试在 PowerShell 中使用 DSC 来部署服务。根据Microsoft 文档,服务资源具有可以设置的以下属性:

  • 姓名
  • 确保
  • 内置帐户
  • 凭据
  • 取决于
  • 论据
  • 启动类型
  • 状态

我在我的 DSC 配置中定义了一个服务,但我得到了错误。

这是我的代码:

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
    }

}
}

这是我得到的错误:

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

我在 GitHub ( https://gist.github.com/grenade/7677021 ) 上遵循了这个示例,所以我知道我的代码走的是正确的道路。

我需要下载一些新版本的 DSC 吗?当我使用其文档列表的属性但似乎不存在时,如何使其工作?

4

3 回答 3

4

就像 Bartek 指出的那样,文档有问题!我想一旦他们完成所有与 DSC 相关的开发,就会更新。自 PowerShell 4.0 发布以来,它几乎每个月都在变化!:)

Ensure 属性的一些附注:

Ensure属性用于需要“创建”配置实体的场景。例如,我们“确保”安装了某个功能。这意味着,如果尚未安装,我们将安装它。

Ensure属性在内置服务资源中没有意义。他们没有创建服务。他们只关注服务是否处于特定状态和其他设置。因此,如果没有确保属性,您始终会查看当前服务配置的状态并根据需要制定新配置。

有一个xServiceEnsure资源可让您使用该属性创建服务。

于 2014-05-28T06:24:53.350 回答
1

看起来 MSDN 文档和示例都不正确。当我选中我的框时,我没有在此资源上看到 Ensure/Arguments/Status。

您可以使用Get-DscResourcecmdlet 检查系统上的可用属性:

Get-DscResource -Name Service | ForEach-Object Properties

我建议将其报告为连接页面上的文档错误(如果还没有)。

于 2014-05-27T13:43:29.987 回答
0

看起来文档不适用于 PowerShell 4 和 5。

电源外壳 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

电源外壳 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
于 2016-12-16T18:14:23.817 回答