Home > Back-end >  add line on existing array with custom value
add line on existing array with custom value

Time:10-05

I need some improvements for the array, I want to add on an Array an extra Line with custom value like the sum for the costs... The way I did, is not the best way, can someone help me?

$subs = Get-AzSubscription 

$currentBillingPeriod = Get-AzBillingPeriod -name 202209 #-MaxCount 1
$startDate = $currentBillingPeriod.BillingPeriodStartDate.ToString("dd-MM-yyyy")
Write-Host "currentBillingPeriod startDate : " $startDate
$endDate = $currentBillingPeriod.BillingPeriodEndDate.ToString("dd-MM-yyyy")
Write-Host "currentBillingPeriod endDate : " $endDate

$report = @()

foreach ($sub in $subs){
$x = "" | Select 'Subscription','cost in Euro','Period'

$acSub = Set-AzContext -SubscriptionName $sub.name
$currentCost = Get-AzConsumptionUsageDetail -StartDate $startDate -EndDate $endDate | Measure-Object -Property PretaxCost -Sum

 $x.Subscription = $sub.name
 $x.'cost in Euro' = [math]::round($currentCost.Sum,2)
 $x.'Period' = $currentBillingPeriod.name
 $report  = $x 


Write-Host "Current Cost of Subscription :" $sub.name $currentCost.Sum
}

$report
$mailreport = $report

$sum = ($mailreport |  Measure-Object 'cost in Euro' -Sum).sum
$mailreport  = '-'
$mailreport  = '-'
$mailreport  = 'Summe in Euro'
$mailreport  = $sum
$mailreport

CodePudding user response:

I think you need to make a custom ps object, where you pour in the contents from the array, as well as the extra property you need

# Create new array
$Array = @()

# Add a custom object for the array for each subscription
$Array  = New-Object psobject -Property @{Subscription = 'SubName'  ;Cost= $value; Sum = $value}

# Output array
$Array | Out-Default

CodePudding user response:

one question ist, how can I add the Value $sum into the secound column 'cost in Euro' if I do it in the simple way =$sum it will be placed into the first column.

for example it should look like this

or, a other way could be to calculate it already in the array?

array

  • Related