1

我刚刚完成了使用 DSC 将我们的产品自动发布到 Azure 虚拟机的初始测试阶段,特别是使用本文中描述的命令,这些命令是 Azure PowerShell SDK 的一部分。

我可以使用 PowerShell 很好地推送 DSC 配置,但由于此过程是自动化的,我想获得有关配置过程进展情况的反馈。当我打电话时Update-AzureVM,我得到了一个好的结果,但是 DSC 配置在那之后发生了,异步的,我不知道它是怎么回事,除非我登录到机器(或者查看现在显示这个的更新的 Azure 门户)。

如果配置失败,我希望我的自动化过程失败。如何从我的脚本检查配置状态并优雅地检测成功或失败?

4

3 回答 3

4

我们添加了一个新的 cmdlet Get-AzureVMDscExtensionStatus 来获取正在运行的 DSC 配置的状态

本文解释了相同的http://blogs.msdn.com/b/powershell/archive/2015/02/27/introducing-get-azurevmdscextensionstatus-cmdlet-for-azure-powershell-dsc-extension.aspx

于 2015-03-06T21:12:26.457 回答
1

有几种方法可以做到这一点。您可以调用基于 REST 的 API,如我在此处最近的一篇文章中所述。

您还可以使用 Get-AzureVM 来钻取值(就像解析 REST 响应一样),如下所示:

((Get-AzureVM -ServiceName "" -Name "").ResourceExtensionStatusList | Where-Object { $_.HandlerName -eq 'Microsoft.PowerShell.DSC' }).ExtensionSettingStatus.Status

于 2014-10-31T22:48:48.150 回答
0

根据@David 的建议,我最终创建了一个轮询函数来检测状态变化并向我的主脚本报告。

首先,我需要找到终止状态代码的位置(我需要在检测到成功的 DSC 操作或发生任何错误后立即完成循环)。

我深入研究了 VM 中 DSC 扩展使用的文件,以找到可能的状态代码并以此为基础。状态代码可以C:\Packages\Plugins\Microsoft.Powershell.DSC\1.4.0.0\bin\DscExtensionStatus.psm1在任何安装了 DSC 扩展的虚拟机中找到。以下是 DSC 扩展 1.4.0.0 版的状态代码:

$DSC_Status = @{
    Initializing = @{
        Code = 1
        Message = "Initializing DSC extension."
    }
    Completed = @{
        Code = 2
        Message = "DSC configuration was applied successfully." 
    }
    Enabled = @{
        Code = 3
        Message = "PowerShell DSC has been enabled." 
    }
    RebootingInstall = @{
        Code = 4
        Message = "Rebooting VM to complete installation."
    }
    RebootingDsc = @{
        Code = 5
        Message = "Rebooting VM to apply DSC configuration." 
    }
    Applying = @{
        Code = 6
        Message = "Applying DSC configuration to VM."
    }

    #
    # Errors
    #
    GenericError = 100; # The message for this error is provided by the specific exception

    InstallError = @{
        Code = 101
        Message = "The DSC Extension was not installed correctly, please check the logs on the VM."
    }
    WtrInstallError  = @{
        Code = 102
        Message = "WTR was not installed correctly, please check the logs on the VM."
    }
}

函数中的逻辑有些复杂,因为状态变化是持久的,即它们不是来自单个 DSC 操作,而是来自整个扩展本身。因此,我需要先选择状态,然后再尝试查找更新。我正在使用该timestamp字段来检测新状态。这是代码:

function Wait-AzureDSCExtensionJob
{
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory)]
        [string] $ServiceName,

        [int] $RefreshIntervalSeconds = 15
    )

    Begin 
    {
        $statusFormat = `
            @{Label = 'Timestamp'; Expression = {$_.TimestampUtc}},
            @{Label = 'Status'; Expression = {"$($_.Code) - $($_.Status)"}}, `
            @{Label = 'Message'; Expression = {$_.FormattedMessage.Message}}

        Write-Verbose 'Getting starting point status...'
        $previousStatus = Get-AzureDscStatus -ServiceName:$ServiceName
        Write-Verbose "Status obtained: $($previousStatus | Format-List $statusFormat | Out-String)"
        Write-Verbose 'This status will be used as the starting point for discovering new updates.'
    }
    Process
    {
        do
        {
            Write-Verbose "Waiting for the next check cycle. $RefreshIntervalSeconds seconds left..."
            Start-Sleep -Seconds:$RefreshIntervalSeconds

            $currentStatus = Get-AzureDscStatus -ServiceName:$ServiceName
            if ($previousStatus.TimestampUtc -eq $currentStatus.TimestampUtc)
            {
                Write-Verbose 'Status has not changed since the last check.'
                $statusUpdated = $false
            }
            else
            {
                Write-Verbose "Status updated: $($currentStatus | Format-List $statusFormat | Out-String)"
                $previousStatus = $currentStatus
                $statusUpdated = $true
            }

            # Script with default message codes for the DSC Extension:
            # On Target VM: "C:\Packages\Plugins\Microsoft.Powershell.DSC\1.4.0.0\bin\DscExtensionStatus.psm1"
        } until ($statusUpdated -and (($currentStatus.Code -eq 2) -or ($currentStatus.Code -ge 100)))
    }
    End 
    {
        switch ($currentStatus.Code)
        {
            2 {Write-Verbose 'Configuration finished successfully.'; break}
            default {throw "Configuration failed: $($currentStatus.Status)"}
        }
    }
}

function Get-AzureDscStatus
{
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory)]
        [string] $ServiceName
    )

    Begin
    {
        $vm = Get-AzureVM -ServiceName:$ServiceName
        $dscExtensionStatus = $vm.ResourceExtensionStatusList | where { $_.HandlerName -eq 'Microsoft.PowerShell.DSC' }
        if (-not $dscExtensionStatus) 
        {
            throw 'Could not find the PowerShell DSC Extension on the VM'
        }

        $dscExtensionStatus.ExtensionSettingStatus
    }
}

我还不是很精通 PowerShell,所以这可能看起来更好一些并且更容易阅读。不过,我希望它可以对和我一样情况的人有用。

2014 年 11 月 28 日更新:

Microsoft 已将 DSC 扩展更新到 1.5.0.0 版,我的功能坏了,它们真好。我的意思是……这并不是说更改响应代码是一项重大更改或类似的事情;)

以下是新的状态代码:

$DSC_Status = @{
    Success = @{
        Code = 1
        Message = 'DSC configuration was applied successfully.' 
    }
    Initializing = @{
        Code = 2
        Message = 'Initializing DSC extension.'
    }
    Enabled = @{
        Code = 3
        Message = 'PowerShell DSC has been enabled.' 
    }
    RebootingInstall = @{
        Code = 4
        Message = 'Rebooting VM to complete installation.'
    }
    RebootingDsc = @{
        Code = 5
        Message = 'Rebooting VM to apply DSC configuration.' 
    }
    Applying = @{
        Code = 6
        Message = 'Applying DSC configuration to VM.'
    }

    #
    # Errors
    #
    GenericError = 1000 # The message for this error is provided by the specific exception

    InstallError = @{
        Code = 1001
        Message = 'The DSC Extension was not installed correctly, please check the logs on the VM.'
    }
    WtrInstallError = @{
        Code = 1002
        Message = 'WTR was not installed correctly, please check the logs on the VM.'
    }
    OsVersionNotSupported = @{
        Code = 1003
        Message = 'The current OS version is not supported. The DSC Extension requires Windows Server 2012 or 2012 R2, or Windows 8.1.'
    }
}

出于某种原因,他们交换了代码,现在1成功了,而错误从 上升1001000(他们肯定希望这个代码有很多错误)。

于 2014-11-05T14:31:49.363 回答