WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: domvalle@comcast.net on October 21, 2023, 08:26:41 AM

Title: Password Complexity Check
Post by: domvalle@comcast.net 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"
}
Title: Re: Password Complexity Check
Post by: td 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.
Title: Re: Password Complexity Check
Post by: td 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) 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
Title: Re: Password Complexity Check
Post by: stanl on October 22, 2023, 03:25:12 AM
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
Title: Re: Password Complexity Check
Post by: domvalle@comcast.net on October 22, 2023, 08:09:11 PM
Your sample works great, thanks much!