0

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\$computer\Health 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'

4
  • 1
    You forgot to ask a question :) Commented Sep 2, 2024 at 10:12
  • apologies, If I couldn't, Please let me know how to append the new data in the old/existing html file in the continuation of the attached code. Commented Sep 2, 2024 at 10:34
  • Store the results from the first query in an easier type of file to process like a CSV file. Then on the next run, read that file back into an array of objects with Import-Csv and add the new column for 'New Status'. Once done, convert that to HTML. By the way, which version of PowerShell are you actually running?
    – Theo
    Commented Sep 3, 2024 at 12:49
  • Thank you Theo. the PS version is - 5.1.22621.3958 . Commented Sep 4, 2024 at 4:24

2 Answers 2

0

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:\Temp\HealthCheck_{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
}
0

You can simply use the -Append parameter in the end with the Out-File command. So, it should look like as below in your code:

Out-File "C:\HealthCheck\$computer\Health Report $CurrentDate.html" -Append
1
  • Thanks for your input, 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 exection will fill 'Service' and 'Status' details and in next execution (maybe after some hours) the services latest status should come under 'New Status' Commented Sep 2, 2024 at 11:59

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.