Powershell and HTML report

matteu31 467 Reputation points
2021-04-13T19:11:27.153+00:00

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
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,361 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 96,266 Reputation points MVP
    2021-04-14T12:43:50.587+00:00

    @matteu31 ,

    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

    1 person found this answer helpful.
    0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Andreas Baumgarten 96,266 Reputation points MVP
    2021-04-13T20:00:48.59+00:00

    Hi @matteu31 ,

    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

    0 comments No comments

  2. matteu31 467 Reputation points
    2021-04-13T21:37:28.517+00:00

    Hello @Andreas Baumgarten ,

    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" ?

    0 comments No comments

  3. Ian Xue (Shanghai Wicresoft Co., Ltd.) 29,571 Reputation points Microsoft Vendor
    2021-04-14T08:26:13.44+00:00

    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.

    0 comments No comments

  4. matteu31 467 Reputation points
    2021-04-14T08:30:46.297+00:00

    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.

    0 comments No comments