1

I'm trying to validate some input in powershell. The user should enter a username for a new AD user. This username should be validated for any invalid characters (e.g !, $, ;, etc). Valid characters are a-z (upper and lower), 0-9, _, - and ,

Thanks for any help.

At the moment my code looks like this:

do{

    $input="notok"
    # Eingabe es Usernamen
    Write-Host "Bite den Usernamen eingeben"
    $Username = Read-Host


    if ($Username -notmatch "[a-zA-Z0-9_.-]") {
        Write-Host ""
        Write-Host "ACHTUNG: Benutzer $Username enthaelt ungueltige Zeichen (nur a-z, A-Z, 0-9, ., - und _ erlaubt), bitte die Eingabe wiederholen" -ForegroundColor Red
        Write-Host ""
        $input="notok"

    }else{

        $input="ok"

    }

}while($input -ne "ok")

2 Answers 2

2

You can use a parameter, and use the built in parameter validation:

Param(
  [Parameter(Mandatory=$false,ValueFromPipeline=$true)] 
  [ValidateScript({$_-match "^[a-zA-Z0-9_\-,]+$"})] 
  [String]
  $Username
)
Sign up to request clarification or add additional context in comments.

5 Comments

This is assuming that he is using a function which I agree with but your regex is only checking that any of those characters are contained, not that ALL characters match the expression. You need the one I posted.
[ValidateScript({$_-match "^[a-z0-9_.-]*$"})]
@Deadly-Bagel - it doesn't need to be a function, you can also use the parameter is it's just a straight-up .ps1 script
Huh, learn something new every day. You need the Mandatory parameter for it to prompt you halfway through the script though.
Using parameter validation is an elegant solution; the caveat is that, unfortunately, you do not get to control the error message that is output (from what I can tell), and that message is likely too technical for end users, because the raw script block is printed. Aside from that: in the case at hand, the simpler ValidatePattern attribute would suffice: [ValidatePattern('^[a-zA-Z0-9_.-]$')].
0

Try this:

do{

    $input="notok"
    # Eingabe es Usernamen
    Write-Host "Bite den Usernamen eingeben"
    $Username = Read-Host


    if ($Username -notmatch "^[a-z0-9_.-]*$") {
        Write-Host ""
        Write-Host "ACHTUNG: Benutzer $Username enthaelt ungueltige Zeichen (nur a-z, A-Z, 0-9, ., - und _ erlaubt), bitte die Eingabe wiederholen" -ForegroundColor Red
        Write-Host ""
        $input="notok"

    }else{

        $input="ok"

    }

}while($input -ne "ok")

4 Comments

No prob =) Though you should check out Campbell's answer if you are using a function, it's a much neater way of doing it. Of course if it's halfway through the script you'll have to stick with this.
Nice, but please add an explanation (the one you've already given in a comment on @campbell.rw's answer).
Regex isn't really my thing but what you had was just asking if any of those characters was in $Username. Using "^[a-z0-9_.-]*$" instead (most notably the ^ before and *$ after) is saying ALL characters must match.
Indeed, that's the correct explanation, which I was hoping you'd add to your answer, because not many people pay attention to comments. As it stands, the only difference between your code and the OP's is the addition of ^ and $, which - while crucial - is currently not obvious.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.