数组对象到哈希 Table 的 Azure Powershell 转换及相关输出尝试

Azure Powershell Conversion of Array Object to Hash Table And Related Output Attempt

我正在创建一个 Powershell 脚本,该脚本将查询我的 Azure 服务并根据标签查找我所有站点的内部版本号。我遇到的主要问题是:

$h = Get-AzureRmTag -Name $Environment -Detailed

似乎 return 一个 Array.Object。尝试使用此将此 Array.Object 转换为哈希表时:

$h  | ForEach-Object {$SiteList.Add($_,$inputobject.$_)}

当我执行此操作并输出我的哈希以供审查时,为确保它有效,我得到以下信息:

我正在使用这个输出数据:

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" 

我的标签的基本层次结构是这样的:

Primary ---> Dev, Test, QA, Prod ---> Webjobs, apis, location based web services, ect...

我想要获取的是我的每个 Web 作业、api、基于位置的 Web 服务等的内部版本号,并将它们存储在一个易于格式化和读取的列表中。

像这样:

目前,第二个和第三个 foreach 循环不起作用,因为它们需要第一个循环才能起作用,但它们的作用是帮助保持一切正常并保持逻辑完整。

感谢您的帮助。

#Hard coded variable
$myResourceGroup = 'resource'
$ApiVersion = '2015-08-01'

#Initialized Hash table
$SiteList = @{}

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

$appServices = Get-AzureRmWebApp -ResourceGroupName $myResourceGroup
foreach ($app in $appServices) 
{
    foreach ($tag in $app.Tags) 
    {
        foreach ($h in $app.Tags.GetEnumerator()) 
        {
            if ($h.Key -eq "Primary" -And $h.Value -eq $Environment) 
            {
                $appName = $app.Name
                $buildNumber = (Invoke-AzureRMResourceAction -ResourceGroupName $myResourceGroup `
                    -ResourceType Microsoft.Web/sites/Config `
                    -Name "$appName/appsettings" `
                    -Action list `
                    -ApiVersion $ApiVersion -Force).Properties.BuildNumber

                $SiteList.add($app.Name, $buildNumber)
            }
        }
    }
}

Write-Host ("Primary>") -ForegroundColor "Blue"
Write-Host ("`t" + $Environment + ">") -ForegroundColor "Blue"

Write-Host ("`t`tSite`t`t`t`t`tBuild Number") -ForegroundColor "Blue"
Write-Host ("`t`t____`t`t`t`t`t____________") -ForegroundColor "Blue"
foreach ($h in $SiteList.GetEnumerator()) 
{
     Write-Host ("`t`t$($h.Name):`t`t`t$($h.Value)") -ForegroundColor `
         "Green"
}

好的,这样就可以了。问题已解决。