Custom object to html report

This topic contains 9 replies, has 2 voices, and was last updated by Profile photo of Wei-Yen Tan Wei-Yen Tan 10 months, 1 week ago.

  • Author
    Posts
  • #34809

    Hi,

    I'm creating a script that iterates over some folders and does a command git status.

    The result is added to a custom object. See code:

    $files = Get-ChildItem;
    echo $files;

    $resultArray = @();

    for ($i=0; $i -lt $files.Count;$i++) {

    cd $files[$i].FullName;
    echo In: $files[$i].FullName;
    $cmdResult = New-Object System.object;
    $gitResponse = git status | Out-String;

    $cmdResult | Add-Member -type NoteProperty -name Name -Value $files[$i].FullName;
    $cmdResult | Add-Member -type NoteProperty -name GitStatus -Value $gitResponse;
    $resultArray += $cmdResult;
    cd ..
    }

    My question is how can I create an html of the $resultArray.

    It must be simple to do but my skills on powershell are limited. (just started learning it)

    Yours,

  • #34810
    Profile photo of Wei-Yen Tan
    Wei-Yen Tan
    Participant

    You will have to pipe it through to an Convertto-HtML fragment.. like this

    $resultArray | ConvertTo-Html -Fragment 
    

    (of course you can add your other components...check the help on Convert-HTML parameters.)

    Then once you have created all your fragments you would create another variable

    $report = Convertto-HTML -Body "$ResultArray"
    

    to out put to a file you would pipe it to out-file

    $report | out-file  -path c:\temp\report.html
    

    In a nutshell you are creating a table out of the object (which is what what the parameter fragment does) and then converting that to HTML by using Convertto-HTML.

    Hope this helps 🙂

    EDIT Edited some errors in cmdlets

  • #34811

    I get.

    Convert-HTML : The term 'Convert-HTML' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and
    try again.

  • #34812
    Profile photo of Wei-Yen Tan
    Wei-Yen Tan
    Participant

    Try Convertto-HTML

    You can get the specific command if you check Get-Command *HTML

  • #34816

    It works. Except the object is printed in the html file and not the attributes name and GitStatus

    need to figure out how to do that.

  • #34817
    Profile photo of Wei-Yen Tan
    Wei-Yen Tan
    Participant

    I don;t think I am following you... does the values appear?

    if output the object as in

    $ResultArray | Format-Table

    what does it produce?

  • #34818

    Hi,

    I get:

    System.Object System.Object System.Object

    My code is:

    $files = Get-ChildItem;
    echo $files;

    $resultArray = @();

    for ($i=0; $i -lt $files.Count;$i++) {

    cd $files[$i].FullName;
    echo In: $files[$i].FullName;
    $cmdResult = New-Object System.object;
    $gitResponse = git status | Out-String;

    $cmdResult | Add-Member -type NoteProperty -name Name -Value $files[$i].FullName;
    $cmdResult | Add-Member -type NoteProperty -name GitStatus -Value $gitResponse;
    $resultArray += $cmdResult;
    cd ..
    }
    $report = ConvertTo-HTML -Body "$ResultArray";

    $report | out-file -path c:\tmp\result.html

  • #34821
    Profile photo of Wei-Yen Tan
    Wei-Yen Tan
    Participant

    Ok I think whats happening here is that your object is not being created properly.

    When you create custom objects...your variables you have to populate them with proper objects not external commands.

    The way I understand producing Custom Object is that you are basing it off other validated objects and then piecing it together.

    So I am actually not sure $gitresponse would even produce a proper value because in that form powershell would not know how to interpret git status.

    I don't have git but the value git status would not produce objects. Its an external command and as such Powershell doesnt know how to deal with them.

    Below is a short example of creating a custom object of three activedirectory groups and getting the counts.

    #region Variables
    $groupa = Get-ADGroup GroupA |Get-ADGroupMember 
    $groupb = Get-ADGroup GroupB |Get-ADGroupMember
    $groupc = Get-ADGroup GroupC |Get-ADGroupMember
    #endregion
    $AllGroupReport = @()
    $AllGroupReport += New-Object psobject -Property @{
        GroupA = $groupa.count;
        GroupB = $groupb.count;
        GroupC = $groupc.count;
    }
    

    Why would this work? Because the values I chose for the variables is based off objects.
    The value of the variable $GroupA is actually Get-ADGroup GroupA |Get-ADGroupMember and this is not the same as listing text.

    Once your values are based off objects then it will start to work.

    I suggest that you work through a book like Powershell in a month of Lunches by Don Jones and watch his accompanying videos on Youtube

    https://www.youtube.com/user/powershelldon/videos

    Also I would strongly suggest that you go through the MVA training on Powershell.

    https://mva.microsoft.com/en-us/training-courses/getting-started-with-powershell-3-0-jump-start-8276?l=r54IrOWy_2304984382

    If you want to jump start into objects I found some links on youtube that may help you.

    Chapter 19 – Custom Output Objects.mp4 (Powershell in a month of Lunches)

    and 04 | Objects for the Admin in PowerShell
    in Microsoft Virtual Academy
    https://mva.microsoft.com/en-us/training-courses/getting-started-with-powershell-3-0-jump-start-8276?l=r54IrOWy_2304984382

    I hope this helps .... 🙂

  • #34822

    Hi,

    Thank you for all the help. Did see the training https://mva.microsoft.com/en-us/training-courses/getting-started-with-powershell-3-0-jump-start-8276?l=r54IrOWy_2304984382 a while ago. That's the reason I try to get this solved by using powershell.

    I will just figure it, will be a good practice on powershell.

    Yours,

  • #34866
    Profile photo of Wei-Yen Tan
    Wei-Yen Tan
    Participant

    No problem...One thing I suggest that you do is to pipe the cmdlet you want to use to Get-Member. Like the format:

    Get-Childitem c:\|Get-Member

    See the input that it produces...If it produces a result like a standard cmdlet then it will work. If it just sits there, then most likely it is not a candidate for use in a custom object.

    I am not quite sure if you have installed posh-git module..download from the powershell gallery install-module posh-git.

    If you have check to see the object that it produces from the cmdlets you want to utilize from the github module using the example I showed above

    If it produces any validated results then you are good to go.

    Best of luck with your journey into Powershell....things start to make sense eventually. I am still learning as well

You must be logged in to reply to this topic.