locked
Noobish Convertto-Html Issue RRS feed

  • Question

  • Well, I've managed to get myself a bit stuck again.

    I'm 'trying' to output a file with specific user information before the script makes any changes. Currently I have it outputting to a raw text file & I thought this would be a good time to 'pretty' it up a bit and learn some HTML powershell tricks at the same time!

    So I'm able to pull the info I want from the main object, and put it into an htm file! Great!

    Try not to hassle me too much on how this code is formatted, I'm still just playing around with how it works!

    $Outputfile = "Test.htm"
    
    $a = @"
    <style>
    BODY{background-color:white;}
    TABLE{border-width: 1px;
      border-style: solid;
      border-color: black;
      border-collapse: collapse;
    }
    </style>
    "@
    
    ### MEMBER OF LOG  
    $Domaininfo = Get-Qaduser -service domain.ad -identity $user
    $Header = "DOMAIN GROUPS USER WAS A MEMBER OF"
    
    $body = @"
    <p style="font-size:25px;family:calibri;color:#08088A">
    $Header
    </p>
    "@
    
    $Domaininfo.Memberof -replace "(CN=)(.*?),.*",'$2' | FL * | convertto-html -property * -head $a -body $body -As list > $Outputfile
    ### END MEMBER OF
    
    ##DOMAIN USER INFO LOG
    $Header = "USER INFO"
    
    $body = @"
    <p style="font-size:25px;family:calibri;color:#08088A">
    $Header
    </p>
    "@
    
    $DOMAINinfo | convertto-html -head $a -body $body -Property DisplayName,givenName,LastName,Company,Department,Title,Manager,sAMAccountName,email,`
    telephoneNumber,fax,mobile,office,StreetAddress,City,StateOrProvince,PostalCode,wWWHomePage,StateOrProvince,HomeDirectory,HomeDrive,ProfilePath,`
    LogonScript,whenChanged,whenCreated,AccountExpires,PasswordLastSet,PasswordAge,PasswordExpires,LastLogon,PasswordStatus,Path,Sid -As list >> $Outputfile
    
    ##END DOMAIN USER INFO LOG

    Now the 'Domain User Log' works just peachy!

    The member of, not so much. I'm getting what looks like some random numbers being output. I tried removing almost everything from that command as well:

    $Domaininfo.Memberof | convertto-html -As list > $Outputfile

    Different result, but still not what I'm after...

    I'm sure it's something simple, any help would be appreciated!

    Thanks!

    Saturday, June 2, 2012 5:21 PM

Answers

  • Here is a sampler of easy techniques that can make HTML reports with PowerShell very powerful and relatively easy to build.  I t demonstrates how to get multiple tables into a single HTML report.  It also shows the correct way to set up each bit of a  report.  It happens to use your user info report and memberof report as a demo.

    Check it out: http://gallery.technet.microsoft.com/scriptcenter/PowerShell-HTML-User-b5705336


    ¯\_(ツ)_/¯

    • Marked as answer by Phreak2600 Sunday, June 3, 2012 4:26 AM
    Sunday, June 3, 2012 12:25 AM

All replies

  • You seem to be on the right track.

    You have applied no formatting to the 'MemberOf' list.  What is it that you are expecting?  Yuo can add a style sheet to your output to make it colorful.


    ¯\_(ツ)_/¯

    Saturday, June 2, 2012 7:27 PM
  • I think I see yout issue:

    Try this:
    $Domaininfo.memberof|%{get-qadgroup $_}|select Name,ParentContainer|ConvertTo-Html


    ¯\_(ツ)_/¯


    • Edited by jrv Sunday, June 3, 2012 12:25 AM
    Saturday, June 2, 2012 7:46 PM
  • Here is a sampler of easy techniques that can make HTML reports with PowerShell very powerful and relatively easy to build.  I t demonstrates how to get multiple tables into a single HTML report.  It also shows the correct way to set up each bit of a  report.  It happens to use your user info report and memberof report as a demo.

    Check it out: http://gallery.technet.microsoft.com/scriptcenter/PowerShell-HTML-User-b5705336


    ¯\_(ツ)_/¯

    • Marked as answer by Phreak2600 Sunday, June 3, 2012 4:26 AM
    Sunday, June 3, 2012 12:25 AM
  • Due to your finding an interesting bug in ConvertTo-Html we need to use this workaround to get teh table to work.

    $Domaininfo.memberof|
         %{get-qadgroup $_}|
         select Name,ParentContainer|
         ConvertTo-Html

    We need to select at least two columns. The bug happens when only one column is selected.


    ¯\_(ツ)_/¯

    Sunday, June 3, 2012 12:27 AM
  • Thanks!

    This worked rather well, I had to remove the parent container as the groups need to be easily copied and pasted back into ADUC if an account needs to be re-activated. Other then that I seem to get a '*' at the top of the groups list in the htm file, but that is a small price to pay. :)

    Sunday, June 3, 2012 4:36 AM
  • So I've played around with this some more and have more questions...


    $groups=$user.Memberof | %{get-qadgroup $_} |select @{N='Group Name';E={$_.Name}}, parentcontainer | 
    ConvertTo-Html -fragment -pre '<div id="groups"><br /><br />' -post '</<div>'

    So, this is taking each group the user is a member of, preforming a Get-QADGroup on it for the name and parent container..

    While this works, it takes a long time to parse through the groups when there are 30+ present. On top of that I'll have to run this against 4 domains. Is there not a more efficient way to do this?

    I also need to pull some .directoryentry information to the log as well, so I tried this.

    $CustomInfo = $user.DirectoryEntry |`

    convertto-html -property IpPhone,Logoncount,badpwdcount,employeenumber`

    -as list -fragment -pre "<div id='detail'><h2>DOMAIN.AD Custom Info For $($user.DisplayName)</h2>" -post '</div>'

    Then added it to the string, but the output is not what I expected..

    I'm still very confused it seems..

    IpPhone:

    System.DirectoryServices.PropertyValueCollection

    Logoncount:

    System.DirectoryServices.PropertyValueCollection

    badpwdcount:

    System.DirectoryServices.PropertyValueCollection

    employeenumber:

    System.DirectoryServices.PropertyValueCollection





    • Edited by Phreak2600 Sunday, June 3, 2012 7:11 AM
    Sunday, June 3, 2012 7:09 AM
  • That's right.  That won't work and cannot be made to work.

    That is one reason  why I posted teh demo.  It contains a number of techniques.  Most other simlpe methods won't work and teh raw DirectoryEntry is unusable vecause all values are delivered as collcetions which have to beunwrapped.

    See this link for a demo of what is happening and one way to address it.

    http://gallery.technet.microsoft.com/scriptcenter/Extract-arbitrary-list-of-6f59d3b4


    ¯\_(ツ)_/¯

    Sunday, June 3, 2012 8:48 AM
  • I actually had some marginal success with this the more I played around with it

    $groups = $user.Memberof -replace "(CN=)(.*?),.*",'$2'

    I just left out the convertto-html completely and removed the path info so only the display name was present

    It worked, more so then before. Everything I wanted to pass did, and displayed in the htm file. However it did it with no formatting, just strings of lines that contains the names of the groups.

    $body = $($groups|Out-String)
    ConvertTo-Html -title 'Disable User Report' -head $style -pre "<div id='wrapper'>" -body $body -post '</div>' > $outfile
    I messed around some more trying to get it to write in a list format, without much success.

    Heck, even having the group names delimited with a semi-colon would work for me at this point... lol



    • Edited by Phreak2600 Sunday, June 3, 2012 9:38 AM
    Sunday, June 3, 2012 9:37 AM
  • I really don't understand why you are doing this.  I posted a link to a complete solution including all groups. It run very fast.  With 30+ grousp it may take about 30 seconds to display but that is not an issue.

    If you want more compplex HTML solutions you will need to dump to XML and use an XSLT to format the report.  The PowerShell components are not capable of extremly complex scenarios.  You are limited to multiple tables and lists and it is up to yuor skill with CSS2 as to how well the output is formatted.

    If you want a more controllable format then use Excel.  DUmp the data to excel named ranges and use an Excel template to generate a foramtted report.  You can aslo do this with Word.

    I have done it by extracting to an Acess database trhen use an Access report which can be designed with a nice GUI report designer.

    If you just want to extract the propewrties then use dsquery and/or dsget as they extract to CSV and can extract thw memberof to a semicolon sepearated list in the CSV.

    What you are trying to do is pretty much a mismatch because of the bug in Convert and because the DirectoryEntry is wrapped property collections.  These can beexpanded but only one item can be expanded ata time.

    The second link I gave you can expand a proeprty collection but, by default, it only expands the first element.  This can be easily modifed to expand all element to a pipe or semi-colon separated string.  This would be the easy path for you.  Just stop using Quest and use my proeprty extractor. Put everything in one list object and alter teh CSS to allow the TD int eh table to wrap and expand to accomodate the longer length strings. 

    You could also custom generate all HTML.

    We have to recognize when a tool is actually prevernting a quick solution and look for a more appropriate tool.

    The old saying goes something liek this: "For the old carpenter, the solution to every problem includes a hammer".  Don't be that carpenter.  Stop usign a hammer and get a more nail gun. If that doesn't work use paint brush.  Find a tool that matches your requirements instead of trying to force the requirements in to the wrong tool or just acccept what you have as good enough.


    ¯\_(ツ)_/¯

    Sunday, June 3, 2012 10:14 AM