基于@大卫的建议,我结束了创建一个轮询函数来检测状态变化并返回到我的主脚本
首先,我需要找到什么终止状态代码在哪里(我需要完成我的循环,只要我检测到一个成功的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