我是编程新手,我有这样的代码。
$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?我怎么能改变它?
答案 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