achahbar
achahbar

Reputation: 991

Powershell combine data from an array into an object

I want to get data from 2 commands and combine them into 1 object. My goal is to get usage and cost of the Partner center. To do this I got the usage and cost with the command Get-PartnerCustomerSubscriptionUsage of the Partner center module. But when i Retrieve information of my customer/subscription I got the ugly ResourceID inside my report. And this is not presentable. This is why I need to get the real ResourceName, not the Resource name that is provided with the Get-PartnerCustomerSubscriptionUsage command.

After some digging in the documentation I got an idea to retrieve the ResourceUri that has the entire uri like /subscription/xxxxx/Resourcegroup/xxxx/ms.vm/The name that i want in my report. The command that has this value is : Get-PartnerCustomerSubscriptionUtilization. So I guessed that I just ditch the Get-PartnerCustomerSubscriptionUsage and use the PartnerCustomerSubscriptionUtilization instead , but this one does not have the totalcost per azure Resource.

Oké hang on with me the problem is getting there.

So now I created a Powershell script that will run the both commands , and combine them inside an Powershell object that will be exported to a csv. I can get one command running , providing the info from that command object into my custom object that is created and export it to csv. The problem is starting when I want to combine the both.

$Customers= Get-PartnerCustomer 
for ($i=0 ; $i -lt $Customers.length; $i ++){
    $subscription = Get-PartnerCustomerSubscription -CustomerId  $Customers[$i].CustomerId
    for ($j=0 ; $j -lt $subscription.length; $j ++){
        if ( $subscription[$j].UnitType -eq "Usage-based" )
        {
        #Create title in csv
        $customerId = $Customers[$i].CustomerId
        $customerName= $Customers[$i].Name
        $subscriptionId = $subscription[$j].SubscriptionId
        $subscriptionName = $subscription[$j].OfferName
        $usage = Get-PartnerCustomerSubscriptionUsage -CustomerId  $customerId  -SubscriptionId $subscriptionId
        #new object for the export excel
        $ExportExcel = New-Object -TypeName PSObject
        $array = New-Object -TypeName PSObject

                       $End=  (get-date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss-08:00")
                       $Start = (Get-Date).AddDays(-1).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss-08:00")
                       $util = Get-PartnerCustomerSubscriptionUtilization -CustomerId $customerId -SubscriptionId $subscriptionId  -StartDate $Start -EndDate $End -ShowDetails
                         for ($y=0 ; $y -lt $util.length; $y ++)
                         {
                            $array = [PSCustomObject][ordered]@{

                            "ResourceID"=$util[$y].Id
                            "ResourceName"=$util[$y].ResourceUri.OriginalString

                            } 

                         }

            for ($z=0 ; $z -lt $usage.length; $z ++)
            {
            $LastModifiedDate =  $usage[$z].LastModifiedDate.DateTime.ToString("yyyy-MM-dd")

                    if ( $LastModifiedDate -ge $Lastdate )
                    {

                        if ($usage[$z].ResourceId -eq $array[$z].ResourceID){
                          #Add-Member -InputObject $ExportExcel -MemberType NoteProperty -Name "Category" -Value $array[$z].ResourceName  -Force
                          **echo $array[$z].ResourceName**
                        }
                         Add-Member -InputObject $ExportExcel -MemberType NoteProperty -Name "Category" -Value $usage[$z].Category   -Force
                         Add-Member -InputObject $ExportExcel -MemberType NoteProperty -Name "QuantityUsed" -Value $usage[$z].QuantityUsed  -Force
                         Add-Member -InputObject $ExportExcel -MemberType NoteProperty -Name "ResourceId" -Value $usage[$z].ResourceId  -Force

                    }


            }


           $ExportExcel |  Export-Csv –append -Path "C:\$customername.csv" -NoTypeInformation

        }
    }
}

As you can see I've read everything from one command inside an object and then loop over the other one. Once the resourceID is equal over the both commands, I need to add it to the Object. (for testing I just test this with an echo) The echo with the ***** doesn't print anything. So I can't get any Resourcename inside my csv. Does anyone have a clue what I am doing wrong. Where the combination of the 2 objects fail ?

Upvotes: 1

Views: 318

Answers (1)

AdminOfThings
AdminOfThings

Reputation: 25031

Just for grins, I've made a few edits. Can you let me know if this improves the situation?

$Customers= Get-PartnerCustomer 
for ($i=0 ; $i -lt $Customers.length; $i ++){
    $subscription = Get-PartnerCustomerSubscription -CustomerId  $Customers[$i].CustomerId
    for ($j=0 ; $j -lt $subscription.length; $j ++){
        if ( $subscription[$j].UnitType -eq "Usage-based" )
        {
        #Create title in csv
        $customerId = $Customers[$i].CustomerId
        $customerName= $Customers[$i].Name
        $subscriptionId = $subscription[$j].SubscriptionId
        $subscriptionName = $subscription[$j].OfferName
        $usage = Get-PartnerCustomerSubscriptionUsage -CustomerId  $customerId  -SubscriptionId $subscriptionId
        #new object for the export excel
        $ExportExcel = New-Object -TypeName PSObject
        $array = @()
                   $End=  (get-date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss-08:00")
                   $Start = (Get-Date).AddDays(-1).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss-08:00")
                   $util = Get-PartnerCustomerSubscriptionUtilization -CustomerId $customerId -SubscriptionId $subscriptionId  -StartDate $Start -EndDate $End -ShowDetails
                     for ($y=0 ; $y -lt $util.length; $y ++)
                     {
                        $array += [PSCustomObject][ordered]@{

                        "ResourceID"=$util[$y].Id
                        "ResourceName"=$util[$y].ResourceUri.OriginalString

                        } 

                     }

        for ($z=0 ; $z -lt $usage.length; $z ++)
        {
        $ExportExcel = New-Object -TypeName PSObject
        $LastModifiedDate =  $usage[$z].LastModifiedDate.DateTime.ToString("yyyy-MM-dd")

                if ( $LastModifiedDate -ge $Lastdate )
                {
                    if ($usage[$z].ResourceId -eq $array[$z].ResourceID){
                      #Add-Member -InputObject $ExportExcel -MemberType NoteProperty -Name "Category" -Value $array[$z].ResourceName  -Force
                      **echo $array[$z].ResourceName**
                    }
                     Add-Member -InputObject $ExportExcel -MemberType NoteProperty -Name "Category" -Value $usage[$z].Category   -Force
                     Add-Member -InputObject $ExportExcel -MemberType NoteProperty -Name "QuantityUsed" -Value $usage[$z].QuantityUsed  -Force
                     Add-Member -InputObject $ExportExcel -MemberType NoteProperty -Name "ResourceId" -Value $usage[$z].ResourceId  -Force
                     $ExportExcel |  Export-Csv –append -Path "C:\$customername.csv" -NoTypeInformation
                }


        }




    }
}
}

I edited the line below to retain all values for each iteration of loop $y.

$array += [PSCustomObject][ordered]@{
          "ResourceID"=$util[$y].Id
          "ResourceName"=$util[$y].ResourceUri.OriginalString
          } 

I added the line below so that a new $ExportExcel object could be created to accept your new property additions during loop $z.

$ExportExcel = New-Object -TypeName PSObject

I moved the CSV export line to inside of the $z loop so that each iteration of setting properties for $ExportExcel could be captured.

Upvotes: 1

Related Questions