I am a bit of a rookie developer so bear with me. I am creating a small script to manage VMs in Hyper-V. I am using a switch statement within a do while loop for user selection. When I select "5", this is supposed to list the names of the current VMs within the terminal and sleep for 4 seconds. I am using the Get-VM cmdlet. When I select "5", it does not display anything. When I quit the program selecting "q" or "Q", it then displays the output after the program is finished running. Why could this be? I will provide my script below. Thanks!
function createVM {
New-VM -Name TestVM -MemoryStartupBytes 4GB -BootDevice VHD -VHDPath .\VMs\Test.vhdx -Path .\VMData -Generation 2
Write-Host "Creating VM..."
Start-Sleep -Seconds 4
}
function deleteVM {
Remove-VM -Name "TestVM" -Force
Write-Host "Removing VM..."
Start-Sleep -Seconds 4
}
function startVM() {
Start-VM -Name TestVM
Write-Host "Starting VM..."
Start-Sleep -Seconds 4
}
function stopVM() {
Stop-VM -Name TestVM -Force
Write-Host "Stopping VM..."
Start-Sleep -Seconds 4
}
function listVMs() {
Write-Host "Here is a list of your VMs:`n"
Get-VM | Select-Object -Property Name #This isn't working for some reason...
Start-Sleep -Seconds 4
}
function settingsVM() {
Write-Host "Here are some VM settings:`n"
Start-Sleep -Seconds 5
}
do {
Write-Host "`nHyper-V VM Manager`n"
Write-Host "Enter Selection:`n"
Write-Host "`t1 - Create a VM"
Write-Host "`t2 - Delete a VM"
Write-Host "`t3 - Start a VM"
Write-Host "`t4 - Stop a VM"
Write-Host "`t5 - List available VMs"
Write-Host "`t6 - List Settings of a VM"
$choice = Read-Host -Prompt "`nPlease enter a number (1-6) or 'Q/q' to quit the program"
switch($choice) {
'1' {
clear
createVM
break
}
'2' {
clear
deleteVM
break
}
'3' {
clear
startVM
break
}
'4' {
clear
stopVM
break
}
'5' {
clear
listVMs
break
}
'6' {
clear
settingVM
break
}
}
} while($choice -ne 'Q' -or $choice -ne 'q')
Write-Host "Quitting program: returning to shell.`n"
Write-Host "Have a wonderful day!`n"
Start-Sleep -Seconds 4
exit
I have tried using a regular elif statement to see if that solves the issue but it doesn't. I assume there is an issue within my loop.