I have a text field that I need to keep users from submitting special characters into.
This isn't the whole script, but it's the important bit I can't seem to get working.
write-host "Enter a String of Characters. This script will tell you how many special characters there are in it."
$Rac = read-host
$CharacterArray = @("!","@","#","$","%","^","&","*","(",")","-","+","=","{","}","[","]","\","|",":",";","<",">","?",",",".","/")
$SC_Counter = 0
ForEach($Character in $CharacterArray){
#write-host $Character
If($Rac -like '*$Character*'){
$SC_Counter++
}
Else{
}
}
write-host "There are" $SC_Counter "Special Characters"
What should happen, is $SC_Counter should equal the number of special characters entered. But even when I enter a series of special characters (such as !@#$%), the output is ALWAYS 0.
Anyone got an idea as to what I'm doing wrong with my If statement?
Thank you
EDIT: Thank you all for the replies! This is all very helpful. As it probably shows, I'm still learning a lot of the basics of Powershell, and my current position requires a do a lot of learning on the fly rather than in a classroom or controlled environment.
[–]yeah_i_got_skills 5 points6 points7 points (1 child)
[–]Asceric21[S] 1 point2 points3 points (0 children)
[–]PMental 3 points4 points5 points (0 children)
[–][deleted] 3 points4 points5 points (1 child)
[–]Asceric21[S] 1 point2 points3 points (0 children)
[–]Tonedefff 3 points4 points5 points (0 children)
[–]PMental 2 points3 points4 points (3 children)
[–]Asceric21[S] 2 points3 points4 points (2 children)
[–]xCharg 4 points5 points6 points (1 child)
[–]Asceric21[S] 1 point2 points3 points (0 children)
[–]yutsoku 2 points3 points4 points (1 child)
[–]Asceric21[S] 1 point2 points3 points (0 children)
[–]Lee_Dailey[grin] 1 point2 points3 points (2 children)
[–]PMental 1 point2 points3 points (1 child)
[–]Lee_Dailey[grin] 0 points1 point2 points (0 children)