Script to collect system health report in Windows 8 (PowerShell)

Introduction

This script uses PowerShell to create a System Health Report. Then, a user can save the report as an .html file to a specified path.

Scenarios

The daily health report gives IT Professionals the ability to run system checks every day. They have to use a script in order to generate a report automatically.

Script

Step 1: Click Start, type powershell in the search box on the Start Menu, right-click the Windows PowerShell icon, and then click Run Windows PowerShell as administrator. If the User Account Control dialog box appears, confirm that the action it displays is what you want, and then click Continue.

Step 2: To run the script in the Windows PowerShell Console, type the following command at the prompt: Import-Module <Script Path>.

For example, type Import-Module C:\Script\ConvertPowerPointToWordDocument.psm1

This is shown in the following figure.

Step 3: Type the Get-OSCSystemHealthReport –SaveToPath C:\HealthReport command in the Windows PowerShell Console.
This command shows generate a system health report to C:\HealthReport.

When the script finishes running, the result is as follows:

Here are some code snippets for your references. To get the complete script sample, please click the download button at the beginning of this page. 

PowerShell
Edit|Remove
If(Test-Path-Path $SaveToPath) 
{ 
    Write-Host "Generating system health report..." 
    Start-Process-FilePath "$env:SystemRoot\System32\perfmon"-ArgumentList "/report"-WindowStyle Hidden 
     
    #waiting for a report to be generatedStart-Sleep-Seconds 100 
    $DiagnosticsFullName = Get-ChildItem-Path "$env:SystemDrive\PerfLogs\System\Diagnostics"|` 
    Sort-Object LastWriteTime -Descending |Select-Object-First 1 |ForEach-Object{$_.FullName} 
     
    $ReportFile = "$DiagnosticsFullName\report.html"#check whether the report is generated, if the report exists, then close the process.If(Test-Path-Path $ReportFile) 
    { 
        Stop-Process-Name "perfmon" 
    } 
     
    #initial a variable$ComputerName = $Env:COMPUTERNAME 
    $key = $(Get-Date-format "MMddhhmm") 
    $FileName = $ComputerName+"_"+$key#move the report to specified path 
    Copy-Item -Path "$DiagnosticsFullName\report.html"-Destination $SaveToPath-Force 
    Rename-Item-Path "$SaveToPath\report.html"-NewName "$FileName.html" 
    Write-Host "Generate system health report successfully." 
} 
Else 
{ 
    Write-Warning"Cannot find path '$SaveToPath'. If you want to save the report file to $SaveToPath, please input a correct path." 
}

Prerequisite

Windows PowerShell 2.0

Windows 8