TheRedMaverick
TheRedMaverick

Reputation: 37

How do I tell my script to go back to a certain point of the script?

Here is my script so far:

$BookTitle = Read-Host "Enter Book Title"

If ($BookTitle -eq "") 

    { 
     [System.Media.SystemSounds]::Beep.Play();
     Write-Host "Please enter a book title"
    }

Else 
    {
     Write-Host "" ; 
     Write-Host "Book title is: $BookTitle"
    }

What I want to do is if a user enters a book title that is null, I want the script to execute the error code block:

{ 
 [System.Media.SystemSounds]::Beep.Play();
 Write-Host "Please enter a book title"
} 

and jump back to the beginning of this particular segment:

$BookTitle = Read-Host "Enter Book Title"

Instead of continuing on with the rest of the script. I don't want it to jump to the beginning of the entire script because I plan on using this kind of logic frequently within the script.

I'm not sure how to do it. I've tried using loops instead like Do...While, Do...Until, While, but I keep getting infinite loops.

So my question is, what am I doing wrong? Should I be using loops instead of conditional statements? A mixture of both?

Upvotes: 2

Views: 8045

Answers (1)

walid toumi
walid toumi

Reputation: 2272

Do {   
   $BookTitle = read-host "enter booktitle.."
   [System.Media.SystemSounds]::Beep.Play(); 
} while ($BookTitle -eq "")

$BookTitle

Upvotes: 1

Related Questions