Password Complexity Check

Started by domvalle@comcast.net, October 21, 2023, 08:26:41 AM

Previous topic - Next topic

domvalle@comcast.net

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

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

No doubt that there are better approaches but here is one example off-the-top:
Code (winbatch) Select
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

This simple CLR code seems to generate passwords that meet the criteria
Code (WINBATCH) Select


;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

Your sample works great, thanks much!