powershell - Get-Random 和 random 有什么区别

标签 powershell

在尝试一个小脚本时,它突然变得慢了很多。我意识到我已经用 random 替换了 get-random,假设它是一个别名。

比较以下输出:

measure-command { (0..1000) | % { get-random } }
...
Seconds           : 0
Milliseconds      : 86
...

对比

measure-command { (0..1000) | % { random } }
...
Seconds           : 44
Milliseconds      : 192
...

看来 randomget-random 慢约 50 倍。看起来random不是get-random的别名,尽管它看起来具有相同的参数。 get-alias randomget-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 command process will behave in exactly the same manner as get-process

关于powershell - Get-Random 和 random 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34032554/

相关文章:

java - 如何使用 java 读取 Windows 日志 evt/evtx 文件

powershell - 如何使用包含$ value的模式选择字符串

PowerShell Lookbehind 的 RegX 代码在 IP 寻址上无法匹配

powershell - Get-ChildItem Exclude 和 File 参数不能一起使用

powershell - 点源脚本而不执行它

sql-server - Jenkins 的单元测试

powershell - Windows PowerShell 2.0(绝对入门)-书中最后一个项目的错误

powershell - RunSpace及其关闭

sql-server - 使用 SQL Server 中的自定义 cmdlet

string - 在文件中查找转义字符