What does %{ $_.Key1 } mean?
Asked Answered
D

2

19

While programming for HDInsight I came across lines like

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

I understand $_ refers to the result of the Get-AzureRmStorageAccountKey command. But what exactly is the meaning of %{} ?

Diagnosis answered 8/2, 2016 at 14:27 Comment(0)
W
40

%{ $_.Key1 }ForEach-Object { Write-Output $_.Key1 } ⇔ for each object in the pipeline, echo the value of its property Key1.

% is an alias for ForEach-Object. $_ is the current object automatic variable.

Waive answered 8/2, 2016 at 14:31 Comment(0)
C
0

In fact $storageAccountKey=Get-AzureRmStorageAccountKey -ResourceGroupName $resourceGroupName -Name $storageAccountName | %{ $_.Key1 } only works for PowerShell 1.3.2 and previous versions.

The above command does not work now.

Please use the latest command like the bellow:

$storageAccountKey = Get-AzStorageAccountKey `
            -ResourceGroupName $storageAccountResourceGroupName `
            -Name $storageAccountName | Where-Object {$_.KeyName -eq "key1"} | %{$_.Value}
Contortive answered 27/1, 2021 at 9:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.