如何将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