如何使用 PowerShell Desired State Configuration 升级 Windows 服务 [英] How to upgrade windows service using PowerShell Desired State Configuration

查看:57
本文介绍了如何使用 PowerShell Desired State Configuration 升级 Windows 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的配置示例,安装工作正常,但如果我用较新版本替换 '\\BuildMachine\Output\MyService.exe' DSC 会失败并显示文件使用错误.使用 DSC 升级 Windows 服务的正确方法是什么?谢谢.

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. What is the correct way to upgrade a windows service using DSC? 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.

添加一个脚本资源,用于检查远程(源)文件是否已更新.如果远程文件已更新,请停止该服务.使文件资源依赖于脚本资源,以便它在文件复制之前运行.服务资源将最后运行并再次启动服务.

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 Desired State Configuration 升级 Windows 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆