Skipping part of code when a condition matches

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Jarek C. Jarek C. 1 year, 5 months ago.

  • Author
  • #25530
    Profile photo of Jarek C.
    Jarek C.

    Part of a script I'm working on uses Read-Host to get user password for an access account. Then it tests if it can use that account to gain access to a particular host. All was great till it was decided that the account should not exist on certain hosts. To deal with that I was thinking of giving whoever runs the script the option to say 'No' so when it get's detected the whole part of code that does the access test get's skipped. Not sure if this is the best solution but should be simple enough for whoever runs the script.
    I was expecting something like Case statemnt so I can say
    Select Case $pass
    Case "No" 'do nothing
    Case else
    run the test code
    How would I go about this sort of problem in Powershell?

  • #25533
    Profile photo of Dave Wyatt
    Dave Wyatt

    A simple IF statement should do fine here, though you could also use Switch if that's your preference (which is the PowerShell equivalent of Select Case).

    if ($pass -ne 'No')
        # Run the test code
  • #25534
    Profile photo of Jarek C.
    Jarek C.

    Often the simplest solution proves best. Many thanks.

You must be logged in to reply to this topic.