powershell - Powershell变量分配与管道
问题描述
这两个命令之间的根本区别是什么?
$myVar = & "notepad.exe"
和
& "notepad.exe" | Set-Variable "myVar"
对于第一个,命令立即返回,而无需等待 exe 终止,这不是我所期望的。
对于第二个(或任何其他带有管道的东西,例如| Out-File
or | Set-Content
),该命令会正确等待 exe 将结果写入 stdout 并终止。
解决方案
Pipeline is nothing but taking the Output from the first set and passing it as an input to the second one. Pipelines act like a series of connected segments of pipe. Items moving along the pipeline must pass through each segment.
In your case, Powershell is actually waiting in both the cases. but if you use Measure-Command
, there is a difference in execution time which is better in case of $myVar = & "C:\path to\program.exe" $argument
推荐阅读
- wireshark - Wireshark 无法识别适配器
- python - 按内部值对嵌套字典进行排序
- excel - 复制列中前 n 个的整行
- python - 读取文件,替换列表中的项目,然后写入该文件
- html - 在 flex 增长框中包含另一个 div 并将其包含在父 div 中
- apache - 将目录重写为查询字符串
- python - FAIL : NameError: name 'urlparse' 未定义
- typescript - 使用带有方法的索引签名 - Typescript 3.5
- vim - vim 帮助产生“无法打开交换文件”错误
- javascript - “访问控制允许来源”:“*”不起作用