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脚本以使用PowerCLI自动使用虚拟机_Powershell_Powercli - Fatal编程技术网

强化PowerShell脚本以使用PowerCLI自动使用虚拟机

强化PowerShell脚本以使用PowerCLI自动使用虚拟机,powershell,powercli,Powershell,Powercli,为了获得一个干净的环境来运行一些其他脚本,我需要在每次定时任务触发时恢复ESX主机上的虚拟机 可以通过运行以下命令来实现恢复: Set-VM -VM $VMName -Snapshot "MySnapshot" -Confirm:$false Start-VM -VM $VMName Shutdown-VMGuest -VM $VMName -Confirm:$false 可以通过运行以下各项来实现启动: Set-VM -VM $VMName -Snapshot "MySnapshot"

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

可以通过运行以下命令来实现恢复:

Set-VM -VM $VMName -Snapshot "MySnapshot" -Confirm:$false
Start-VM -VM $VMName
Shutdown-VMGuest -VM $VMName -Confirm:$false
可以通过运行以下各项来实现启动:

Set-VM -VM $VMName -Snapshot "MySnapshot" -Confirm:$false
Start-VM -VM $VMName
Shutdown-VMGuest -VM $VMName -Confirm:$false
可通过运行以下程序实现停止:

Set-VM -VM $VMName -Snapshot "MySnapshot" -Confirm:$false
Start-VM -VM $VMName
Shutdown-VMGuest -VM $VMName -Confirm:$false
如何以更安全的方式处理此问题,例如,在恢复、启动或停止VM时能够处理错误,并在成功执行其中一项任务时获得返回


我使用的是PowerCLI 6.5.0。

您可以使用多种方法来实现这一点。 以下是两个示例:

  • 使用-error变量

    # 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
    }
    
  • 使用@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"
    }
    

  • 如果您以前没有尝试过,请查看一下
    Try..Catch
    的概念。我在这里写了一篇关于它的博客: