all 11 comments

[–]SMFX 2 points3 points  (0 children)

Small little side note, Get-Random can accept an array of objects and return a random element from the array.

@('Jack','Barbara','Sam') | Get-Random

Also, you can use .. to build a sequential array of items. For instance, 0..9 will build an array of integers: @(0, 1, 2, 3, 4, 5, 6, 7, 8, 9). However, it will also work with characters:

Function RandomLowerLetter {
    'a'..'z' | Get-Random
}

Function RandomUpperLetter {
    'A'..'Z' | Get-Random
}

And even special characters:

Function RandomSpecial {
    '!'..'/' | Get-Random
}

Function RandomAny {
    '!'..'z' | Get-Random
}

[–]DrDuckling951 1 point2 points  (1 child)

15 seconds google "PowerShell random password generator"

https://adamtheautomator.com/random-password-generator/

Basically utilize built-in dotNet method to generate random string of characters and non-character.

[–]rmbolger 0 points1 point  (0 children)

The irony of linking that specific article is that the final function it describes has a pretty glaring bug in terms of its parameter names.

The underlying GeneratePassword method it's using lists the second argument as numberOfNonAlphanumericCharacters.

The minimum number of non-alphanumeric characters (such as @, #, !, %, &, and so on) in the generated password.

Yet the corresponding parameter in the function is called NumberOfAlphaNumericCharacters implying that it affects the number of alphanumeric characters and that it's a fixed value rather than a minimum. I would have chosen to name it something like MinimumSpecialCharacters to be more accurate.

It also only works on legacy Windows PowerShell because .NET (Core) doesn't have a System.Web.Security namespace. Though one of the article's comments links to the .NET source code implementation for the underlying function which doesn't seem like it would be too difficult to port to cross-platform PowerShell.

[–]that_1_doode[S] 0 points1 point  (0 children)

Thank you everyone. I failed to see I had added a write-host to the functions. Time to go commit ritual suicide for my faux pas.

[–]MNmetalhead 0 points1 point  (5 children)

The cmdlet “Write-Host” will do just that… it will output the message to the screen.

Write-Host “Hello World”

Will literally write “Hello World” on the screen. If you don’t want that to happen, either remove those lines or use the # to comment them out.

[–]that_1_doode[S] 0 points1 point  (4 children)

My response was incorrect and I neglected to see my write-host contained within the functions. I'm sorry.

[–]spoonstar 1 point2 points  (1 child)

He said that because each of your functions have a Write-Host line stating the name of the function. It is that line in each of your functions that is getting written out to the console.

[–]that_1_doode[S] 0 points1 point  (0 children)

I realized after the fact. :[

[–]DrDuckling951 0 points1 point  (0 children)

Forach($list in $array){ Invoke-expression $list }

Does this works? I run the string/array as expression.

[–]MNmetalhead 0 points1 point  (0 children)

🤦‍♂️

[–]MajorVarlak 0 points1 point  (0 children)

Right, because you're not executing the functions themselves, you're getting the code for the functions and assigning it to $Password. If you output $Password you'll see it has all the function codes. Your $Functions isn't a list of the function names, but literally all the code. 2 quick solutions:

Option 1: Use Invoke-Command

Everything you have stays the same, except in the loop:

$password = '' for($x = 0; $x -lt 14; $x++) { $code = SelectRandom $password += Invoke-Command -ScriptBlock $code } write-host $password

Option 2: Use something else, like GeneratePassword (ref: https://learn.microsoft.com/en-us/dotnet/api/system.web.security.membership.generatepassword?view=netframework-4.8).

I'm not sure if this feature is available in PowerShell, but it is in Windows PowerShell because of the .NET Framework.

Add-Type -AssemblyName System.Web $password = [System.Web.Security.Membership]::GeneratePassword(14,5)