locked
Using Variables as continuation criteria RRS feed

  • General discussion

  • Hello guys

    I am just learning how to use variables as continuation criteria I can create a do statement that iterates itself until a web address pattern is identified using the following string

    Do {$input = Read-Host} While (!($input -like "www.*.*))

    However whenever I put any if conditions between Do and While they don't seem to apply and just read out in one do statement. Is the below string correct and if not could you please advise me as to what is wrong or needs to be done for the if conditions to apply to the do statement. 

    Do {$input = Read-Host "Your Homepage" if ($input -like "www.*.*) {$Furtherquery = $false} else {Write-Host -ForegroundColor Red "Please give a valid address." $FurtherQuery = $True}} While (!($FurtherQuery))


    Monday, March 12, 2018 1:48 PM

All replies

  • Please post code samples correctly.  What you have posted is unusable and unreadable.

    Use the code posting tool provided.


    \_(ツ)_/

    Monday, March 12, 2018 2:06 PM
  • Be aware that you cannot use notepad++ or many Unix compatible editors to create scripts.

    Use PowerShell ISE to create scripts until you learn how to correctly use the syntax.  The ISE editor will show you your mistakes immediately.


    \_(ツ)_/

    Monday, March 12, 2018 2:11 PM
  • Do {$URLInput = Read-Host "Enter URL"} Until ($URLInput -Like "www.*.*")

    You should probably listen to JRV and fix your post. If you're trying to endlessly use read-host until the input matches what you want, you could use something like the below:

    Monday, March 12, 2018 2:33 PM
  • $input is a reserved variable.


    \_(ツ)_/

    Monday, March 12, 2018 5:56 PM
  • To undersatand reserved variables see:

    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-6

    or

    help about_automatic_Variables

    You cannot assign to these variables as they  internal and "Read-Only".


    \_(ツ)_/

    Monday, March 12, 2018 7:04 PM