簡體   English   中英

如何使用PowerShell所需狀態配置升級Windows服務

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM