Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 如何获得有关Azure VM上DSC执行的反馈?_Powershell_Azure_Azure Powershell_Dsc - Fatal编程技术网

Powershell 如何获得有关Azure VM上DSC执行的反馈?

Powershell 如何获得有关Azure VM上DSC执行的反馈?,powershell,azure,azure-powershell,dsc,Powershell,Azure,Azure Powershell,Dsc,我刚刚完成了使用DSC将产品发布自动化到Azure虚拟机的初始测试阶段,特别是使用,这是Azure PowerShell SDK的一部分 我可以使用PowerShell推送DSC配置,但由于此过程是自动化的,因此我希望获得有关配置过程进展情况的反馈。当我调用Update AzureVM时,我得到了一个ok,但DSC配置随后异步进行,除非我登录到机器(或),否则我不知道它是如何进行的 如果配置失败,我希望自动化流程失败。如何从脚本中检查配置的状态并优雅地检测成功或失败?有几种方法可以做到这一点。您

我刚刚完成了使用DSC将产品发布自动化到Azure虚拟机的初始测试阶段,特别是使用,这是Azure PowerShell SDK的一部分

我可以使用PowerShell推送DSC配置,但由于此过程是自动化的,因此我希望获得有关配置过程进展情况的反馈。当我调用
Update AzureVM
时,我得到了一个ok,但DSC配置随后异步进行,除非我登录到机器(或),否则我不知道它是如何进行的


如果配置失败,我希望自动化流程失败。如何从脚本中检查配置的状态并优雅地检测成功或失败?

有几种方法可以做到这一点。您可以调用基于REST的API,正如我在最近的一篇文章中所描述的那样

您还可以使用Get-AzureVM钻取值(就像解析REST响应一样),如下所示:


((
Get-AzureVM-ServiceName”“-Name”“).ResourceExtensionStatusList | Where对象{$\uu.HandlerName-eq'Microsoft.PowerShell.DSC'})。ExtensionSettingStatus.Status

有几种方法可以做到这一点。您可以调用基于REST的API,正如我在最近的一篇文章中所描述的那样

您还可以使用Get-AzureVM钻取值(就像解析REST响应一样),如下所示:


((
Get-AzureVM-ServiceName”“-Name”“).ResourceExtensionStatusList | Where对象{$.HandlerName-eq'Microsoft.PowerShell.DSC')).ExtensionSettingStatus.Status

基于@David的建议,我最终创建了一个轮询函数来检测状态更改并向我的主脚本报告

首先,我需要找到终止状态代码的位置(我需要在检测到成功的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操作,而是来自整个扩展本身。因此,我需要先选择状态,然后尝试查找更新。我正在使用
时间戳
字段检测新状态。代码如下:

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日:

微软已经将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
(他们肯定期待这一次会有很多错误)。

根据@David的建议,我最终创建了一个轮询函数来检测状态变化并向我的主脚本报告

首先,我需要找到终止状态代码的位置(我需要在检测到成功的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操作,而是来自整个扩展本身。因此,我需要先选择状态,然后尝试查找更新。我正在使用
时间戳
字段检测新状态。代码如下:

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日:

微软已经将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
(他们肯定期待在这一次中出现很多错误)。

我们添加了一个新的cmdlet Get-AzureVMDscExtensionStatus以获取正在运行的DSC配置的状态


本文解释了相同的问题,我们添加了一个新的cmdlet Get-AzureVMDscExtensionStatus来获取正在运行的DSC配置的状态


这篇文章也解释了同样的问题

我认为
Get-xDscOperation
可能就是您想要的?它基本上是使用事件日志来检查status@Paul非常有趣。。我想我需要对VM远程运行这些命令,对吗?我希望使用Azure SDK实现更自动化的功能。你也可以在本地调用它,但基本上是可以的。不确定Azure SDK是否有专门的cmdlet,但我有点怀疑。我认为
Get-xDscOperation
可能是您要找的?它基本上是使用事件日志来检查status@Paul非常有趣。。我想我需要对VM远程运行这些命令,对吗?我希望