2017-04-25 51 views
1

爲了讓一個乾淨的環境運行一些其他腳本,每當計劃任務觸發它時,我需要在ESX主機上還原虛擬機。強化PowerShell腳本,用於使用PowerCLI自動使用虛擬機

復歸可以通過運行來實現:可以實現

Start-VM -VM $VMName 

雪夜運行:

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

啓動可以通過運行來實現

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

我該如何處理這以一種更安全的方式,例如能夠在恢復,啓動或停止虛擬機並獲得時處理錯誤如果這些任務之一正在成功執行,則返回?

我正在使用PowerCLI 6.5.0。

+1

作爲mentionned看看概念'Try..Catch'如果你以前沒有。我在這裏寫了一篇關於它的博客文章:http://wragg.io/powershell-try-catch/ –

回答

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. 使用try/catch語句通過@標記wragg

    # 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" 
    } 
    
相關問題