即使需要重新启动/重新启动,如何强制 DSC 执行所有配置(包)

Jup*_*aol 6 windows powershell windows-server-2012 dsc

来自MSDN

RebootNodeIfNeeded:目标节点上的某些配置更改可能需要重新启动它才能应用更改。值为“true”时,此属性将立即重新启动节点,而不会发出警告。如果为“false”,则配置将完成,但必须手动重新启动节点以使更改生效。

所以我的理解是,即使需要重启,DSC 也应该运行所有配置

但在我的情况下,这不是真的,在安装包后,有时 DSC 被标记为重新启动并且 DSC 不运行其余的配置

我必须再次手动执行命令才能运行其余配置

Start-DscConfiguration -Wait -Force -Path .\SomePath
Run Code Online (Sandbox Code Playgroud)

我想强制 DSC 运行所有配置,然后通知我是否需要重新启动服务器

我如何配置包的示例

    LocalConfigurationManager
    {
        RebootNodeIfNeeded = $false
    }

   Package MVC3
    {
        Name = "Microsoft ASP.NET MVC 3"
        Ensure = "Present"
        Path = "$Env:SystemDrive\AspNetMVC3ToolsUpdateSetup.exe"
        ProductId = "DCDEC776-BADD-48B9-8F9A-DFF513C3D7FA"
        Arguments = "/q"
        DependsOn = "[WindowsFeature]IIS"
        Credential = $Credential
    }

   Package MVC4
    {
        Name = "Microsoft ASP.NET MVC 4 Runtime"
        Ensure = "Present"
        Path = "$Env:SystemDrive\AspNetMVC4Setup.exe"
        ProductId = "942CC691-5B98-42A3-8BC5-A246BA69D983"
        Arguments = "/q"
        DependsOn = "[Package]MVC3"
        Credential = $Credential
    }
Run Code Online (Sandbox Code Playgroud)

Jup*_*aol 1

我想出了这个解决方案

我想找到一种更好的方法来做到这一点。但无论如何它对我有用

我仍然相信 DSC 流程应该以某种方式通知我,而不仅仅是通过 Write-Verbose,因为在我的情况下,这个流程是作为我们持续集成流程的一部分启动的

[int]$maximumAttempts = 5
[int]$attempt = 0
[ValidateNotNull()][guid]$dscResTmp = [guid]::NewGuid()
[ValidateNotNullOrEmpty()][string]$dscResPathTmp = Join-Path $baseFolderPathTmp "$dscResTmp.log"

do
{
    [bool]$stopLoop = $false
    [int]$attempt = ++$attempt

    Start-DscConfiguration -Wait -Force -Path $folderPathTmp 4> $dscResPathTmp

    [string[]]$rebootServerCoincidences = Select-String -Pattern "reboot" -Path $dscResPathTmp

    if ($rebootServerCoincidences.Length -le 0)
    {
        [bool]$stopLoop = $true
    }
    else
    {
        Write-Warning ($rebootServerCoincidences -join [Environment]::NewLine)
    }
}
while($stopLoop -eq $false -and $attempt -le $maximumAttempts)

if ($stopLoop -eq $false)
{
    Write-Warning "Max attempts reached"
}
Run Code Online (Sandbox Code Playgroud)