Last active
June 14, 2022 10:03
-
-
Save saxomoose/ec9b8862f4bffb2c3557c41bbe95bda9 to your computer and use it in GitHub Desktop.
hyper-v scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$working_directory = "C:\utilities\scripts\hyper-v\" | |
Set-Location -Path $working_directory | |
$s = Get-Date -Format o | ForEach-Object { $_ -replace ":", "." } | |
$timestamp = $s.Substring(0, $s.indexOf('.') + 6) # yyyy-MM-ddThh.mm.ss | |
$prefix = Read-Host "Prefix (default: mt)" | |
if ($prefix -eq '') { | |
$prefix = "mt" | |
} | |
$project = Read-Host "Project (default: lab)" | |
if ($project -eq '') { | |
$project = "lab" | |
} | |
$u_int_32 = Get-Random -Minimum ([UInt16]::MinValue) -Maximum ([UInt16]::MaxValue) # 2-byte unsigned integer represented in hexadecimal form | |
[UInt16]$u_int_16 = $u_int_32 | |
$suffix = '{0:x}' -f $u_int_16 | |
$vm_name = [string]::Format("{0}-{1}-{2}", $prefix, $project, $suffix) | |
$switch_name = Read-Host "Select virtual switch (default: Default Switch)" | |
if ($switch_name -eq '') { | |
$switch_name = "Default Switch" | |
} | |
$base_vhd_path = "D:\Hyper-V\Virtual Hard Disks\base-ubuntu-vm.vhdx" | |
$vhd_path = [string]::Format("D:\Hyper-V\Virtual Hard Disks\{0}.vhdx", $vm_name) | |
$memory_startup_bytes = 4GB | |
$ssh_user = "root" | |
Add-Content -Path log.txt -Value "VM name: $vm_name" | |
Add-Content -Path log.txt -Value "Timestamp: $timestamp" | |
# New ubuntu vm based on base-ubuntu-vm. | |
Add-Content -Path log.txt -Value "Copying base virtual hard disk..." | |
Copy-Item $base_vhd_path -Destination $vhd_path | |
Add-Content -Path log.txt -Value "Creating vm..." | |
New-VM -Name $vm_name -MemoryStartupBytes $memory_startup_bytes -BootDevice VHD -VHDPath $vhd_path -Generation 2 -SwitchName $switch_name | Add-Content log.txt -Encoding ASCII | |
Add-Content -Path log.txt -Value "Disabling secure boot..." | |
Set-VMFirmware -VMName $vm_name -EnableSecureBoot Off | |
Add-Content -Path log.txt -Value "Disabling dynamic memory..." | |
Set-VMMemory $vm_name -DynamicMemoryEnabled $false | |
Add-Content -Path log.txt -Value "Enabling nested virtualization..." | |
Set-VMProcessor -VMName $vm_name -ExposeVirtualizationExtensions $true | |
Add-Content -Path log.txt -Value "Setting intgration services..." | |
Enable-VMIntegrationService -Name "Shutdown", "Time Synchronization", "Exchange", "Heartbeat", "VSS", "Guest Service Interface" -VMName $vm_name | |
Disable-VMIntegrationService -Name "Time Synchronization", "VSS" -VMName $vm_name | |
Add-Content -Path log.txt -Value "Setting management options..." | |
Set-VM -Name $vm_name -AutomaticCheckpointsEnabled $false -AutomaticStartAction Nothing -AutomaticStopAction Save | |
Add-Content -Path log.txt -Value "Starting vm..." | |
Start-VM -Name $vm_name | |
if ($switch_name -eq "Default Switch") { | |
Add-Content -Path log.txt -Value "Waiting for vm to stabilize..." | |
Wait-VM $vm_name | |
Start-Sleep -Seconds 5 | |
} elseif ($switch_name -eq "NAT") { | |
Add-Content -Path log.txt -Value "Waiting for vm to stabilize..." | |
Wait-VM $vm_name | |
Add-Content -Path log.txt -Value "Waiting for NetworkManager to activate static profile..." | |
Start-Sleep -Seconds 60 | |
} | |
# Add-Content -Path log.txt -Value "Upgrading distro..." | |
# hvc ssh -oStrictHostKeyChecking=no -l $ssh_user $vm_name "apt-get update && apt-get upgrade -y" | Add-Content log.txt -Encoding ASCII | |
Add-Content -Path log.txt -Value "Setting hostname in vm" | |
$ssh_command_hostname = [string]::Format("sed -i s/base-ubuntu-vm/{0}/ /etc/hostname", $vm_name) | |
hvc ssh -oStrictHostKeyChecking=no -l $ssh_user $vm_name $ssh_command_hostname | Add-Content log.txt -Encoding ASCII | |
$ssh_command_hosts = [string]::Format("sed -i 's/127.0.1.1 base-ubuntu-vm/127.0.1.1 {0}/' /etc/hosts", $vm_name) | |
hvc ssh -l $ssh_user $vm_name $ssh_command_hosts | Add-Content log.txt -Encoding ASCII | |
Add-Content -Path log.txt -Value "Rebooting..." | |
hvc ssh -l $ssh_user $vm_name "reboot" | |
Add-Content -Path log.txt -Value "Waiting for vm to stabilize..." | |
Wait-VM $vm_name | Add-Content log.txt | |
Add-Content -Path log.txt -Value "vm ready." | |
Add-Content -Path log.txt -Value "___" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$working_directory = "C:\utilities\scripts\hyper-v\" | |
Set-Location -Path $working_directory | |
$vm_name = '' | |
while ($vm_name -eq '') { | |
$vm_name = Read-Host "VM name" | |
} | |
Add-Content -Path log.txt -Value "VM name: $vm_name" | |
Add-Content -Path log.txt -Value "Timestamp: $timestamp" | |
Add-Content -Path log.txt -Value "Stopping vm..." | |
Stop-VM -Name $vm_name | |
Add-Content -Path log.txt -Value "Deleting vhd..." | |
Get-VMHardDiskDrive -VMName $vm_name | Remove-Item | |
Get-VMHardDiskDrive -VMName $vm_name | Remove-VMHardDiskDrive | |
Add-Content -Path log.txt -Value "Deleting vm..." | |
Remove-VM -Name $vm_name -Force | |
Add-Content -Path log.txt -Value "___" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment