skip to Main Content

I am working on a health check PowerShell script that is generating an HTML file. Now, I want to update the solution where If I run the same script again (as a post check) it should create maybe another html file with the new health check values and old file values should also be there. This is to compare what changed during the day in the system. Please let me know how to append the new data in the old/existing html file.

I have written below code to generate health check report.

$ServHead = ConvertTo-HTML -AS Table -Fragment -PreContent '<H2>Services Information</H2>'|Out-String

# Gathers information on Services.  Displays the service name, System name of the Service, Start Mode, and State.  Sorted by Start Mode and then State.
$Service = Get-WmiObject win32_service -ComputerName $computer | Select-Object DisplayName, Name, StartMode, State | sort StartMode, State, DisplayName | ConvertTo-HTML -Fragment 

# create an html output file
ConvertTo-HTML -Head $Style -PostContent "$ReportHead $Service " -Title "System Health Check Report"  |  Out-File "C:HealthCheck$computerHealth Report $CurrentDate.html"

so, the requirement is if we run the script again it should append the latest values so that we can compare what was the earlier state of the service and what is now.
Please let me know how to append the new data in the old/existing html file ?

I am looking for an additional column to compare the previous and current output in the same file – e.g. – Service | Status | New Status so, previous execution will fill ‘Service’ and ‘Status’ details and in next execution (maybe after some hours) the services latest status should come under ‘New Status’

2

Answers


  1. you can simply use -Append parameter in the end with out-file command. So, it should look like as below in your code

    Out-File "C:HealthCheck$computerHealth Report $CurrentDate.html" -Append

    Login or Signup to reply.
  2. As commented, I would suggest saving the results from the first run into an easy to use CSV file. Then on the second run load the data back into a variable and update the objects to contain a new column ‘NewStatus’.

    # set a variable for the path of the temporary csv file
    $csvFile  = 'C:TempHealthCheck_{0}.csv' -f $computer
    $services = Get-CimInstance win32_service -ComputerName $computer | 
                Select-Object DisplayName, Name, StartMode, State | 
                Sort-Object StartMode, State, DisplayName
    
    # or use the Get-Service cmdlet. This has different property names for
    # StartMode and State, so below I use calculated properties to alter these
    # $services = Get-Service -ComputerName $computer | 
    #             Select-Object DisplayName, Name, @{Name = 'StartMode'; Expression = $_.StartType}, 
    #                                              @{Name = 'State'; Expression = $_.Status | 
    #             Sort-Object StartMode, State, DisplayName
    
    
    # next check if the code has run before by checking the output csv file
    if (Test-Path -Path $csvFile) {
        # we have stored a first result, so read that in and add the NewStatus column
        $fullreport = Import-Csv -Path $csvFile | Select-Object *, NewStatus
        # next, fill in the NewStatus column with the values from $services
        foreach ($item in $services) {
           $svc = $fullreport | Where-Object { $_.Name -eq $item.Name }
           if ($svc) { $svc.NewStatus = $item.State }
        }
        # delete the temporary csv file
        Remove-Item -Path $csvFile -Force
    
        # now convert the $fullreport to HTML using ConvertTo-HTML
        # I'll leave that out because your code snippet didn't specify the
        # variables $Style, $ReportHead and $CurrentDate ..
    }
    else {
        # this is a first run, so save $services as csv file
        $services | Export-Csv -Path $csvFile -NoTypeInformation
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search