【发布时间】:2021-02-08 23:49:29
【问题描述】:
因此,我正在尝试创建一个 powershell 脚本,该脚本使用各种大小的文件来“衡量”GPG 的速度。给我带来麻烦的具体部分是:
1 $temppath="$($env:Temp)\"
...
4 Measure-Command {$bytes= 10000MB
5 [System.Security.Cryptography.RNGCryptoServiceProvider]$rng = New- Object.System.Security.Cryptography.RNGCryptoServiceProvider
6 $rndbytes = New-Object byte[] $bytes
7 [System.Io.File]::WriteAllBytes("$($temppath)\test.txt", $rndbytes)} | Select-Object -OutVariable gentime | Out-Null;
当$bytes >~ 1.5GB(我使用 10000MB 来测试它)时,我收到一个错误,如果我正确理解发生了什么,则对应于字节数组由 int32 类型的变量索引的事实.具体错误是这样的:
New-Object : Cannot convert argument "0", with value: "10485760000", for "Byte[]" to type "System.Int32": "Cannot convert value "10485760000" to type "System.Int32". Error: "Value was either too large or too small for an Int32.""
At line:6 char:13
+ $rndbytes = New-Object byte[] $bytes
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
有没有办法强制 powershell 在创建对象时使用 int64 或 uint64 类型的索引来索引 $rndbytes?如果这不是问题,我很乐意被展示并解释到底出了什么问题。
请注意,如果您使用例如$bytes=1000MB(或写$bytes=1GB),则代码可以完美运行。我怀疑我在$bytes.index > 2^31 -1 左右碰壁了。
谢谢你的帮助~!!
【问题讨论】:
-
不,数组的长度不能超过
[int]::MaxValue。批量输出 -
该死,真不幸。那么只需要创建多个数组并批量处理?
-
创建一个小的缓冲区数组(例如 4KB),然后写入该大小的块,直到达到所需的文件大小
-
是时候学习如何在 powershell 中做到这一点了。你知道
[System.Io.File]::WriteAllBytes是覆盖数据还是追加数据?
标签: powershell types int32