[英]How to upgrade windows service using PowerShell Desired State Configuration
Below is an example of my config, install works fine but if I replace '\\\\BuildMachine\\Output\\MyService.exe' with a newer version DSC fails with file in use errors. 以下是我的配置示例,安装可以正常运行,但是如果我将“ \\\\ BuildMachine \\ Output \\ MyService.exe”替换为较新的版本,DSC将失败,并出现文件使用错误。 What is the correct way to upgrade a windows service using DSC?
使用DSC升级Windows服务的正确方法是什么? Thanks.
谢谢。
Configuration ServiceTestConfiguration {
Import-DscResource -ModuleName PSDesiredStateConfiguration
Import-DscResource -ModuleName xPSDesiredStateConfiguration
Node localhost
{
File EnsureLatestServiceExist {
Ensure = 'Present'
Type = 'File'
Checksum = 'ModifiedDate'
SourcePath = '\\BuildMachine\Output\MyService.exe'
DestinationPath = 'c:\MyService\MyService.exe'
}
xService EnsureServiceStarted {
Ensure = 'Present'
DependsOn = '[File]EnsureLatestServiceExist'
Name = 'MyService'
DisplayName = 'My Service'
Description = 'My Service'
Path = 'c:\MyService\MyService.exe'
StartupType = 'Automatic'
State = 'Running'
}
}
}
I have not found a built-in method to accomplish this, but the Script resource lets you pretty much do anything. 我还没有找到一种内置的方法来完成此任务,但是Script资源使您几乎可以做任何事情。
Add a Script resource that checks to see if the remote (source) file was updated. 添加一个脚本资源,以检查远程(源)文件是否已更新。 If the remote file was updated, stop the service.
如果远程文件已更新,请停止该服务。 Make the File resource depend upon the Script resource so that it runs before the file copy.
使“文件”资源依赖于“脚本”资源,以便它在复制文件之前运行。 The Service resource will run last and start the service again.
服务资源将最后运行,然后再次启动服务。
Script StopServiceCheck
{
SetScript =
{
Stop-Service -Name ServiceName -Force
}
TestScript =
{
$LocalFile = "C:\Path\To\Local.exe"
$RemoteFile = "\\Path\To\Remote.exe"
#Returns false if the remote file is newer than the local file or use -eq
return ((Get-Item -Path $RemoteFile).LastWriteTime -le (Get-Item -Path $LocalFile).LastWriteTime)
}
GetScript =
{
$LocalFile = "C:\Path\To\Local.exe"
$RemoteFile = "\\Path\To\Remote.exe"
$return = @{Result = "Executables match"}
If ((Get-Item -Path $RemoteFile).LastWriteTime -gt (Get-Item -Path $LocalFile).LastWriteTime) { $return.Result = "Remote file is newer" }
return $return
}
}
开源PowerShell模块Carbon为此具有定制的DSC资源: http : //get-carbon.org/Carbon_Service.html
It's not one desired state, it's two desired states. 这不是一个期望的状态,而是两个期望的状态。
The first desired state is: The service is properly shut down and ready for maintenance . 所需的第一个状态是: 服务已正确关闭并可以进行维护 。
The second desired state is: The service is active and running using the latest version of the code . 第二个所需状态是: 服务处于活动状态,并且正在使用最新版本的代码运行 。
Write it as two scripts. 将其编写为两个脚本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.