Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/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
如何将powershell get命令的输出传递给变量并在没有@{output}的情况下返回输出_Powershell_Azure_Azure Powershell - Fatal编程技术网

如何将powershell get命令的输出传递给变量并在没有@{output}的情况下返回输出

如何将powershell get命令的输出传递给变量并在没有@{output}的情况下返回输出,powershell,azure,azure-powershell,Powershell,Azure,Azure Powershell,代码如下 $rgName = "my-rg" $nameprefix = "vmtest" ##(following statement creates storage account with nameprefix followed by randomly generated string) New-AzureRmResourceGroupDeployment -ResourceGroupName $rgName -TemplateFile "C:\YourpathtoTemplates\Te

代码如下

$rgName = "my-rg"
$nameprefix = "vmtest"
##(following statement creates storage account with nameprefix followed by randomly generated string)
New-AzureRmResourceGroupDeployment -ResourceGroupName $rgName -TemplateFile "C:\YourpathtoTemplates\Template.json" -storageNamePrefix $nameprefix -storageSKU Standard_LRS

##Here is the problem statement
$tsaname = Get-AzureRmStorageAccount -ResourceGroupName $rgname | select 
StorageAccountName | Where-Object {$_.StorageAccountName -like 
"$nameprefix*"}
我需要将
$tsaname
的值传递给另一个命令,但它将拾取
@{StorageAccountName=vmtest4rncl5tyt798}
,这将导致一个错误

我们当前的解决方法是使用split传递值

$tsaname1 = ($tsaname -split "(?=$nameprefix)"|  Select -last 1).Trim("}") 
返回所需的值
vmtest4rncl5tyt798

有更好的办法吗

使用以下语句帮助进行故障排除

write-output = $tsaname
write-host $tsaname
write-host $tsaname1

您需要展开属性StorageAccountName:

$tsaname = Get-AzureRmStorageAccount -ResourceGroupName $rgname `
| Where-Object -Property StorageAccountName -like -Value "$nameprefix*" `
| Select-Object -ExpandProperty StorageAccountName
这将返回一个
字符串
,而不是具有属性的对象


或者,您也可以只使用
$tsaname.StorageAccountName

您是否查看了此文件?您能告诉我以下内容的输出是什么吗?(获取AzureRmStorageAccount-ResourceGroupName$rgname |其中对象{$\类似于“$nameprefix*”})。StorageAccountName