如何将定时检测的IIS相关服务状态生成自动刷新网页?
Solution
Step 1: Create the PowerShell Script to Generate HTML Status Page
First, create a script file (e.g., C:\Get-IISServiceStatus.ps1) that retrieves IIS service statuses, formats them into a styled HTML page with auto-refresh, and saves it to your target path.
# Retrieve IIS-related service statuses $targetServices = Get-Service AppHostSVC,FTPSVC,IISAdmin,MSFTPSVC,W3SVC,WAS,WMSVC # Build HTML content with auto-refresh and custom styling $htmlOutput = $targetServices | ConvertTo-Html -Title "IIS Service Monitor" -Body "<h1 style='text-align:center;'>IIS Related Service Status</h1>" | ForEach-Object { # Insert auto-refresh meta tag (300 seconds = 5 minutes) if ($_ -eq "<head>") { "<head><meta http-equiv='refresh' content='300'>" } # Add table styling elseif ($_ -match "<table>") { $_ -replace "<table>", "<table style='border-collapse: collapse; width: 80%; margin: 20px auto;'>" } # Style table headers elseif ($_ -match "<th>") { $_ -replace "<th>", "<th style='border: 1px solid #ddd; padding: 10px; background-color: #f5f5f5; text-align:left;'>" } # Highlight running services in green elseif ($_ -contains "<td>Running</td>") { $_ -replace "<td>Running</td>", "<td style='border:1px solid #ddd; padding:10px; color:#2ecc71; font-weight:bold;'>Running</td>" } # Highlight stopped services in red elseif ($_ -contains "<td>Stopped</td>") { $_ -replace "<td>Stopped</td>", "<td style='border:1px solid #ddd; padding:10px; color:#e74c3c; font-weight:bold;'>Stopped</td>" } # Style regular table cells else { $_ -replace "<td>", "<td style='border:1px solid #ddd; padding:10px;'>" } } # Save the HTML output to the target file $htmlOutput | Out-File -Path "C:\IISServiceChecker.html" -Encoding UTF8
Step 2: Update the Scheduled Task to Run the Script
Replace your original scheduled task setup with this script, which configures the task to run the HTML-generating script every 5 minutes (avoids messy command-line arguments and ensures reliable execution).
# Define the path to your script $scriptPath = "C:\Get-IISServiceStatus.ps1" # Create the task action (run PowerShell with the script) $taskAction = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-ExecutionPolicy Bypass -File `"$scriptPath`"" # Create a trigger that runs immediately and repeats every 5 minutes indefinitely $taskTrigger = New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionInterval (New-TimeSpan -Minutes 5) -RepetitionDuration ([TimeSpan]::MaxValue) # Configure task settings: hide window, run regardless of user login, handle battery scenarios $taskSettings = New-ScheduledTaskSettingsSet -Hidden ` -AllowStartIfOnBatteries ` -DontStopIfGoingOnBatteries ` -StartWhenAvailable ` -RunOnlyIfNetworkAvailable $false # Register the scheduled task Register-ScheduledTask -TaskName "Monitor IIS Service Status" ` -Action $taskAction ` -Trigger $taskTrigger ` -Settings $taskSettings ` -Description "Automatically updates IIS service status to C:\IISServiceChecker.html every 5 minutes"
Step 3: Verify the Setup
- Manually run
C:\Get-IISServiceStatus.ps1to confirm it createsC:\IISServiceChecker.htmlwith correct statuses and styling. - Open the HTML file in a browser to check that it displays properly and refreshes every 5 minutes.
- Check Task Scheduler to ensure the task is registered and runs at the specified interval.
Key Notes
- Ensure the task's user account has permissions to write to
C:\and access service statuses. - If you hit execution policy errors, run
Set-ExecutionPolicy RemoteSigned(as admin) to allow local script execution.
内容的提问来源于stack exchange,提问作者sakworld
相关产品推荐
相关产品推荐

