我正在尝试在PowerShell中使用DSC来部署服务。根据{{3}},服务资源具有以下可以设置的属性:
我已在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(Microsoft Documentation)上关注了这个例子,所以我知道我的代码是正确的。
我需要下载一些新版本的DSC吗?当我使用他们的文档列表属性时,我如何使它工作,但似乎不存在?
答案 0 :(得分:3)
就像Bartek指出的那样,文档是错误的!我想一旦他们完成所有与DSC相关的开发,就会更新。自PowerShell 4.0发布以来,它几乎每个月都在变化! :)
关于确保财产的一些附注:
Ensure
属性用于您需要"创建"配置实体。例如,我们确保"已安装功能。这意味着,如果尚未安装,我们会安装它。
Ensure
属性在内置服务资源中没有意义。他们没有创建服务。他们只关注服务是否处于特定状态和其他设置。因此,如果没有确保属性,您始终会查看当前服务配置的状态,并根据需要制定新配置。
有xService资源可让您使用Ensure
属性创建服务。
答案 1 :(得分:1)
看起来MSDN文档和示例都不正确。当我检查我的盒子时,我没有看到此资源上的确认/参数/状态。
您可以使用Get-DscResource
cmdlet
Get-DscResource -Name Service | ForEach-Object Properties
我建议将其作为connect页面上的文档错误报告(如果还没有)。
答案 2 :(得分:0)
看起来文档不适用于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