【问题标题】:DSC: How to stop and start a windows serviceDSC:如何停止和启动 Windows 服务
【发布时间】:2016-02-16 14:33:03
【问题描述】:

我想用 DSC(Desired State Configuration)做一件非常简单的事情:

停止一个 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,我将它与 Microsoft 发布管理一起使用,在这里,'MyTestConfig2' 似乎不再执行(或其他未提及的错误在日志中)。

如何在发布管理的上下文中使用 dsc 实现这个简单的场景?或者有没有更好的方法来做这样的事情?

【问题讨论】:

  • 这是个坏消息!也许使用 Chef 会更好。我认为要记住它适用于他们的解决方案。但是非常感谢您的输入和链接(如果链接被破坏,“解决方案”似乎是:编写您自己的自定义 DSC 资源)。
  • @DanielMann - 也许您想发布一个我可以接受的答案?你给我指出了正确的方向。 :)

标签: ms-release-management dsc


【解决方案1】:

最简单的方法是创建一个以 Name 和 State 为 key 的服务资源。随意扩展这个简单的服务资源(我会在有时间的时候尝试使用它https://github.com/nanalakshmanan/nServiceManager

【讨论】:

  • 感谢您的意见。看起来非常好。在丹尼尔曼的帖子之后,我想出了我刚刚发布的简化解决方案,所以我很想把功劳归功于他,但你的回答至少是赞成票! :) 再次感谢。
【解决方案2】:

在 Daniel Mann 的帖子之后,我想出了这个最小的解决方案:

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

}

【讨论】:

    【解决方案3】:

    至少对我来说以下效果最好:

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

    然后:

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

    是的,让它变得更复杂,但拆分似乎最适合某些服务。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多