简体   繁体   English

如何使用PowerShell所需状态配置升级Windows服务

[英]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.

相关问题 使用DSC(所需状态配置)安装Windows服务 - Install windows service using DSC (Desired State Configuration) 在PowerShell中为所需状态配置脚本返回IIS Windows身份验证提供程序 - Return IIS Windows Authentication Provider in PowerShell for Desired State Configuration script 是否可以延迟评估Windows PowerShell所需状态配置中的功能? - Is it possible to lazily evaluate features in Windows PowerShell Desired State Configuration? PowerShell所需的状态配置 - 如何 - PowerShell Desired State Configuration -WhatIf 使用Powershell所需的配置状态的App Pool高级设置 - App Pool advanced settings using Powershell Desired Configuration State 如何使用 Powershell DSC 为 Windows 服务设置自动启动配置 - How to setup automatic startup configuration for a windows service using Powershell DSC 如何在PowerShell期望状态配置中从配置文件和配置数据文件创建MOF? - How to create MOFs from Configuration File and Configuration Data File in PowerShell Desired State Configuration? 使用Powershell或Powershell所需状态配置将Binary Registry Key设置为GUID值 - Set Binary Registry Key to GUID Value using Powershell or Powershell Desired State Configuration DSC(所需状态配置) - DSC (Desired State Configuration) 所需状态配置 - Desired state configuration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM