@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
(彼らは確かに、これには多くのエラーが予想されます)。