Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays Powershell、循环、数组有价值,为什么Powershell无法识别循环中的有价值_Arrays_Loops_Powershell - Fatal编程技术网

Arrays Powershell、循环、数组有价值,为什么Powershell无法识别循环中的有价值

Arrays Powershell、循环、数组有价值,为什么Powershell无法识别循环中的有价值,arrays,loops,powershell,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 -Subscriptio

我对编程非常陌生,我有一个这样的代码

$azuresubscription = get-azuresubscription



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

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

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

}
而回声是

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识别为猫、狗或鸟?如何更改此选项?

您需要使用
选择上的
-Expand
选项

$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
,可以获得名为的属性的实际值

一种更为强大的方法是:

get-azuresubscription | 
   ForEach-Object { 
       select-azuresubscription -SubscriptionName $_.SubscriptionName   
   }   
或者可能更短(如果Azure命令正确支持管道):


您需要使用
选择

$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
,可以获得名为的属性的实际值

一种更为强大的方法是:

get-azuresubscription | 
   ForEach-Object { 
       select-azuresubscription -SubscriptionName $_.SubscriptionName   
   }   
或者可能更短(如果Azure命令正确支持管道):


关于PS,要记住的主要一点是它是.NET,因此所有东西都是.NET对象。如果你想要字符串,那就是
-expand
的作用。关于PS,要记住的主要一点是它是.NET,因此所有东西都是.NET对象。如果您想要字符串,那么
-expand
就是在这里输入的。