【发布时间】:2021-06-25 13:58:23
【问题描述】:
我根据我们环境中使用的两个标签(在本例中为应用程序标签和所有者标签)编写了用于 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 代码的输出是:
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
...等等...
现在我需要以某种方式将此输出流式传输到 HTML 表,我尝试使用 ConvertTo-HTML 命令执行此操作,但它会不断用最后一个输出重写自身,并且不会以任何方式填充表。
我还尝试将 ForEach 循环制作成这样的数组:
$ApplicationTagLoop = @(foreach ($i in $ApplicationTag)
并将其与 Convert-To-HTML 一起使用,但这样做 $ApplicationTagLoop 根本不会提供任何结果,因此不会将任何内容转换为 HTML。
如何重写 ConvertTo-HTML 部分,以便将循环的每个输出保存到 HTML 文件的新行中?
所需的输出格式应为: https://i.stack.imgur.com/z13SM.png
【问题讨论】:
-
生成的表格应该是什么样子?
-
我修改了原始问题,以便更好地理解最终输出的外观。是否可以修改 $html 变量(如 Zilog80 回复中所见)以产生所需的结果?
标签: html powershell loops converters script