2016-02-16 25 views
2

我想做的事與DSC(期望狀態配置)很簡單的事情:DSC:如何停止並啓動Windows服務

停止Windows服務,部署文件,終於再次啓動該服務。因此,我有以下幾點:

Service ServicesStop 
{ 
    Name = "TheTestService" 
    State = "Stopped" 
} 

File CopyDeploymentBits 
{ 
    Ensure = "Present" 
    Type = "Directory" 
    Recurse = $true 
    SourcePath = $applicationPath 
    DestinationPath = $deployOutputPath 
} 

Service ServicesStart 
{ 
    Name = "TheTestService" 
    StartupType = "Automatic" 
    State = "Running" 
} 

但不幸的是這是行不通的,因爲它是不允許有在配置相同的名稱(NAME =「TheTestService」)兩次(爲什麼在這種情況下?這是完全有道理的)作爲一種解決方法,我試過這樣的事情

Configuration MyTestConfig { 
    Node $env:COMPUTERNAME { 
     Service ServicesStop 
     { 
      Name = "TheTestService" 
      State = "Stopped" 
     } 

     File CopyDeploymentBits 
     { 
      Ensure = "Present" 
      Type = "Directory" 
      Recurse = $true 
      SourcePath = $applicationPath 
      DestinationPath = $deployOutputPath 
     } 
    } 
} 

Configuration MyTestConfig2 { 
    Node $env:COMPUTERNAME { 
     Service ServicesStart 
     { 
      Name = "TheTestService" 
      StartupType = "Automatic" 
      State = "Running" 
     } 
    } 
} 

MyTestConfig 
MyTestConfig2 

看起來瘋了 - 但它的工作!

不幸的是,我沒有使用我使用它與微軟發佈管理,在這裏普通DSC,似乎「MyTestConfig2」不執行了(或遇到其他未在日誌中提到的錯誤) 。

如何在發佈管理的上下文中使用dsc實現這個簡單的場景?還是有更好的方式來做這樣的事情?

非常感謝您的幫助和意見!

+1

看到這個問題:http://stackoverflow.com/questions/35302781/how-to-upgrade-windows-service-using-powershell-desired-state-configuration –

+0

這是個壞消息!也許比使用廚師更好。我想要記住,它的解決方案可以很好地工作。但是非常感謝您的輸入和鏈接(如果鏈接被破壞,'解決方案'似乎是:編寫您自己的自定義DSC資源)。 – timtos

+0

@DanielMann - 也許你想發佈一個我可以接受的答案?你指出我正確的方向。 :) – timtos

回答

1

最簡單的方法是創建一個以Name和State爲關鍵字的服務資源。隨意延長這一簡單的服務資源(我將嘗試找到它時,我發現時間https://github.com/nanalakshmanan/nServiceManager

+0

感謝您的意見。看起來非常好。在Daniel Mann的帖子後,我想出了我剛發佈的簡化解決方案,因此我很樂意將這些信用給他,但您的答案至少是一個投票! :) 再次感謝。 – timtos

0

AFER丹尼爾·曼的文章中,我想出了這個最小的解決方案:

[DscResource()] 
class InstallStopCopyStartServiceResource { 
    [DscProperty(Key)] 
    [string]$ServiceName 

    [DscProperty(Mandatory)] 
    [string] $SourcePath 

    [DscProperty(Mandatory)] 
    [string] $DestinationPath 

    [void] Set() 
    { 
     $needsInstallation = $false; 

     $testService = Get-Service | Where-Object {$_.Name -eq $this.ServiceName} 
     if ($testService -eq $null) 
     { 
      $needsInstallation = $true; 
     } elseif ($testService.Status -eq "Running") 
     { 
      Stop-Service $this.ServiceName 
     } 

     # Due to stupid Copy-Item behavior we first delete all old files 
     # (https://social.technet.microsoft.com/Forums/office/en-US/20b9d259-90d9-4e51-a125-c0f3dafb498c/copyitem-not-overwriting-exising-files-but-creating-additional-subfolder?forum=winserverpowershell) 
     Remove-Item -Path $this.DestinationPath -Recurse -Force -ErrorAction SilentlyContinue 

     # Copy files 
     Copy-Item -Path $this.SourcePath -Destination $this.DestinationPath -Recurse -Force 

     if ($needsInstallation -eq $true) 
     { 
      # Install service 
      $param = 'C:\Windows\Microsoft.NET\Framework\v4.0.30319\installUtil ' + $this.DestinationPath + '\service.exe' 
      Invoke-Expression $param 
     } 

     # Start the service 
     Start-Service $this.ServiceName 

     # Configure service 
     Set-Service $this.ServiceName -StartupType "Automatic" 
    } 

    [bool] Test() 
    { 
     # Always perform all steps 
     return $false 
    } 

    [InstallStopCopyStartServiceResource] Get() 
    { 
     return $this 
    } 

} 
0

至少在我下面的作品最好的:

# Configure the Service 1st 
Service Servicewuauserv { 
    Name = 'wuauserv' 
    BuiltInAccount = 'LocalSystem' 
    State = 'Running' 
} 

然後:

# Ensure it is running 
ServiceSet wuauserv { 
    Name = 'wuauserv' 
    BuiltInAccount = 'LocalSystem' 
    State = 'Running' 
} 

是的,使它更復雜,但拆分似乎最適合一些服務。