2014-10-29 86 views
1

我剛剛完成了使用DSC將我們的產品發佈到Azure虛擬機的初始測試階段,特別是the commands described in this article(它們是Azure PowerShell SDK的一部分)。如何獲得有關Azure VM上DSC執行的反饋?

我可以使用PowerShell推送DSC配置,但由於此過程是自動化的,我想獲得有關配置過程如何進展的反饋。當我打電話給Update-AzureVM時,我覺得沒問題,但DSC配置是在異步後發生的,除非我登錄機器(或look at the updated Azure Portal which now shows this),否則我不知道如何進行。

如果配置失敗,我想讓我的自動過程失敗。如何從腳本中檢查配置的狀態並正常檢測成功或失敗?

+1

我認爲'GET-xDscOperation'可能是你在找什麼狀態? http://technet.microsoft.com/en-us/library/dn249926.aspx它基本上使用事件日誌來檢查狀態 – Paul 2014-10-29 14:09:01

+0

@Paul非常有趣..我想我需要運行這些命令對虛擬機遙遠的權利?我希望使用Azure SDK進行更自動化的操作。 – julealgon 2014-10-29 15:03:39

+0

你也應該可以在本地調用它,但基本上是。不確定Azure SDK是否有專門的cmdlet,但我對此表示懷疑。 – Paul 2014-10-29 15:10:47

回答

1

有幾種方法可以做到這一點。您可以調用基於REST的API,如我在最近的帖子here中所述。

你也可以使用Get-AzureVM鑽入值(就像解析REST響應),像這樣:

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

+0

非常好。有了這個,我假設我可以在讀取該字段的腳本中創建一個循環,並向調用者報告它的價值。可能的價值觀是否在某個地方被闡述?我需要至少知道終止狀態,以便我可以停止腳本並報告結果。 – julealgon 2014-11-02 00:49:34

0

基於@大衛的建議,我結束了創建一個輪詢函數來檢測狀態變化並返回到我的主腳本

首先,我需要找到什麼終止狀態代碼在哪裏(我需要完成我的循環,只要我檢測到一個成功的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(他們肯定預計會出現很多錯誤)。