如果你对标点符号没问题,你可以使用这个:
Add-Type -AssemblyName System.Web
#get a random filename in the present working directory
$fn = [System.IO.Path]::Combine($pwd, [GUID]::NewGuid().ToString("N") + '.txt')
#set number of iterations
$count = 1mb/128
do{
#Write the 1267 chars plus eol
[System.Web.Security.Membership]::GeneratePassword(126,0) | Out-File $fn -Append ascii
#decrement the counter
$count--
}while($count -gt 0)
这可以让您达到大约 7 秒。样本输出:
0b5rc@EXV|e{kftc+1+Xn$-c%-*9q_9L}p=I=k@zrDg@HaJDcl}B(38i&m{lV@vlq%5h/a?m2X!yo]qs0=pEw:Tn4wb5F$k$O85$8F.QLvUzA{@X2-w%5(3k;BE2Qi
使用流写入器而不是 Out-File -Append 可避免打开/关闭周期并将其缩短至 62 毫秒。
Add-Type -AssemblyName System.Web
#get a random filename in the present working directory
$fn = [System.IO.Path]::Combine($pwd, [GUID]::NewGuid().ToString("N") + '.txt')
#set number of iterations
$count = 1mb/128
#create a filestream
$fs = New-Object System.IO.FileStream($fn,[System.IO.FileMode]::CreateNew)
#create a streamwriter
$sw = New-Object System.IO.StreamWriter($fs,[System.Text.Encoding]::ASCII,128)
do{
#Write the 1267 chars plus eol
$sw.WriteLine([System.Web.Security.Membership]::GeneratePassword(126,0))
#decrement the counter
$count--
}while($count -gt 0)
#close the streamwriter
$sw.Close()
#close the filestream
$fs.Close()
您还可以使用字符串生成器和 GUID 来生成伪随机数和小写字母。
#get a random filename in the present working directory
$fn = [System.IO.Path]::Combine($pwd, [GUID]::NewGuid().ToString("N") + '.txt')
#set number of iterations
$count = 1mb/128
#create a filestream
$fs = New-Object System.IO.FileStream($fn,[System.IO.FileMode]::CreateNew)
#create a streamwriter
$sw = New-Object System.IO.StreamWriter($fs,[System.Text.Encoding]::ASCII,128)
do{
$sb = New-Object System.Text.StringBuilder 126,126
0..3 | %{$sb.Append([GUID]::NewGuid().ToString("N"))} 2> $null
$sw.WriteLine($sb.ToString())
#decrement the counter
$count--
}while($count -gt 0)
#close the streamwriter
$sw.Close()
#close the filestream
$fs.Close()
这大约需要 4 秒并生成以下示例:
1fef6ccabc624e4dbe13a0415764fd2c58aa873377c7465eaecabdf6ba6fdf71c55496600a374c4c8cff75be46b1fe474230231ffccc4e3aa2753391afb32c
如果您想使用与示例中相同的字符,您可以使用以下方法:
#get a random filename in the present working directory
$fn = [System.IO.Path]::Combine($pwd, [GUID]::NewGuid().ToString("N") + '.txt')
#array of valid chars
$chars = [char[]] ([char]'0'..[char]'9' + [char]'A'..[char]'Z' + [char]'a'..[char]'z')
#create a random object
$rand = New-Object System.Random
#set number of iterations
$count = 1mb/128
#get length of valid character array
$charslength = $chars.length
#create a filestream
$fs = New-Object System.IO.FileStream($fn,[System.IO.FileMode]::CreateNew)
#create a streamwriter
$sw = New-Object System.IO.StreamWriter($fs,[System.Text.Encoding]::ASCII,128)
do{
#get 126 random chars This is the major slowdown
$randchars = 1..126 | %{$chars[$rand.Next(0,$charslength)]}
#Write the 1267 chars plus eol
$sw.WriteLine([System.Text.Encoding]::ASCII.GetString($randchars))
#decrement the counter
$count--
}while($count -gt 0)
#close the streamwriter
$sw.Close()
#close the filestream
$fs.Close()
这需要大约 27 秒并生成以下示例:
Fev31lweOXaYKELzWOo1YJn8LpZoxonWjxQYhgZbR62EmgjHit5J1LrvqniBB7hZj4pNonIpoCZSHYLf5H63iUUN6UhtyOQKPSViqMTvbGUomPeIR36t1drEZSHJ6O
索引 char 数组和输出文件 - 每次都追加打开和关闭文件会大大降低速度。