Search code examples
powershellpowercli

Hardening PowerShell Script for Automating Usage of VMs using PowerCLI


To get a clean environment to run some other scripts I need to revert a Virtual Machine on an ESX Host every time when a Scheduled Task triggers it.

Reverting can be achieved by running:

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

Starting can be achieved by running:

Start-VM -VM $VMName

Stopping can be achieved by running:

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

How can I handle this in a more secure way, e.g to be able to handle Errors when reverting, starting or stopping the VM and get a return if one of these Tasks were being executed successfully?

I am using PowerCLI 6.5.0.


Solution

  • You can use several methods to achieve this. Here are 2 examples :

    1. Using -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. Using Try/Catch as mentionned by @mark-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"
      }