%{ $_.Key1 } 是什么意思?

What does %{ $_.Key1 } mean?

在为 HDInsight 编程时,我遇到了像

这样的行
$storageAccountKey = Get-AzureRmStorageAccountKey 
    -ResourceGroupName $resourceGroupName 
    -Name $storageAccountName 
    |  %{ $_.Key1 }

我理解 $_ 指的是 Get-AzureRmStorageAccountKey 命令的结果。但是 %{} 到底是什么意思?

%{ $_.Key1 }ForEach-Object { Write-Output $_.Key1 } ⇔ 对于管道中的每个对象,回显其 属性 Key1.

的值

% 是一个 alias for ForEach-Object. $_ is the current object automatic variable.

事实上 $storageAccountKey=Get-AzureRmStorageAccountKey -ResourceGroupName $resourceGroupName -Name $storageAccountName | %{ $_.Key1 } 仅适用于 PowerShell 1.3.2 和以前的版本。

以上命令现在有效。

请像下面这样使用最新的命令:

$storageAccountKey = Get-AzStorageAccountKey `
            -ResourceGroupName $storageAccountResourceGroupName `
            -Name $storageAccountName | Where-Object {$_.KeyName -eq "key1"} | %{$_.Value}