viewpoint-particle

Author Topic: Password Complexity Check  (Read 449 times)

domvalle@comcast.net

  • Newbie
  • *
  • Posts: 23
Password Complexity Check
« on: October 21, 2023, 08:26:41 am »
Found this PS script to check Password Complexity.
Very short but works nicely.
Would like to duplicate in WB but cannot find correct functions to use.
...Or maybe call from WB?
here is the code, thanks!

$Input = Read-Host "Please enter your password. `nPassword must meet complexity requirements:
`nAt least one upper case English letter [A-Z]`nAt least one lower case English letter [a-z]`nAt least one digit [0-9]`nAt least one special character (!,@,#,%,^,&,$)`nMinimum 7 in length."

if(($input -cmatch '[a-z]') -and ($input -cmatch '[A-Z]') -and ($input -match '\d') -and ($input.length -ge 7) -and ($input -match '!|@|#|%|^|&|$'))
{
    Write-Output "$input is valid password"
}
else
{
    Write-Output "$input is Invalid password"
}

td

  • Tech Support
  • *****
  • Posts: 4444
    • WinBatch
Re: Password Complexity Check
« Reply #1 on: October 21, 2023, 08:51:13 am »
You can use one or more WinBatch string functions to duplicate the PS business or call the  .Net regular expression class by using the WinBatch CLR hosting subsystem. And yes, you could call a PS script using CLR hosting if you think you must but there are more direct approaches.  .Net regex examples can be found in the Tech Database and the WinBatch string functions are all documented in the Consolidated WIL help file and on-line. The links to the Tech Database and the online version of help are in the menu bar at the top of this forum.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

  • Tech Support
  • *****
  • Posts: 4444
    • WinBatch
Re: Password Complexity Check
« Reply #2 on: October 21, 2023, 11:14:44 pm »
No doubt that there are better approaches but here is one example off-the-top:
Code: Winbatch
pwd ="@Topsecret1"
Uletters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Digits = "012456789"
Lletters = StrLower(Uletters)
Special = "!@#%%^&$"
if StrLen(pwd) > 6&&StrScan(pwd,Uletters,1,@Fwdscan)&&StrScan(pwd,Digits,1,@Fwdscan)&&StrScan(pwd,Lletters,1,@Fwdscan)&&StrScan(pwd,Special,1,@Fwdscan)
   Text = "Password OK"
else
   Text = "Password invalid"
endif
Message("Password Check", Text)
exit
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

  • Pundit
  • *****
  • Posts: 1849
Re: Password Complexity Check
« Reply #3 on: October 22, 2023, 03:25:12 am »
This simple CLR code seems to generate passwords that meet the criteria
Code: Winbatch

;simple password generator with CLR
;length and nonchars can vary
length = 15
nonchars = 4
ObjectClrOption("useany", "System.Web")
oSecure = ObjectClrNew("System.Web.Security.Membership")
password = oSecure.GeneratePassword(length,nonchars)
Message("Password Created",password)
Exit
 

domvalle@comcast.net

  • Newbie
  • *
  • Posts: 23
Re: Password Complexity Check
« Reply #4 on: October 22, 2023, 08:09:11 pm »
Your sample works great, thanks much!