基於@大衛的建議,我結束了創建一個輪詢函數來檢測狀態變化並返回到我的主腳本
首先,我需要找到什麼終止狀態代碼在哪裏(我需要完成我的循環,只要我檢測到一個成功的DSC操作或如果有任何錯誤發生)
我在虛擬機中的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中很精通呢,所以這可能會看起來有點更好,更容易閱讀。儘管如此,我希望它能適用於和我一樣的人。
更新28/11/2014:
微軟已經更新了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
(他們肯定預計會出現很多錯誤)。
我認爲'GET-xDscOperation'可能是你在找什麼狀態? http://technet.microsoft.com/en-us/library/dn249926.aspx它基本上使用事件日誌來檢查狀態 – Paul 2014-10-29 14:09:01
@Paul非常有趣..我想我需要運行這些命令對虛擬機遙遠的權利?我希望使用Azure SDK進行更自動化的操作。 – julealgon 2014-10-29 15:03:39
你也應該可以在本地調用它,但基本上是。不確定Azure SDK是否有專門的cmdlet,但我對此表示懷疑。 – Paul 2014-10-29 15:10:47