Azure Powershell将数组对象转换为哈希表和相关的输出尝试

时间:2018-05-08 17:21:35

标签: powershell azure azure-powershell

我正在创建一个Powershell脚本,该脚本将查询我的Azure服务并根据标记查找我所有网站的内部版本号。我遇到的主要问题是$h = Get-AzureRmTag -Name $Environment -Detailed似乎返回一个Array.Object。尝试使用:$h | ForEach-Object {$SiteList.Add($_,$inputobject.$_)}将此Array.Object转换为Hashtable时。当我这样做并输出我的Hash进行审核时,为了确保它有效,我得到:enter image description here 我正在使用:Write-Host ($i | Out-String) -ForegroundColor "green"输出。

这是我的完整代码:

#Holder variables to ensure logic works
$myResourceGroup = 'A_Resource_Group'
$ApiVersion = '2015-08-01'
#$Environment = "Primary"

#Initialized Hash tables
$SiteList = @{}
$EvList = @{}
$BuildList = @{}

#Ask user for the environment tag they wish to query.
$Environment = Read-Host "Tag For Query"

#Forms initial object array of data
$h = Get-AzureRmTag -Name $Environment -Detailed 

#converts object to a hash
$h  | ForEach-Object {$SiteList.Add($_,$inputobject.$_)} 

#find all the environments inside the larger ones (Everything in 
Primary)
foreach ($i in $SiteList)
{
#Check that the output contains desired information
Write-Host ($i | Format-List | Out-String) -ForegroundColor "green" 

#Search for the tags under each of the tags saved in $h (Such as all of the 
sites tagged with Prod)
$x = Get-AzureRmTag -Name ($i) -Detailed |Format-List
#save new tags to the $EvList array
$x | Out-String | ForEach-Object {$EvList.Add($_,$inputobject.$_)}
}

#Go through each sub-tag in $EvList  and find the build number of each
foreach ($a in $EvList)
{
#Check that the output contains desired information
Write-Host ($a | Format-List | Out-String) -ForegroundColor "DarkYellow" | Format-List

#looks for and reads app settings from azure, only keeps buildNumber
$p = (Invoke-AzureRMResourceAction -ResourceGroupName $myResourceGroup `
-ResourceType Microsoft.Web/sites/Config -Name ($a | Out-String)/appsettings 
`
-Action list -ApiVersion $ApiVersion -Force).Properties |Select-Object 
BuildNumber |Format-List

#Save each buildNumber to a list containing the site name($a) as the key and 
the BuildNumber($p) as the value.
$BuildList.Add($a , $p)
}

#Output the list of sites and buildNumbers in cyan for easy readability
Write-Host ($BuildList | Format-List | Out-String) -ForegroundColor "Cyan" 

我的代码的基本层次结构如下:

  

主要---> Dev,Test,QA,Prod ---> Webjobs,apis,基于位置的Web服务等......

我想要获得的是我的每个Webjobs,api,基于位置的Web服务的内置编号等等,并将它们存储在一个易于格式化和阅读的列表中。

像这样: enter image description here

目前第二个和第三个foreach循环不起作用,因为它们需要第一个才能工作,但它们可以帮助保持一切顺利并保持逻辑在一起。

感谢您的帮助,我上周开始学习Powershell。仍在努力解决它的怪癖:)

0 个答案:

没有答案