question

matteu31400 avatar image
0 Votes"
matteu31400 asked AndreasBaumgarten edited

Powershell and HTML report

Hello,

I would like to know if what I do is "correct" and the good way to do.
It works, but I'm not powershell dev and I would like some advices pls :)

 #Requires -Modules DhcpServer
 # =======================================================
    
    
 $htmlReportPath = "c:\DNSReport.html"
    
 function Get-DHCPServersSettings
 {
     $allDHCP = Get-DhcpServerInDC
     $allDHCP | Foreach-Object {
         [PSCustomObject]@{
             Name = $_.DnsName
             DBPath =  (Get-DhcpServerDatabase -ComputerName $_.Dnsname).FileName
             BackupPath = (Get-DhcpServerDatabase -ComputerName $_.Dnsname).BackupPath
             Logging = (Get-DhcpServerDatabase -ComputerName $_.Dnsname).LoggingEnabled
             AuditState = (Get-DhcpServerAuditLog -ComputerName $_.Dnsname).Enable
             AuditPath = (Get-DhcpServerAuditLog -ComputerName $_.Dnsname).path
         }
     }
 }
    
    
 $DHCPServersSettings = Get-DHCPServersSettings
    
    
 #region HTML
 @"
 <!DOCTYPE html>
 <html>
 <head>
 </head>
 <body>
 <h1>DHCP Report</h1>
    
 <h3>DHCP Servers Settings</h3>
 $($DHCPServersSettings | ConvertTo-Html -Fragment)
    
    
    
 </body>
 "@ | Out-File -Encoding utf8 $htmlReportPath
 #endregion
    
 Invoke-Item $htmlReportPath

Is it better to create custom object or just use "command | select A,B,C" ?
Does my method to build my HTML is ok or not ?

I know there are not any comment, I need to add it :)


Thanks for all advices you can give me :)



windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndreasBaumgarten avatar image
1 Vote"
AndreasBaumgarten answered AndreasBaumgarten edited

@matteu31400 ,

there is maybe one thing I would modify/change/try.
In your function you are using this three times (Get-DhcpServerDatabase -ComputerName $_.Dnsname) and this two times (Get-DhcpServerAuditLog -ComputerName $_.Dnsname) .
Each line will do a new "get-query".

Maybe you like to test the function below. This will run each "get-query" only one time.

   function Get-DHCPServersSettings
  {
      $allDHCP = Get-DhcpServerInDC
      $allDHCP | Foreach-Object {
          $DHCPServerDB = Get-DhcpServerDatabase -ComputerName $_.Dnsname
          $DHCPServerAuditLog = Get-DhcpServerAuditLog -ComputerName $_.Dnsname
          [PSCustomObject]@{
              Name = $_.DnsName
              DBPath =  $DHCPServerDB.FileName
              BackupPath = $DHCPServerDB.BackupPath
              Logging = $DHCPServerDB.LoggingEnabled
              AuditState = $DHCPServerAuditLog.Enable
              AuditPath = $DHCPServerAuditLog.path
          }
      }
  }


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered

Hi @matteu31400 ,

first of all some questions:
Is the script working? -> If "yes" it's great ;-)
You can read the script and understand what the script is doing? -> If "yes" it's great :-)

I am able to read and understand the script and what it is doing -> It's great.

I don't see anything in your script that can be optimised (maybe beside adding more comments ;-) ) -> It's great this way

Maybe there is a different approach to get this done ... you never know ... but still -> It's great

Hope this helps.


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

matteu31400 avatar image
0 Votes"
matteu31400 answered

Hello @AndreasBaumgarten ,

Thank you very much for this message.
My script is working fine yes.

OK, for you it's ok like this...

What I mean is the Get-DHCPServersSettings function could be replaced by :

Get-DhcpServerDatabase | select FileName,BackupPath,LoggingEnabled
Get-DhcpServerAuditLog | select Enable,path

Isn't it "better" ?


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered

Hi,

There is not much difference between these two methods, however, the Get-DHCPServersSettings function returns a PSCustomObject and it could be easier to deal with it later if necessary.

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

matteu31400 avatar image
0 Votes"
matteu31400 answered

Thank you for your return.

Someone in french forum give me the advices to use pswriteHTML module. I find it really good, I'm going to use it in my HTML report :)
It's really easier to manage than html / css hard coded.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

matteu31400 avatar image
0 Votes"
matteu31400 answered matteu31400 edited

Thanks for your answer :) . I'm going to update my script to follow this good advice.

Have a nice day.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.