locked
How to add lines at the end of an auto generated html file. RRS feed

  • Question

  • Good day,

    I have created a script taking all the phone  numbers from our AD account. That file is generated daily in an html format and is available to all the organisations. My problem is that some numbers exist but they are not in AD. I was wondering how I could add lines at the end of my file everytime a new file is generated. Here is a copy of my actual script:

    *************************************************************************

    #Define location of my script variable
    #the -parent switch returns one directory lower from directory defined. 
    #below will return up to ImportADUsers folder 
    #and since my files are located here it will find it.
    #It failes withpout appending "*.*" at the end

    $path = Split-Path -parent "\\vbdf-ad11\NETLOGON\rep_tel\*.*"

    #Create a variable for the date stamp in the log file

    $LogDate = get-date -f yyyyMMddhhmm

    #import the ActiveDirectory Module

    Import-Module ActiveDirectory


    #Définit le OU où les comptes seront traités.
    $SearchBase = "OU=Utilisateurs,OU=VBDF,DC=XXXXXXXXXX,DC=lan"  

    #Define variable for a server with AD web services installed
    $ADServer = 'VBDF-AD11'

    # À Where object, j'indique que les comptes qui n'ont pas de poste téléphonique (ipPhone) doivent être exclus de la liste
    $AllADUsers = Get-ADUser -server $ADServer `
    -searchbase $SearchBase `
    -Filter * -Properties * | Where-Object {$_.ipPhone -ne $Null} 

    $AllADUsers | Sort-Object -Property Surname |  
    Select-Object @{Label = "Prénom";Expression = {$_.GivenName}},
    @{Label = "Nom";Expression = {$_.Surname}},
    #@{Label = "Nom complet";Expression = {$_.DisplayName}},
    @{Label = "Cellulaire";Expression = {$_.mobile}},
    @{Label = "Poste";Expression = {$_.ipPhone}},
    @{Label = "Courriel";Expression = {$_.Mail}},

    #Convert data to an HTML file
    Get-Process | 
    ConvertTo-Html Nom,Prénom,Poste,Cellulaire,Courriel, -body "<H2>Liste téléphonique Ville de Bois-des-Filion</H2>" -CssUri \\vbdf-ad11\NETLOGON\rep_tel\style.css | 
    Set-Content \\vbdf-ad11\NETLOGON\rep_tel\liste_tel.htm

    ***************************************************************************

    How could I add information that would insert itself in the table that is automatically generated? (see below) (BTW, the phone numbers are fake! :)  )

      .

    Thanks in advance for your help.

    Dag



    Tuesday, March 6, 2018 7:52 PM

Answers

  • Hi,

    If you have any updates during this process, please feel free to let me know.
    And if there is anything else we can do for you, please feel free to post in the forum.

    Best Regards,
    Albert

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Thursday, March 22, 2018 5:49 AM

All replies

  • There is no table.  The table you see is just what is generated by the script.  It is not editable.

    Also, what you are asking doesn't make sense.  Are you asking how you can save the data and then discover what is new and old?


    \_(ツ)_/

    Tuesday, March 6, 2018 8:32 PM
  • Hi Jrv,

    Thank you for your answer. I think that yes there is a table. Please take a look at the html script. We can see that a table is called:

    Start of the script:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <H2>Liste téléphonique Ville de Bois-des-Filion</H2>
    <link rel="stylesheet" type="text/css" href="file://vbdf-ad01/NETLOGON/rep_tel/style.css" />
    </head><body>
    <table>
    <colgroup>
    <col/>
    <col/>
    <col/>
    <col/>
    <col/>
    </colgroup>
    <tr><th>Nom</th><th>Prénom</th><th>Poste</th><th>Cellulaire</th><th>Courriel</th></tr>

    ......

    End of script:

    </table>
    </body></html>

    As you can see the data seems generated directly in a table.

    What I was wondering is that after that table is created if it is possible to add lines that would look like this:

    Janitor  ext 121 cell: 555-555-7777
    Night Janitor ext 122 cell: 555-666-9999

    If it could be generated directly in the table that would be great if not, simply adding that information at the end of the script would be OK too.

    With that added information do you think that is possible?

    Have a great day

    Dag


    Wednesday, March 7, 2018 3:54 PM
  • There is no data table that can be edited as data.  What you are seeing is format instruction.  That HTML formatted text only exisits after you run the code and it is not editable by any normal means.

    As suggested output to a CSV which can be appended to then generate the HTML from the updated CSV file.


    \_(ツ)_/

    Wednesday, March 7, 2018 4:00 PM
  • First, Use code posting tool for posting code snippets in the forum.

    Create a custom object with same column headers and pass it to ConvertTo-HTML like below.

    #List from AD
    $FromAD = $AllADUsers | Sort-Object -Property Surname |  
     Select-Object @{Label = "Prénom";Expression = {$_.GivenName}},
     @{Label = "Nom";Expression = {$_.Surname}},
     #@{Label = "Nom complet";Expression = {$_.DisplayName}},
     @{Label = "Cellulaire";Expression = {$_.mobile}},
     @{Label = "Poste";Expression = {$_.ipPhone}},
     @{Label = "Courriel";Expression = {$_.Mail}}
    
    
    $AdditionalList = New-Object -TypeName PSObject -Property @{
    'Nom' = 'new surname'
    'Prénom' = 'newname'
    'Poste' = 3343
    'Cellulaire' = 1233434
    
    
    }
    
    $FromAD,$AdditionalList | ConvertTo-Html Nom,Prénom,Poste,Cellulaire,Courriel, -body "<H2>Liste téléphonique Ville de Bois-des-Filion</H2>" -CssUri \\vbdf-ad11\NETLOGON\rep_tel\style.css | 
     Set-Content \\vbdf-ad11\NETLOGON\rep_tel\liste_tel.htm
    


    Regards kvprasoon

    Wednesday, March 7, 2018 7:20 PM
  • Thank you for your answer Prasoon!

    I have tried different ways to insert your code but I just cannot make it work.

    Do I have to add the 

    $FromAD,$AdditionalList | ConvertTo-Html Nom,Prénom,Poste,Cellulaire,Courriel, -body "<H2>Liste téléphonique Ville de Bois-des-Filion</H2>" -CssUri \\vbdf-ad11\NETLOGON\rep_tel\style.css | 
     Set-Content \\vbdf-ad11\NETLOGON\rep_tel\liste_tel.htm

    directly below my already existing other ConvertTo-Html line or do I need to replace it? I tried both without success but I am certain that I have missed something.

    About the $AdditionaList object, I have tried too to insert it without success. If I understand correctly, the object $AdditionalList will add the new data with the one generated with AD and after that, will create the html file. Am I correct with that analysis?

    I am not  familiar with programming and much less with Powershell so I am sorry for my ignorance. Your help is really appreciated.

    Have a great day

    Dag

     
    Monday, March 12, 2018 3:28 PM
  • I would advice you to start learning PowerShell.

    This is a very good place to start.

    Yes, you should replace it. From high level,

    1. Get the required details from the DC and store it in a variable

    2. Build the custom object with custom values with same headers in step 1 and store it in a variable.

    3. Convert to html with the snippet I shared by including both step 1 and step 2 output(step1,step2)


    Regards kvprasoon

    Monday, March 12, 2018 7:36 PM
  • There is also another method of generating a combined table.  Use ConvertTo-Html to generate fragments and add them to a single file.  HTML will combine fragments into the file.

    $frag2 = Get-Process s*|select name,displayname,path | ConvertTo-Html -Fragment
    $frag1 = Get-Process m* |select name,displayname,path |ConvertTo-Html -Fragment
    ConvertTo-Html -Body ($frag1+$frag2)|Out-File test.html
    .\test.html


    \_(ツ)_/

    Monday, March 12, 2018 8:09 PM
  • You can eliminate the duplicate headers like this:

    $all = $frag1[0..($frag1.Count-2)] + $frag2[3..($frag2.Count)]
    ConvertTo-Html -Body $all |Out-File test.html
    .\test.html


    \_(ツ)_/

    Monday, March 12, 2018 8:18 PM
  • Hi JRV,

    Thanks for this update.

    FYI

    I have been sick for the last week and just came back to work earlier this week. I have some catchup to do and I will work on the script when I will have some free time.


    Wednesday, March 21, 2018 3:52 PM
  • Hi,

    If you have any updates during this process, please feel free to let me know.
    And if there is anything else we can do for you, please feel free to post in the forum.

    Best Regards,
    Albert

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Thursday, March 22, 2018 5:49 AM
  • Hi guys,

    Thanks for your help. I have finally decided to create dummys account in AD located in a specific OU. That way, I can add my users with phone numbers directly through the powershell script originally created. 

    Have a great day

    Dag

    Tuesday, March 27, 2018 2:05 PM