Sha*_*tin 92 syntax powershell
似乎%操作在管道之后启动脚本块,尽管about_Script_Blocks表示%不是必需的.
这一切都很好.
get-childitem | %{ write-host $_.Name }
{ write-host 'hello' }
%{ write-host 'hello' }
Run Code Online (Sandbox Code Playgroud)
但是当我们在管道之后添加一个脚本块时,我们需要首先使用%.
get-childitem | { write-host $_.Name }
Run Code Online (Sandbox Code Playgroud)
Koh*_*brr 129
在cmdlet(例如您的示例)的上下文中使用时,它是以下别名ForEach-Object:
> Get-Alias -Definition ForEach-Object
CommandType Name Definition
----------- ---- ----------
Alias % ForEach-Object
Alias foreach ForEach-Object
Run Code Online (Sandbox Code Playgroud)
当在方程的上下文中使用时,它是模数运算符:
> 11 % 5
1
Run Code Online (Sandbox Code Playgroud)
并且作为模数运算符,%也可以在赋值运算符(%=)中使用:
> $this = 11
> $this %= 5
> $this
1
Run Code Online (Sandbox Code Playgroud)
一篇文章PowerShell - 特殊字符和标记
提供多个符号的描述,包括%
% (percentage)
1. Shortcut to foreach.
Task: Print all items in a collection.
Solution.
... | % { Write-Host $_ }
2. Remainder of division, same as Mod in VB.
Example:
5 % 2
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
65895 次 |
| 最近记录: |