繁体   English   中英

Powershell:将 ForEach 循环结果转换为 HTML 表

[英]Powershell: convert ForEach loop results into an HTML table

我为 Azure 订阅成本计算编写了一个代码,该代码基于我们环境中使用的两个标签(在本例中为应用程序标签和所有者标签)。

$ApplicationTags = ((Get-AzResource).Tags).Application | select -Unique
$ApplicationTagLoop = @(Foreach ($ApplicationTag in $ApplicationTags) {
    $ConsumptionUsageDetail = (Get-AzConsumptionUsageDetail -StartDate (Get-Date).addmonths(-1) -EndDate 
    (Get-Date)) | Where-Object {$_.Tags -ne $null} | Where-Object {$_.Tags['Application'] -eq 
    $ApplicationTag}
    $SumForApplicationTag = 0
    $TotalCostPerApplicationTag = $ConsumptionUsageDetail.PretaxCost
    $TotalCostPerApplicationTag | ForEach {$SumForApplicationTag += $_}
    Write-Host "Application tag is"$ApplicationTag". Sum for the tag is:" $([int]$SumForApplicationTag) "Eur" 
        $OwnerTags = ((Get-AzResource -TagValue $ApplicationTag).Tags).Owner | select -Unique
        ForEach ($OwnerTag in $OwnerTags) {
        $ConsumptionUsageDetail = (Get-AzConsumptionUsageDetail -StartDate (Get-Date).addmonths(-1) - 
        EndDate (Get-Date)) | Where-Object {$_.Tags -ne $null} | Where-Object {$_.Tags['Application'] -eq 
        $ApplicationTag} | Where-Object {$_.Tags['Owner'] -eq $OwnerTag}
        $SumForOwner = 0
        $TotalCostPerOwnerTag = $ConsumptionUsageDetail.PretaxCost
        $TotalCostPerOwnerTag | Foreach {$sumforowner += $_}
        ConvertTo-HTML -Body "Application tag: $ApplicationTag 'Owner: $OwnerTag Cost: 
        $([int]$SumForOwner) Eu." -Title "Cost of subscriptions" | Out-File c:\example.html
        Write-Host Owner is - $OwnerTag" Sum for the owner is:" $([int]$SumForOwner) "Eur"
    }
})

此 PS 代码的 Output 为:

Application tag is Testing. Sum for the tag is: 25 Eur
Owner is - john.johnson@contoso.com Sum for the owner is: 15 Eur
Owner is - tom.thompson@contoso.com Sum for the owner is: 10 Eur
Application tag is Testing 2. Sum for the tag is: 100 Eur
Owner is - jim.jameson@contoso.com Sum for the owner is: 40 Eur
Owner is - eve.evens@contoso.com Sum for the owner is: 40 Eur
Owner is - charles.mcgill@contoso.com Sum for the owner is: 20Eur 

...等等...

Now I need to stream this output to an HTML table somehow, I tried doing that with ConvertTo-HTML command but it keeps rewriting itself with the last output and does not populate the table in any way. 我还尝试让 ForEach 循环到$ApplicationTagLoop = @(foreach ($i in $ApplicationTag)中,如下所示:所以没有任何东西被转换为 HTML。

我如何重写 ConvertTo-HTML 部分,以便将循环的每个 output 保存到 HTML 文件的新行中?

所需的 output 格式应为: https://i.stack.imgur.com/z13SM.png

查看代码,我猜是预期的输出。

使用 object 集合构建来获得正确的 HTML:

$ApplicationTags = ((Get-AzResource).Tags).Application | select -Unique
$html = [System.Collections.ArrayList]@()
Foreach ($ApplicationTag in $ApplicationTags) {
    $ConsumptionUsageDetail = (Get-AzConsumptionUsageDetail -StartDate (Get-Date).addmonths(-1) -EndDate 
    (Get-Date)) | Where-Object {$_.Tags -ne $null} | Where-Object {$_.Tags['Application'] -eq 
    $ApplicationTag}
    $SumForApplicationTag = 0
    $TotalCostPerApplicationTag = $ConsumptionUsageDetail.PretaxCost
    $TotalCostPerApplicationTag | ForEach {$SumForApplicationTag += $_}
    Write-Host "Application tag is"$ApplicationTag". Sum for the tag is:" $([int]$SumForApplicationTag) "Eur" 
        $OwnerTags = ((Get-AzResource -TagValue $ApplicationTag).Tags).Owner | select -Unique
        ForEach ($OwnerTag in $OwnerTags) {
           $ConsumptionUsageDetail = (Get-AzConsumptionUsageDetail -StartDate (Get-Date).addmonths(-1) - 
           EndDate (Get-Date)) | Where-Object {$_.Tags -ne $null} | Where-Object {$_.Tags['Application'] -eq 
             $ApplicationTag} | Where-Object {$_.Tags['Owner'] -eq $OwnerTag}
           $SumForOwner = 0
           $TotalCostPerOwnerTag = $ConsumptionUsageDetail.PretaxCost
           $TotalCostPerOwnerTag | Foreach {$sumforowner += $_}
           #ConvertTo-HTML -Body "Application tag: $ApplicationTag 'Owner: $OwnerTag Cost:         
               #$([int]$SumForOwner) Eu." -Title "Cost of subscriptions" | Out-File c:\example.html
           $html.Add((Select-Object @{n='Application tag';e={$ApplicationTag}},@{n='Owner';e={$OwnerTag}},@{n='Cost';e={[string]$([int]$SumForOwner)+" Eu."}} -InputObject ''))
           Write-Host Owner is - $OwnerTag" Sum for the owner is:" $([int]$SumForOwner) "Eur"
    }
}; $html |  ConvertTo-Html -As Table -Title "Cost of subscriptions"  | Out-File c:\example.html

应该呈现以下 HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Cost of subscriptions</title>
</head><body>
<table>
<colgroup><col/><col/><col/></colgroup>
<tr><th>Application tag</th><th>Owner</th><th>Cost</th></tr>
<tr><td>Testing</td><td>john.johnson@contoso.com</td><td>15 Eu</td></tr>
<tr><td>Testing</td><td>tom.thompson@contoso.com</td><td>10 Eu</td></tr>
<tr><td>Testing 2</td><td>jim.jameson@contoso.com</td><td>40 Eu</td></tr>
<tr><td>Testing 2</td><td>eve.evens@contoso.com</td><td>40 Eu</td></tr>
<tr><td>Testing 2</td><td>charles.mcgill@contoso.com</td><td>20 Eu</td></tr>
</table>
</body></html>

在此处输入图像描述

第二次编辑作为 OP 需要一个新格式的 output,我们使用 hash 来打破应用程序标签并相应地格式化 output。

$ApplicationTags = ((Get-AzResource).Tags).Application | select -Unique
$html = [System.Collections.ArrayList]@()
$hash = @{}
Foreach ($ApplicationTag in $ApplicationTags) {
    $ConsumptionUsageDetail = (Get-AzConsumptionUsageDetail -StartDate (Get-Date).addmonths(-1) -EndDate 
    (Get-Date)) | Where-Object {$_.Tags -ne $null} | Where-Object {$_.Tags['Application'] -eq 
    $ApplicationTag}
    $SumForApplicationTag = 0
    $TotalCostPerApplicationTag = $ConsumptionUsageDetail.PretaxCost
    $TotalCostPerApplicationTag | ForEach {$SumForApplicationTag += $_}
    Write-Host "Application tag is"$ApplicationTag". Sum for the tag is:" $([int]$SumForApplicationTag) "Eur" 
        $OwnerTags = ((Get-AzResource -TagValue $ApplicationTag).Tags).Owner | select -Unique
        ForEach ($OwnerTag in $OwnerTags) {
           $ConsumptionUsageDetail = (Get-AzConsumptionUsageDetail -StartDate (Get-Date).addmonths(-1) - 
           EndDate (Get-Date)) | Where-Object {$_.Tags -ne $null} | Where-Object {$_.Tags['Application'] -eq 
             $ApplicationTag} | Where-Object {$_.Tags['Owner'] -eq $OwnerTag}
           $SumForOwner = 0
           $TotalCostPerOwnerTag = $ConsumptionUsageDetail.PretaxCost
           $TotalCostPerOwnerTag | Foreach {$sumforowner += $_}
           # We use the hash to check for break by ApplicationTag
           if (!$hash[ApplicationTag]) { 
               $hash[$ApplicationTag]=$True
               $html.Add((Select-Object @{n="Application tag";e={$ApplicationTag}},@{n="Owner";e={""}},@{n="Cost";e={[string]$([int]$SumForApplicationTag)+" Eu."}} -InputObject ''))
           }
           # Adding the owners
           $html.Add((Select-Object @{n="Application tag";e={""}},@{n='Owner';e={$OwnerTag}},@{n='Cost';e={[string]$([int]$SumForOwner)+" Eu."}} -InputObject ''))
           Write-Host Owner is - $OwnerTag" Sum for the owner is:" $([int]$SumForOwner) "Eur"
    }
}; $html |  ConvertTo-Html -As Table -Title "Cost of subscriptions"  | Out-File c:\example.html

应该呈现以下 HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Cost of subscriptions</title>
</head><body>
<table>
<colgroup><col/><col/><col/></colgroup>
<tr><th>ApplicationTag</th><th>Owner</th><th>Cost</th></tr>
<tr><td>Testing</td><td></td><td>25 Eu</td></tr>
<tr><td></td><td>john.johnson@contoso.com</td><td>15 Eu</td></tr>
<tr><td></td><td>tom.thompson@contoso.com</td><td>10 Eu</td></tr>
<tr><td>Testing 2</td><td></td><td>100 Eu</td></tr>
<tr><td></td><td>jim.jameson@contoso.com</td><td>40 Eu</td></tr>
<tr><td></td><td>eve.evens@contoso.com</td><td>40 Eu</td></tr>
<tr><td></td><td>charles.mcgill@contoso.com</td><td>20 Eu</td></tr>
</table>
</body></html>

应该呈现为:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM