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,381 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 97,076 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. matteu31 467 Reputation points
    2021-04-14T18:37:04.953+00:00

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

    Have a nice day.

    0 comments No comments