【问题标题】:Hardening PowerShell Script for Automating Usage of VMs using PowerCLI强化 PowerShell 脚本以使用 PowerCLI 自动使用 VM
【发布时间】:2017-04-25 06:38:24
【问题描述】:

为了获得一个干净的环境来运行其他一些脚本,我需要在每次计划任务触发它时恢复 ESX 主机上的虚拟机。

可以通过运行实现还原:

Set-VM -VM $VMName -Snapshot "MySnapshot" -Confirm:$false

可以通过运行实现启动:

Start-VM -VM $VMName

可以通过运行实现停止:

Shutdown-VMGuest -VM $VMName -Confirm:$false

如何以更安全的方式处理此问题,例如,能够在恢复、启动或停止 VM 时处理错误,并在这些任务之一成功执行时获得返回?

我正在使用 PowerCLI 6.5.0。

【问题讨论】:

标签: powershell powercli


【解决方案1】:

您可以使用多种方法来实现这一点。 这里有两个例子:

  1. 使用 -ErrorVariable

    # Revert VM
    Set-VM -VM $VMName -Snapshot "MySnapshot" -Confirm:$false -ErrorVariable revertError
    
    If ($revertError)
    {
        Write-Host "An error occured while reverting snapshot !" -ForegroundColor Red
        Write-Host $revertError
    }
    Else
    {
        Write-Host "Successfully reverted to snapshot." -ForegroundColor Green
    }
    
    # Start VM
    Start-VM -VM $VMName -ErrorVariable startError
    
    If ($startError)
    {
        Write-Host "An error occured while starting VM :" -ForegroundColor Red
        Write-Host $startError
    }
    Else
    {
        Write-Host "Successfully started VM." -ForegroundColor Green
    }
    
    # Stop VM
    Shutdown-VMGuest -VM $VMName -Confirm:$false -ErrorVariable shutdownError
    
    If ($shutdownError)
    {
        Write-Host "An error occured while shutting down guest OS of VM :" -ForegroundColor Red
        Write-Host $shutdownError
    }
    Else
    {
        Write-Host "Successfully stopped VM." -ForegroundColor Green
    }
    
  2. 使用 @mark-wragg 提到的 Try/Catch

    # Revert VM
    Try
    {
        # Temporarily make all errors terminating
        $errorActionPreference = "Stop"
        Set-VM -VM $VMName -Snapshot "MySnapshot" -Confirm:$false
        Write-Host "Successfully reverted to snapshot." -ForegroundColor Green
    }
    Catch
    {
        Write-Host "An error occured while reverting snapshot !" -ForegroundColor Red
        Write-Host $_.Exception.Message
    }
    Finally
    {
        $errorActionPreference = "Continue"
    }
    
    # Start VM
    Try
    {
        # Temporarily make all errors terminating
        $errorActionPreference = "Stop"
        Start-VM -VM $VMName
        Write-Host "Successfully started VM." -ForegroundColor Green
    }
    Catch
    {
        Write-Host "An error occured while starting VM :" -ForegroundColor Red
        Write-Host $_.Exception.Message
    }
    Finally
    {
        $errorActionPreference = "Continue"
    }
    
    # Stop VM
    Try
    {
        # Temporarily make all errors terminating
        $errorActionPreference = "Stop"
        Shutdown-VMGuest -VM $VMName -Confirm:$false
        Write-Host "Successfully stopped VM." -ForegroundColor Green
    }
    Catch
    {
        Write-Host "An error occured while shutting down guest OS of VM :" -ForegroundColor Red
        Write-Host $_.Exception.Message
    }
    Finally
    {
        $errorActionPreference = "Continue"
    }
    

【讨论】:

    猜你喜欢
    • 2014-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-14
    • 2019-02-18
    • 2017-07-05
    • 1970-01-01
    相关资源
    最近更新 更多