简体   繁体   English

在Powershell中%,$ _和@是什么意思?

[英]What does mean % , $_ and @ in Powershell?

这是什么意思:Powershell中的$_%

1..10 | Foreach {if($_%2){"$_ is odd number"}}
  • %
    • In your case, it is the modulus operator. 在您的情况下,它是模运算符。 It will return the remainder of dividing the left-hand side value by the right-hand side value. 它将返回将左侧值除以右侧值的余数。
    • It defaults as a PowerShell alias for Foreach-Object . 它默认为Foreach-Object的PowerShell别名。 You can execute the Get-Alias command to see other potential aliases that may contain special characters like Where-Object 's alias ? 您可以执行Get-Alias命令来查看其他可能包含特殊字符的别名,例如Where-Object的别名? .

  • $_
    • Synonymous with $PSItem $PSItem同义词
    • Contains the current object in the pipeline object 在管道对象中包含当前对象
    • In your case, it represents the current object passed into your Foreach-Object script block ( {} ). 在您的情况下,它表示传递到您的Foreach-Object脚本块( {} )中的当前对象。
    • It will commonly show up in the Where-Object {} script block and Select-Object hash tables. 它通常会显示在Where-Object {}脚本块和Select-Object哈希表中。

  • @

    • A literal @ character 文字@字符
    • Denotes splatting 表示飞溅
      • The syntax is @VariableName . 语法为@VariableName The variable can be an array or hash table. 该变量可以是数组或哈希表。 It is commonly used with a hash table or dictionary where the Name property represents a parameter name and the value property is the value for that parameter. 它通常与哈希表或字典一起使用,其中Name属性表示参数名称,而value属性是该参数的值。 Then that variable is splatted into another command. 然后将该变量放入另一个命令中。 An example is Get-Process @Params . 一个示例是Get-Process @Params
    • Used for declaring and initializing arrays via the array sub-expression operator @() . 用于通过数组子表达式运算符@()声明和初始化数组。

      • Examples are $myArray = @() and $myArray = @("value1","value2") . 例如$myArray = @()$myArray = @("value1","value2")
    • Used to create and/or initialize a hash table 用于创建和/或初始化哈希表
      • The syntax is $variable = @{} or $variable = @{Property=Value} . 语法为$variable = @{}$variable = @{Property=Value}
    • Used in here-strings 用于此处字符串
      • Here-strings are special case strings that can expand multiple lines and contain special characters 这里的字符串是特殊情况的字符串,可以扩展多行并包含特殊字符
      • Denoted by beginning a string value with @' or @" and closing the string value with a corresponding '@ or "@ . 通过以@'@"开头的字符串值并以相应的'@"@ @"结束字符串值来表示。
        • The here-string open and close characters should be isolated on their respective lines of the right-hand side (RHS). 此处的打开和关闭字符应在右侧(RHS)的相应行上隔离。
    • Common At symbol 常见于符号
      • Used in email address construction, ie user@domain.com. 用于电子邮件地址构建,即user@domain.com。
      • Used in external program remote logon syntax, ie user@hostname. 在外部程序远程登录语法中使用,即user @ hostname。

Extra Reading and Notable Links: 额外阅读和著名链接:

  1. See About Arithmetic Operators for information on modulus among other arithmetic operators. 有关其他算术运算符的模数信息,请参见关于算术运算符。

  2. See Foreach-Object for more information about Foreach-Object and how objects are processed. 有关Foreach Foreach-Object以及如何处理对象的更多信息,请参见Foreach对象

  3. See About Splatting for more information and usage of splatting. 有关飞溅的更多信息和用法,请参阅关于飞溅

  4. Another good resource is About Automatic Variables , which will list PowerShell's reserved/automatic variables. 另一个很好的资源是关于自动变量 ,它将列出PowerShell的保留/自动变量。 They are created and maintained by PowerShell. 它们由PowerShell创建和维护。 You will notice there are some variables that have non-alpha and non-numeric characters. 您会注意到有些变量具有非字母和非数字字符。 You should only use these variables for their intended purposes and not use their names when you create your own custom variables. 在创建自己的自定义变量时,应仅将这些变量用于其预期目的,而不应使用其名称。

  5. See About Arrays for details on the array sub-expression operator. 有关数组子表达式运算符的详细信息,请参见关于数组

  6. See About Hash Tables for details on creating and manipulating hash table objects. 有关创建和操作哈希表对象的详细信息,请参见关于哈希表

  7. See About Quoting Rules to see more information and examples of using here-strings. 请参阅关于报价规则,以了解更多信息和使用此处字符串的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM