function/tool/Get-RandomString.ps1
Function Get-RandomString { <# .SYNOPSIS Creates random strings .DESCRIPTION This functions creates random strings. It is possible to generate Secure Strings. .PARAMETER Length Determined the length of the string. .PARAMETER Securestring Can be used, to generate securestrings. .INPUTS System.Int[] System.Switch[] .OUTPUTS System.String[] System.Securestring[] .EXAMPLE Get-RandomString -Length 23 -SecureString .LINK https://github.com/gisp497/psgisp #> [CmdletBinding()] param ( [Parameter( Mandatory = $false, ValueFromPipeline = $false, HelpMessage = "length of the password")] [int]$Length = 24, [Parameter( Mandatory = $false, ValueFromPipeline = $false, HelpMessage = "number of special characters in password")] [int]$SpecialCharacter = 8, [Parameter( Mandatory = $false, ValueFromPipeline = $false, HelpMessage = "Creates Output as secure string")] [switch]$SecureString = $false ) Begin { #check if password lenght is bigger than special character number if ($SpecialCharacter -ge $Length){ Throw "Can't use more special characters than password length." } #calculate password length $NormalCharacter = $Length - $SpecialCharacter #set of possible characters $Normal = Get-Random -Count $NormalCharacter -InputObject ([Char[]]'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') $Special = Get-Random -Count $SpecialCharacter -InputObject ([Char[]]'!#$%&*+-/=?@_') $StringSet = $Normal + $Special } Process { #Check if Securestring is wanted if ($SecureString) { $outputobject = (Get-Random -Count $Length -InputObject $StringSet) -join '' | ConvertTo-SecureString -AsPlainText -Force }else{ [string]$outputobject = (Get-Random -Count $Length -InputObject $StringSet) -join '' } } End { #return value return $outputobject } } |