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
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
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’.