Hi
Sorry for bad writing English.Today after i wrote a Persian wiki about backup hyper-v . i interest to learn backup hyper-v Using PowerShell .i can do it , but i create approximately automatic code But there is some error.Please help me to fix these error.
1. when ask for input and i type wrong input it continue to scripts. For example when i select "elect specific Virtual machine for Backup" and then when it show "Please Type The VMName" and intentionally type wrong input , but is show error and run next line of code.i want if any body type wrong input , it ask again.
2. if you see problem report me to correct it.
for now these are my questions.
# Create Backup form You Hyper-v Vms
# WSB(Windows Server Backup)
$a= Read-Host "Enter Server you want to check WSB"
$WSB=Get-WindowsFeature -Computername $a -Name "Windows-Server-Backup"
if ( $WSB.Installed -eq "True" ) {
Write-host "Windows Backup is Installed"}
else {$a= Read-Host "Enter Server you want to add WSB"
Add-WindowsFeature -ComputerName $a -Name Windows-Server-Backup}
$WSBPolicy=New-WBPolicy
Write-Host "*****Menu*****
1- Show All Virtual Machine
2- Select All of Virtual machine for backup
3- Select specific Virtual mchine for Backup "
$Num=Read-Host "please Select Number"
switch ($Num)
{
1 { Get-WBVirtualMachine}
2 {$VirtualMachines = Get-WBVirtualMachine}
3 {$VirtualMachines = Read-Host "Please Type The VMName: " }
default {$VirtualMachines = Get-WBVirtualMachine}
}
Add-WBVirtualMachine -Policy $WSBPolicy -VirtualMachine $VirtualMachines
Write-Host "Where do you want to save backupfiles :
1- local Drivee
2- Remote Share Foler"
$Num1=Read-Host "please Select Number"
switch ($Num1)
{
1 { $BackupLocation = New-WBBackupTarget -VolumePath Read-Host "Please Typer Voulume With : in end for Example D:" }
2 { $BackupLocation = New-WBBackupTarget -NetworkPath Read-Host "Please Typer Network Path for Example\Server01\HypervVMs\ (Need Credential)" }
}
$lastexitcode
Add-WBBackupTarget -Policy $WSBPolicy -Target $BackupLocation
Set-WBSchedule -Policy $WSBPolicy -Schedule 20:00
Set-WBPolicy $WSBPolicy
Start-WBBackup -Policy $WSBPolicy
#EndWhenever you see a helpful reply, click on Vote As Helpful& click on
Mark As Answer if a post answers your question.