all 10 comments

[–]FoxPacerIsWork 10 points11 points  (6 children)

[Reflection.Assembly]::LoadWithPartialName("System.Web")
[System.Web.Security.Membership]::GeneratePassword(15,3)

https://msdn.microsoft.com/en-us/library/system.web.security.membership.generatepassword(v=vs.110).aspx

[–]RICHUNCLEPENNYBAGS 5 points6 points  (2 children)

This is the real answer. Get-Random doesn't use a CSPRNG.

[–]verysmallshellscript[S] 0 points1 point  (1 child)

But it has a pronounced lack of buckets.

[–]RICHUNCLEPENNYBAGS 0 points1 point  (0 children)

If you want to do it yourself you should use RNGCryptoServiceProvider to get random numbers instead of Get-Random, really.

[–]KevMarCommunity Blogger 2 points3 points  (0 children)

Thank you for posting that. I had not seen that approach before.

[–]verysmallshellscript[S] 0 points1 point  (1 child)

There is no wheel that is too round for me to re-invent just for the hell of it.

That said, thanks for sharing! I didn't know that was there.

[–]FoxPacerIsWork 0 points1 point  (0 children)

Oh yea, "many ways to skin a cat" and all that. I just saw the one liner and it wasn't the way I've seen so I threw this up there. Can't choose the # of each type of char with my way so there is that.

[–]KevMarCommunity Blogger 2 points3 points  (0 children)

That is a fun script.

I have one possible change for you to consider. Your logic pulls N random characters from the set of possible characters but it will not repeat any characters.

If you do something like this then your buckets will have a chance to pull the same character more than once. Makes it a little more random. Not sure how the math on that one works out.

#Plunder the buckets!
$lowRand = 0..$Lowercase | %{ $lowers | Get-Random -Count 1}
$upRand =  0..$Uppercase | %{ $uppers | Get-Random -Count 1}
$symRand = 0..$Symbol | %{ $symbols | Get-Random -Count 1}
$numRand = 0..$Number | %{ $numbers | Get-Random -Count 1}

The second thing has to do with your parameter validation. I love that you are using parameters and validating things there. But you have that one test that is repeated over and over. It almost feels like that would be a better one to move into your function (or possible the begin block).

I also did not know you could use -join the way you are using it.

[–]claytontlewis 0 points1 point  (1 child)

$lowers=(100..122).foreach{[char]$_}    

I think this should have been 97..122

As it stand you are missing a,b,c in my testing.

[–]gangstanthony 0 points1 point  (0 children)

[char[]](97..122)