“@@quot”是什么符号在powershell中做?

发布于 2025-02-05 20:00:06 字数 101 浏览 1 评论 0 原文

我已经看到@ PowerShell中使用的符号用于初始数组。

@符号究竟是什么表示的,我在哪里可以阅读有关它的更多信息?

I've seen the @ symbol used in PowerShell to initialise arrays.

What exactly does the @ symbol denote and where can I read more about it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

同尘 2025-02-12 20:00:07

有用的 - 即使是较晚的问题 - 提供完整的答案,才能:

虽然上述答案提供了答案的最多,但它是 参见 about_arrays

迫使该值是一个数组 $ a = @(ps | whene -like'foo')

hash Initializer (请参阅 about_hash_tables

初始化带有键值对的哈希表,例如
$ hasharguments = @{path =“ test.txt”; destination =“ test2.txt”; whatif = $ true}

splatting (请参阅 about_splatting

让我们调用一个带有数组或哈希桌子的参数的cmdlet,而不是更习惯的单独枚举参数,例如,使用上面的哈希表, copy> copy> copy-item @hasharguments

(请参阅 about_quoting_rules

让您使用易于嵌入式的引号,通常用来创建字符串,通常使用多行字符串,例如:

$data = @"
line one
line two
something "quoted" here
"@

因为这种类型的问题('x'符号在powershell中意味着什么?)在stackoverflow上如此普遍,在许多读者的评论中,我都放在一个词典上。 PowerShell标点符号,刚刚在Simple-Talk.com上出版。阅读有关 @以及%和#和$ _的所有信息?以及 PowerShell Punctuation 的完整指南。该文章附带的是该壁画,它为您提供了一张纸上的所有内容:

While the above responses provide most of the answer it is useful--even this late to the question--to provide the full answer, to wit:

Array sub-expression (see about_arrays)

Forces the value to be an array, even if a singleton or a null, e.g. $a = @(ps | where name -like 'foo')

Hash initializer (see about_hash_tables)

Initializes a hash table with key-value pairs, e.g.
$HashArguments = @{ Path = "test.txt"; Destination = "test2.txt"; WhatIf = $true }

Splatting (see about_splatting)

Let's you invoke a cmdlet with parameters from an array or a hash-table rather than the more customary individually enumerated parameters, e.g. using the hash table just above, Copy-Item @HashArguments

Here strings (see about_quoting_rules)

Let's you create strings with easily embedded quotes, typically used for multi-line strings, e.g.:

$data = @"
line one
line two
something "quoted" here
"@

Because this type of question (what does 'x' notation mean in PowerShell?) is so common here on StackOverflow as well as in many reader comments, I put together a lexicon of PowerShell punctuation, just published on Simple-Talk.com. Read all about @ as well as % and # and $_ and ? and more at The Complete Guide to PowerShell Punctuation. Attached to the article is this wallchart that gives you everything on a single sheet:
enter image description here

冰火雁神 2025-02-12 20:00:07

PowerShell实际上将把任何逗号分隔的列表视为一个数组:

"server1","server2"

因此,在这些情况下, @是可选的。但是,对于关联阵列, @是必需的:

@{"Key"="Value";"Key2"="Value2"}

正式, @是“数组操作员”。您可以在与PowerShell一起安装的文档中阅读有关它的更多信息,或者我共同撰写的“ Windows PowerShell:TFM”之类的书。

PowerShell will actually treat any comma-separated list as an array:

"server1","server2"

So the @ is optional in those cases. However, for associative arrays, the @ is required:

@{"Key"="Value";"Key2"="Value2"}

Officially, @ is the "array operator." You can read more about it in the documentation that installed along with PowerShell, or in a book like "Windows PowerShell: TFM," which I co-authored.

甜心 2025-02-12 20:00:07

您还可以在@()中将CMDLET(或Pipeline)的输出包裹起来,以确保您获得的内容是数组而不是单个项目。

例如,DIR通常返回列表,但根据选项,它可能会返回一个对象。如果您打算使用foreach-object进行迭代,则需要确保返回列表。这是一个人为的示例:

$results = @( dir c:\autoexec.bat)

另一件事...一个空数组(喜欢初始化变量)表示为@()

You can also wrap the output of a cmdlet (or pipeline) in @() to ensure that what you get back is an array rather than a single item.

For instance, dir usually returns a list, but depending on the options, it might return a single object. If you are planning on iterating through the results with a foreach-object, you need to make sure you get a list back. Here's a contrived example:

$results = @( dir c:\autoexec.bat)

One more thing... an empty array (like to initialize a variable) is denoted @().

豆芽 2025-02-12 20:00:07

剥落操作员

要创建一个数组,我们创建一个变量并分配数组。数组由“@”符号指出。让我们进行上面的讨论,并使用数组连接到多台远程计算机:

$strComputers = @("Server1", "Server2", "Server3")<enter>

它们用于数组和哈希。

PowerShell教程7:累积,回忆和修改数据

The Splatting Operator

To create an array, we create a variable and assign the array. Arrays are noted by the "@" symbol. Let's take the discussion above and use an array to connect to multiple remote computers:

$strComputers = @("Server1", "Server2", "Server3")<enter>

They are used for arrays and hashes.

PowerShell Tutorial 7: Accumulate, Recall, and Modify Data

Array Literals In PowerShell

緦唸λ蓇 2025-02-12 20:00:07

我希望这有助于更好地理解它。
您可以将“值”存储在键中,然后返回该值进行操作。
在这种情况中

$array = @{
a = "test1";
b = "test2";
c = "test3"
}

foreach($elem in $array.GetEnumerator()){
    if ($elem.key -eq "a"){
        $key = $elem.key
        $value = $elem.value
    }
    elseif ($elem.key -eq "b"){
        $key = $elem.key
        $value = $elem.value
    }
    elseif ($elem.key -eq "c"){
        $key = $elem.key
        $value = $elem.value
    }
    else{
        Write-Host "No other value"
    }

    Write-Host "Key: " $key "Value: " $value 
}

I hope this helps to understand it a bit better.
You can store "values" within a key and return that value to do something.
In this case I have just provided @{a="";b="";c="";} and if not in the options i.e "keys" (a, b or c) then don't return a value

$array = @{
a = "test1";
b = "test2";
c = "test3"
}

foreach($elem in $array.GetEnumerator()){
    if ($elem.key -eq "a"){
        $key = $elem.key
        $value = $elem.value
    }
    elseif ($elem.key -eq "b"){
        $key = $elem.key
        $value = $elem.value
    }
    elseif ($elem.key -eq "c"){
        $key = $elem.key
        $value = $elem.value
    }
    else{
        Write-Host "No other value"
    }

    Write-Host "Key: " $key "Value: " $value 
}
尘世孤行 2025-02-12 20:00:06

在PowerShell V2中, @也是 splat操作员

PS> # First use it to create a hashtable of parameters:
PS> $params = @{path = "c:\temp"; Recurse= $true}
PS> # Then use it to SPLAT the parameters - which is to say to expand a hash table 
PS> # into a set of command line parameters.
PS> dir @params
PS> # That was the equivalent of:
PS> dir -Path c:\temp -Recurse:$true

In PowerShell V2, @ is also the Splat operator.

PS> # First use it to create a hashtable of parameters:
PS> $params = @{path = "c:\temp"; Recurse= $true}
PS> # Then use it to SPLAT the parameters - which is to say to expand a hash table 
PS> # into a set of command line parameters.
PS> dir @params
PS> # That was the equivalent of:
PS> dir -Path c:\temp -Recurse:$true
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文