0

I have a small loop in powershell produces an object with 4 elements

PS'>$object
DMC : 5061
Conv : 6.94%
Sales : 351
OptIn : 185

The loop will run twice and produce data for Team A and Team B (and per haps Teams C and D at a later date), I would like to create an output that looks like this, where I can just add columns as I need.

        Team A    Team B
DMC     5061      5612
CONV    9.62%     6.3%
Sales   351       320
Optin   185       220

I can use | format-table to get output that looks something like this

       DMC CONV Sales Optin
Team A 
Team B

I have been able to do something similar by directly manipulating Excel as a COM object but it's both massive over kill and doesn't produce what I need.

Ultimately I want to convert the output into HTML and send it as an email body (which I'm able to do if I can get the table into a format I can use).

I feel there is probably a very simple way to do this buty I'm absolutely making no headway.

2 Answers 2

3

The easy solution here is to create a PowerShell Custom object. You simply provide a hashtable of the values you want and away you go!

[psCustomObject]@{TeamName="TeamA";DMC=5061; Conv="9.62%"; Sales=351;OptIn=185} 
[psCustomObject]@{TeamName="TeamB";DMC=5612; Conv="6.3%"; Sales=320;OptIn=220} 

It will render up like this

TeamName  DMC Conv  Sales OptIn
--------  --- ----  ----- -----
TeamA    5061 9.62%   351   185
TeamB    5612 6.3%    320   220

But you wanted these in a different format, this is where transposition comes from. Fortunately, someone did the hard work for us, with the fabulous Transpose-Object cmdlet.

It is available on github here

You use it by piping an object or array of objects into it, like so:

[psCustomObject]@{TeamName="TeamA";DMC=5061; Conv="9.62%"; Sales=351;OptIn=185},
[psCustomObject]@{TeamName="TeamB";DMC=5612; Conv="6.3%"; Sales=320;OptIn=220} | Transpose-Object 

TeamName TeamA                                                 TeamB
DMC      5061                                                  5612 
Conv     9.62%                                                 6.3% 
Sales    351                                                   320  
OptIn    185                                                   220  

If you wanted to use it with your loop, then add each teams custom object to an array, like this

#start of your script
$teams= new-object -Type System.Collections.Arraylist

forEach($team in $teams){

   #do stuff to calculate your custom object
   $teams.Add([psCustomObject]@{TeamName="TeamA";DMC=5061; Conv="9.62%"; Sales=351;OptIn=185})
}

#end of your script
$teams | Transpose-Object

Sign up to request clarification or add additional context in comments.

Comments

0

Use a PSCustomObject

[PSCustomObject]@{
    DMC = "objvalue here"
    Conv = "objvalue here"
    Sales = "objvalue here"
    OptIn = "objvalue here"}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.