Powershell,Loop,Array很有价值,为什么Powershell无法在循环中识别出有价值的东西

时间:2016-01-20 18:04:55

标签: arrays loops powershell

我是编程新手,我有这样的代码。

$azuresubscription = get-azuresubscription



for($K = 0; $K -le $azuresubscription.Length -1; $K++){

$s= $azuresubscription[$K]|select SubscriptionName

$s 
Write-Host "++++++++++++++++++++";
select-azuresubscription -SubscriptionName $s   

}

和echo out是

SubscriptionName                                                                                                                                                                                                                                                 
----------------
CAT                                                                                                                                                                                                                                              
++++++++++++++++++++
select-azuresubscription : Must specify a non-null subscription name.
Parameter name: name
At line:12 char:1
+ select-azuresubscription -SubscriptionName $s
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Select-AzureSubscription], ArgumentException
    + FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.Profile.SelectAzureSubscriptionCommand

Dog                                                                                                                                                                                                                                                    
++++++++++++++++++++
select-azuresubscription : Must specify a non-null subscription name.
Parameter name: name
At line:12 char:1
+ select-azuresubscription -SubscriptionName $s
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Select-AzureSubscription], ArgumentException
    + FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.Profile.SelectAzureSubscriptionCommand

 Bird                                                                                                                                                                                                                                               
++++++++++++++++++++
select-azuresubscription : Must specify a non-null subscription name.
Parameter name: name
At line:12 char:1
+ select-azuresubscription -SubscriptionName $s
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Select-AzureSubscription], ArgumentException
    + FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.Profile.SelectAzureSubscriptionCommand

所以我尝试了这个

select-azuresubscription -SubscriptionName "CAT"

它有效, 根据错误,似乎Powershell认为$ s为空

为什么Powershell无法识别$ s为CAT,Dog还是Bird?我怎么能改变它?

1 个答案:

答案 0 :(得分:3)

您需要在-Expand

上使用select选项
$azuresubscription = get-azuresubscription

for($K = 0; $K -le $azuresubscription.Length -1; $K++) {
    $s = $azuresubscription[$K] | select -Expand SubscriptionName

    Write-Verbose -Verbose "`$s = $s" 
    Write-Verbose -Verbose "++++++++++++++++++++";
    select-azuresubscription -SubscriptionName $s   
}

在初始形式中,您将获得包含所选属性子集的powershell对象。简而言之,-ExpandProperty-Expand,您将获得名为。

的属性的实际值

更多的PowerShell-y方式也是:

get-azuresubscription | 
   ForEach-Object { 
       select-azuresubscription -SubscriptionName $_.SubscriptionName   
   }   

或者甚至可能更短(如果Azure命令正确支持管道):

get-azuresubscription | select-azuresubscription