How to upgrade windows service using PowerShell Desired State Configuration

江枫思渺然 提交于 2019-12-06 12:46:16

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
    }
}

Open source PowerShell module Carbon has custom DSC Resource for this very purpose: 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!