skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    I found a solution any improvements?

    $sum = ($report |  Measure-Object 'cost in Euro' -Sum).sum
    
    $report 
    $total = New-Object PSObject
    $total | Add-Member -MemberType NoteProperty -Name "cost in Euro" -Value $sum
    $report += ''
    $report += $total
    

  2. 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search