Skip to content

Instantly share code, notes, and snippets.

@Vibhu2
Created October 6, 2022 18:32
Show Gist options
  • Save Vibhu2/bc790147b320493d22e61275c3e3eae2 to your computer and use it in GitHub Desktop.
Save Vibhu2/bc790147b320493d22e61275c3e3eae2 to your computer and use it in GitHub Desktop.
$Cleanup=Test-Path -Path "C:\HypervisorRestartLogs\*.txt"
if ($Cleanup -eq 'True') {
Remove-Item -Path C:\HypervisorRestartLogs\*.txt
}
# Creating Log files
New-Item C:\HypervisorRestartLogs -ItemType Directory -ErrorAction SilentlyContinue
New-Item C:\HypervisorRestartLogs\Logfiles.txt -ItemType File -ErrorAction SilentlyContinue
Get-Date | Out-File -FilePath C:\HypervisorRestartLogs\Logfiles.txt -Append
# Checking Presence of Hyper-V
Get-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V | Out-File -FilePath C:\HypervisorRestartLogs\Logfiles.txt -Append
#BCDEdit / enum
#Bcdedit / set hypervisorlaunchtype auto
# Logging Virtual Machines State
$MachinesState = Get-VM
$MachinesState | Out-File -FilePath C:\HypervisorRestartLogs\Logfiles.txt -Append
$RunningMachines= Get-VM | where-Object {$_.state -eq 'Running' -or $_.State -eq 'Starting'}
$RunningMachineNames=$RunningMachines.Name
$RunningMachineNames | Out-File -FilePath C:\HypervisorRestartLogs\Logfiles.txt -Append
$RunningMachineNames | Out-File -FilePath C:\HypervisorRestartLogs\RunningMachines.txt -Append
# Creating Snapshot of Running VM's Before savingtheir State.
#$RunningMachineNames | Checkpoint-VM -SnapshotName (Get-Date)
#$RunningMachineNames | Stop-VM -Save -Force
$RunningMachineNames | Stop-VM -Force
Get-Date | Out-File -FilePath C:\HypervisorRestartLogs\Logfiles.txt -Append
Get-VM | Out-File -FilePath C:\HypervisorRestartLogs\Logfiles.txt -Append
Start-Sleep -Seconds 60
Restart-Computer -Force
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment