如何在Azure VM上获得有关DSC执行的反馈?

时间:2014-10-29 13:36:44

标签: powershell azure azure-powershell dsc

我刚刚完成了使用DSC自动将产品发布到Azure虚拟机的初始测试阶段,尤其是the commands described in this article,它们是Azure PowerShell SDK的一部分。

我可以使用PowerShell推动DSC配置,但由于这个过程是自动化的,我想获得有关配置过程如何进展的反馈。当我打电话给Update-AzureVM时,我得到了一个确定,但DSC配置发生在那之后,异步,除非我登录机器(或look at the updated Azure Portal which now shows this),否则我不知道它是怎么回事。

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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

有几种方法可以做到这一点。您可以像我在最近的帖子here中所描述的那样调用基于REST的API。

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

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

答案 2 :(得分:0)

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

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

我深入了解了VM中DSC扩展所使用的文件,以查找可能的状态代码并根据我的条件。可以在安装了DSC Extension的任何虚拟机中的C:\Packages\Plugins\Microsoft.Powershell.DSC\1.4.0.0\bin\DscExtensionStatus.psm1处找到状态代码。以下是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,所以这可能看起来更好一点,更容易阅读。尽管如此,我希望它可以对与我情况相同的人有用。

更新28/11/2014:

微软已经将DSC Extension更新到版本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成功了,而错误从100上升到1000(他们肯定期待这个错误很多)。