Advertisement
Nestor10

New-Password

Mar 15th, 2019
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function New-Password {
  2.     param([int]$Length=32, [int]$MinimumSpecialChars)
  3.    
  4.     # Handle super long password requests
  5.     if($Length -gt 128) {
  6.         if (!$MinimumSpecialChars) {
  7.             # Calculate minimum per 128-character segment
  8.             $MinimumSpecialChars = $Length/(6/($Length/128))
  9.         } else {
  10.             $MinimumSpecialChars = ($MinimumSpecialChars+($Length/128))/($Length/128)
  11.         }
  12.        
  13.         $pwd = ""
  14.         do {
  15.             $PieceLength = if ($Length -gt 128) { 128 } else { $Length }
  16.             if ($MinimumSpecialChars -gt $PieceLength) {
  17.                 $MinimumSpecialChars = $PieceLength
  18.             }
  19.             $pwd += [System.Web.Security.Membership]::GeneratePassword($PieceLength, $MinimumSpecialChars)
  20.             $Length -= 128
  21.         } While ($Length -gt 0)
  22.         return $pwd
  23.        
  24.     # Normal password requests
  25.     } else {
  26.         if (!$MinimumSpecialChars) {
  27.             $MinimumSpecialChars = $Length/6
  28.         } elseif ($MinimumSpecialChars -gt $Length) {
  29.             $MinimumSpecialChars = $Length
  30.         }
  31.         return [System.Web.Security.Membership]::GeneratePassword($Length, $MinimumSpecialChars)
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement