[英]How to upgrade windows service using PowerShell Desired State Configuration
以下是我的配置示例,安装可以正常运行,但是如果我将“ \\\\ BuildMachine \\ Output \\ MyService.exe”替换为较新的版本,DSC将失败,并出现文件使用错误。 使用DSC升级Windows服务的正确方法是什么? 谢谢。
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'
}
}
}
我还没有找到一种内置的方法来完成此任务,但是Script资源使您几乎可以做任何事情。
添加一个脚本资源,以检查远程(源)文件是否已更新。 如果远程文件已更新,请停止该服务。 使“文件”资源依赖于“脚本”资源,以便它在复制文件之前运行。 服务资源将最后运行,然后再次启动服务。
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
这不是一个期望的状态,而是两个期望的状态。
所需的第一个状态是: 服务已正确关闭并可以进行维护 。
第二个所需状态是: 服务处于活动状态,并且正在使用最新版本的代码运行 。
将其编写为两个脚本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.