2014-10-29 87 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(他们肯定预计会出现很多错误)。