1

特にこの記事で説明されているコマンドを使用して、DSC を使用して Azure Virtual Machines への製品リリースを自動化するための初期テスト フェーズを完了しました。これらは Azure PowerShell SDK の一部です。

PowerShell を使用して DSC 構成をうまくプッシュできますが、このプロセスは自動化されているため、構成プロセスがどのように進行したかについてフィードバックを得たいと思いました。を呼び出すと、OK が表示されますが、DSC の構成はその後、非同期的に行われます。マシンにログインしない限り (または、現在これを示している更新された Azure ポータルを見Update-AzureVMない限り)、どのようになっているのかわかりません。

構成が失敗した場合、自動化されたプロセスを失敗させたいです。スクリプトから構成のステータスを確認し、成功または失敗を適切に検出するにはどうすればよいですか?

4

3 に答える 3

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は成功していますが、エラーは から まで増加し100ました1000(彼らは確かに、これには多くのエラーが予想されます)。

于 2014-11-05T14:31:49.363 に答える