【问题标题】:How do I get feedback about DSC execution on an Azure VM?如何获得有关在 Azure VM 上执行 DSC 的反馈?
【发布时间】:2015-03-07 05:12:26
【问题描述】:

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

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

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

【问题讨论】:

  • 我认为Get-xDscOperation 可能是您要找的东西? technet.microsoft.com/en-us/library/dn249926.aspx 基本上是用事件日志来检查状态
  • @Paul 非常有趣.. 我想我需要对虚拟机远程运行这些命令,对吧?我希望使用 Azure SDK 实现一些更自动化的东西。
  • 您应该也可以在本地调用它,但基本上可以。不确定 Azure SDK 是否有专门的 cmdlet,但我有点怀疑。

标签: powershell azure azure-powershell dsc


【解决方案1】:

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

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

【讨论】:

  • 绝对完美,正是我需要的!感谢娜娜的更新。我有一种感觉,使用这个 cmdlet,我们的流程会更加可靠。
【解决方案2】:

有几种方法可以做到这一点。您可以按照我在最近的一篇文章here 中描述的那样调用基于 REST 的 API。

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

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

【讨论】:

  • 非常好。有了这个,我假设我可以在我的脚本中创建一个循环来读取该字段并将其值报告给调用者。可能的值是否记录在某处?我至少需要知道终止状态,以便我可以停止脚本并报告结果。
【解决方案3】:

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

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

我深入研究了 VM 中 DSC 扩展使用的文件,以查找可能的状态代码并以此为基础。可以在任何安装了 DSC 扩展的虚拟机中的 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,所以这可能看起来更好一些并且更容易阅读。不过,我希望它对和我有同样情况的人有用。

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 成功了,而错误从 100 上升到 1000(他们肯定希望这个代码会出现很多错误)。

【讨论】:

    猜你喜欢
    • 2020-05-06
    • 2021-01-11
    • 1970-01-01
    • 1970-01-01
    • 2020-09-01
    • 2022-01-18
    • 2021-12-21
    • 2022-11-09
    • 1970-01-01
    相关资源
    最近更新 更多