2016-02-16 105 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' 
} 

是的,使它更复杂,但拆分似乎最适合一些服务。

相关问题