在尝试一个小脚本时,它突然变得慢了很多。我意识到我已经用 random
替换了 get-random
,假设它是一个别名。
比较以下输出:
measure-command { (0..1000) | % { get-random } }
...
Seconds : 0
Milliseconds : 86
...
对比
measure-command { (0..1000) | % { random } }
...
Seconds : 44
Milliseconds : 192
...
看来 random
比 get-random
慢约 50 倍。看起来random
不是get-random
的别名,尽管它看起来具有相同的参数。 get-alias random
和 get-command random
均返回找不到 random
的错误。
TL;DR
random
不是 get-random
,它是什么?
最佳答案
随机
是 获取随机
当 PowerShell 无法将 1 个字的命令解析为函数/别名/可执行文件时,它的行为就像隐含 Get
动词一样。
这也适用于任何其他 Get-*
cmdlet。在 powershell.exe
中尝试其中一些:
item .
childitem $env:USERPROFILE
help random
content $env:SystemRoot\System32\drivers\etc\hosts
我实际上不确定帮助文件中是否提到了这一点,但自 PowerShell 1.0 版以来就是如此,如 2006 年书籍 "Monad (AKA. PowerShell): Introducing the MSH Command Shell and Language" 中所述。 :
All nouns have a default verb,
get
, which is assumed if no verb is given. In other words, the commandprocess
will behave in exactly the same manner asget-process
关于powershell - Get-Random 和 random 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34032554/