【问题标题】:PowerShell DSC Service Missing MembersPowerShell DSC 服务缺少成员
【发布时间】:2014-05-27 13:09:18
【问题描述】:

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

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

我在我的 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 吗?当我使用他们的文档列表中的属性但似乎不存在时,如何让它工作?

【问题讨论】:

    标签: powershell dsc


    【解决方案1】:

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

    关于 Ensure 属性的一些附注:

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

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

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

    【讨论】:

    • 谢谢@ravikanth!看起来 xService 资源正是我要寻找的。​​span>
    【解决方案2】:

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

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

    Get-DscResource -Name Service | ForEach-Object Properties
    

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

    【讨论】:

    • 这真是令人遗憾,他们显然不支持他们以前所做的全部属性。由于缺少这些属性,此资源对我的用例完全没用。
    【解决方案3】:

    文档似乎不适用于 PowerShell 4 和 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多