src/Public/Reboot-HypervVM.ps1
|
function Reboot-HyperVVM { [CmdletBinding()] param ( [Parameter(Mandatory = $true, Position = 0)] [string]$Name ) Begin { } Process { Try { Import-Module Hyper-V -ErrorAction Stop } Catch { Throw "Failed to import Hyper-V module - $_" } If (-Not (Get-VM $Name)) { Throw "Virtual Machine $Name not found" } Write-Host "Virtual Machine $Name found. Shutting down..." Try { Stop-VM $Name -Force -Verbose -ErrorAction Stop } Catch { Throw "Failed to shut down virtual machine $Name - $_" } Write-Host "$Name shut down. Waiting 30 seconds..." Start-Sleep 30 Write-Host "Starting virtual machine $Name..." Try { Start-VM $Name -Verbose -ErrorAction Stop } Catch { Throw "Failed to start virtual machine $Name - $_" } If ((Get-VM $Name).State -eq 'Running') { Write-Host "Virtual machine $Name started successfully" } Else { Throw "Failed to start virtual machine $Name" } } End { } } |