Powershell:将 ForEach 循环结果转换为 HTML table
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 代码的输出是:
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 table,我尝试使用 ConvertTo-HTML 命令进行此操作,但它会不断用最后的输出重写自身并执行不以任何方式填充 table。
我还尝试将 ForEach 循环制作成数组,如下所示:
$ApplicationTagLoop = @(foreach ($i in $ApplicationTag)
并将其与 Convert-To-HTML 一起使用,但以这种方式 $ApplicationTagLoop 根本不提供任何结果,因此没有任何内容被转换为 HTML.
如何重写 ConvertTo-HTML 部分,以便将循环的每个输出保存到 HTML 文件的新行中?
所需的输出格式应为:
https://i.stack.imgur.com/z13SM.png
查看代码,我猜是预期的输出。
使用对象集合构建获得正确的 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 需要一个新的格式化输出,我们使用散列来按应用程序标签打断并相应地格式化输出。
$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>
应该呈现为:
我编写了一个基于我们环境中使用的两个标签(在本例中为应用程序标签和所有者标签)的 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 table,我尝试使用 ConvertTo-HTML 命令进行此操作,但它会不断用最后的输出重写自身并执行不以任何方式填充 table。
我还尝试将 ForEach 循环制作成数组,如下所示:
$ApplicationTagLoop = @(foreach ($i in $ApplicationTag)
并将其与 Convert-To-HTML 一起使用,但以这种方式 $ApplicationTagLoop 根本不提供任何结果,因此没有任何内容被转换为 HTML.
如何重写 ConvertTo-HTML 部分,以便将循环的每个输出保存到 HTML 文件的新行中?
所需的输出格式应为: https://i.stack.imgur.com/z13SM.png
查看代码,我猜是预期的输出。
使用对象集合构建获得正确的 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 需要一个新的格式化输出,我们使用散列来按应用程序标签打断并相应地格式化输出。
$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>
应该呈现为: