Author Posts

May 21, 2015 at 7:08 am

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?

May 21, 2015 at 7:50 am

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
}

May 21, 2015 at 8:00 am

Often the simplest solution proves best. Many thanks.