48
到57
是數字0-9,powershell的範圍操做符是..
,和Perl 5
的同樣, 因此 48..57
就是指(48 49 50 51 52 53 54 55 56 57)
的列表。 65
到90
是大寫字符A到Z,97
到122
是小寫字母。若是須要獲取多有的可打印字符(包括空格)的話,範圍是32..127
。shell
[char]
把對應數字轉換成字符,例如 [char](66)
就是B
(大寫字母B),C語言使用的小括號來進行類型強制轉換。windows
# 1 -join((48..57 + 65..90 + 97..122) | get-random -count 6 | %{[char]$_}) # 若是不指定-count參數,則前面的list有多少個字符 # get-random就會獲取多少個字符,只是順序打亂了 # 2 -join(0..1024|%{[char][int]((48..57 + 65..90 + 97..122)| Get-Random)}) # 這裏的0..1024至關於循環控制,每循環一次後面的%{[char][int]((48..57 + 65..90 + 97..122)| Get-Random)}執行一次,其中在數字字母中隨機選一個字符 # 0..1024, like Perl, loop controller #-join是 字符鏈接操做符 # 3 -join ([char[]](65..90+97..122) | Get-Random -Count 6)
function Get-RandomString() { param( [int]$length=10, # 這裏的[int]是類型指定 [char[]]$sourcedata ) for($loop=1; $loop –le $length; $loop++) { $TempPassword+=($sourcedata | GET-RANDOM | %{[char]$_}) } return $TempPassword } Get-RandomString -length 14 -sourcedata (48..127)
1. Powershell Reference 3.0: Get-Random
2. Generating A New Password With Windows Powershell
3. Generate Random Letters With Powershell
4. How do I encode Unicode character codes in a PowerShell string literal?dom