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

时间:2016-02-08 14:27:36

标签: powershell hdinsight

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

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

我理解$_是指Get-AzureRmStorageAccountKey命令的结果。但是%{}的含义究竟是什么?

2 个答案:

答案 0 :(得分:19)

对于管道中的每个对象,

%{ $_.Key1 }ForEach-Object { Write-Output $_.Key1 }⇔,回显其属性Key1的值。

%aliasForEach-Object$_是当前对象automatic variable

答案 1 :(得分:0)

事实上,$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}
相关问题