Create HTML Report

Hello,
im new here and im completly newbie in powershell scripting, im trying to write my first powershell script.
The script is supposed to restore virtual machines from a copy, change their parameters, run them and test them in a simple way, and more precisely check whether the ports defined in the csv file are open and the services are running. And it works fine, but I would like the script to additionally create a nice html report based on the collected data. Below I present a part of the script that is responsible for testing virtual machines, an example input csv file and how less the output html file should look like.
Please guide me on how this can be done.

input csv file

ServerName;SourceID;vCPU;vRAM;console;ports;services
ServerX;1;2;4096;PS;1433 445;DHCP SENS
ServerY;2;2;4096;PS;1433 80 445;BITS DHCP SENS

script

    foreach ($line in $lines)
    {
            if($line.console -eq 'PS')
            {
                $cred = Get-StoredCredential -Target VM

                foreach($ports in $line.ports)
                {
                    $ports = $line.ports -split '\s+'
                    
                    foreach ($port in $ports)
                    {
                        Invoke-Command -VMName $line.ServerName -Credential $cred { Param($s,$p) Test-NetConnection -ComputerName $s -port $p } -ArgumentList ($line.ServerName,$port) | Select-Object -Property Remoteport,TcpTestSucceeded
                    }
                }

                foreach($service in $line.services )
                {
                    $services = $line.services -split '\s+'

                    foreach ($service in $services)
                    {
                        Invoke-Command -VMName $line.ServerName -Credential $cred { Param($Service) Get-Service -Name $Service } -ArgumentList ($Service) | Select-Object -Property Name,Status
                    }
                }

             

            }

            elseif($line.console -eq 'SSH')
            {
                
            }
    }

example html report
obraz

Patryk,
Welcome to the forum. :wave:t4:

What have you tried so far? We actually expect you to make an own attempt in the first place to get your task done or to solve your problem. If you have done so already please document here what exactly you have done and show your code. Then we probably might be able to help you step further.

What you ask for is a very common question and has been asked and answered thousand times before already. Please use your favorite internet search engine to look for examples …

https://www.google.com/search?q=Powershell+Create+HTML+Report

I modified my script to input both foreach loops result into variables.


   $OutputPorts = foreach ($line in $lines)
    {
            if($line.console -eq 'PS')
            {
                $cred = Get-StoredCredential -Target VM

                foreach($ports in $line.ports)
                {
                    $ports = $line.ports -split '\s+'
                    
                    foreach ($port in $ports)
                    {
                        Invoke-Command -VMName $line.ServerName -Credential $cred { Param($s,$p) Test-NetConnection -ComputerName $s -port $p } -ArgumentList ($line.ServerName,$port) | Select-Object -Property PSComputerName,Remoteport,TcpTestSucceeded
                    }
                }

        $OutputServices = foreach($service in $line.services )
                {
                    $services = $line.services -split '\s+'

                    foreach ($service in $services)
                    {
                        Invoke-Command -VMName $line.ServerName -Credential $cred { Param($Svc) Get-Service -Name $Svc } -ArgumentList ($Service) | Select-Object -Property PSComputerName,Name,Status
                    }
                }

             

            }

            elseif($line.console -eq 'SSH')
            {
                
            }
    }

$OutputPorts is working fine, but $OutputServices shows only services status from last checked server.

$OutputPorts
PSComputerName RemotePort TcpTestSucceeded
-------------- ------ ----------------
ServerX        1433           True
ServerX        80             True
ServerX        445            True
ServerY        1433           False
ServerY        80             True
ServerY        445            True

$OutputServices
PSComputerName Name     Status 
-------------- ----     ------ 
ServerY       BITS     Stopped
ServerY       DHCP     Running
ServerY       SENS     Running
ServerY       Netlogon Running

when i manage to solve this problem i am going to try to merge both tables into one by Join-Object module

Of course. :man_shrugging:t4: You overwrite the variable $OutputServices in each iteration of the loop $line in $lines. That’s why you only get the result from the last iteration. :wink:

Since both property sets are actually unrelated it does not make any sense to do this. I’d treat them in two separate / not nested loops and therefor in two separate tables.

How do you think would a combined table look like?