Functions/Get-RandomHexDigit.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
function Get-RandomHexDigit { <# .SYNOPSIS Gets a random hex digit, or a string of hex digits .DESCRIPTION Gets a random hex digit, or a string of hex digits .PARAMETER Length Determines number of hexadecimal digits, defaults to 1 .PARAMETER IncludePrefix Switch which will prepend the result with '0x' .PARAMETER UpperCase Switch will make hexadecimal digits uppercase, otherwise they return as lowercase .EXAMPLE Get-RandomHexDigit -Length 4 f293 .EXAMPLE Get-RandomHexDigit -Length 2 -IncludePrefix 0x2d .EXAMPLE Get-RandomHexDigit -Length 6 -UpperCase 92C08A #> [CmdletBinding(ConfirmImpact='None')] param ( [Parameter(Position=0)] [ValidateRange(1,16)] [int] $Length = 1, [switch] $IncludePrefix, [switch] $UpperCase ) begin { Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]" } process { $ReturnVal = ( 1..$Length | foreach-object {'{0:x}' -f (Get-Random -Maximum 16)}) -join '' if ($UpperCase) { $ReturnVal = $ReturnVal.ToUpper() } if ($IncludePrefix) { $ReturnVal = '0x' + $ReturnVal } Write-Output -InputObject $ReturnVal } end { Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]" } } |