Skipping part of code when a condition matches

Welcome Forums General PowerShell Q&A Skipping part of code when a condition matches

This topic contains 2 replies, has 2 voices, and was last updated by

YCE
 
Participant
3 years, 8 months ago.

  • Author
    Posts
  • #25530
    YCE

    Participant
    Points: 12
    Rank: Member

    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

    Member
    Points: 0
    Rank: Member

    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
    YCE

    Participant
    Points: 12
    Rank: Member

    Often the simplest solution proves best. Many thanks.

The topic ‘Skipping part of code when a condition matches’ is closed to new replies.