-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlink-check.ps1
More file actions
273 lines (221 loc) · 8.32 KB
/
Copy pathlink-check.ps1
File metadata and controls
273 lines (221 loc) · 8.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<#
.SYNOPSIS
Validate HTTP/HTTPS links in one or more input scripts.
.DESCRIPTION
Scans each specified file for absolute HTTP/HTTPS URLs and issues a web
request to verify the HTTP status code. Results are written to:
- Central script log via the shared myTech.Today logging module.
- URL-specific log at "%USERPROFILE%\myTech.Today\logs\link-check.md" with
monthly archiving to "link-check.YYYY-MM.md" (for the month the log was written).
.PARAMETER Path
One or more file paths or glob patterns for scripts to validate.
.PARAMETER TimeoutSeconds
Per-request timeout in seconds. Default is 15.
.EXAMPLE
# Validate all links in a single script
.\scripts\link-check.ps1 -Path .\bookmarks\bookmarks.ps1
.EXAMPLE
# Validate links in multiple scripts using wildcards
.\scripts\link-check.ps1 -Path .\bookmarks\*.ps1
#>
#Requires -Version 5.1
[CmdletBinding()]
param(
[Parameter(Mandatory = $true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
Position = 0)]
[Alias('FullName')]
[string[]]$Path,
[int]$TimeoutSeconds = 15
)
# PowerShell 7+ Version Check - myTech.Today standard
$script:PS7ContinueOnPS51 = $true # Allow running on PS 5.1 with warning
$script:PS7Silent = $false
$script:_RepoRoot = $PSScriptRoot
while ($script:_RepoRoot -and -not (Test-Path (Join-Path $script:_RepoRoot 'scripts\Require-PowerShell7.ps1'))) {
$script:_RepoRoot = Split-Path $script:_RepoRoot -Parent
}
if ($script:_RepoRoot -and (Test-Path (Join-Path $script:_RepoRoot 'scripts\Require-PowerShell7.ps1'))) {
. (Join-Path $script:_RepoRoot 'scripts\Require-PowerShell7.ps1')
}
$ErrorActionPreference = 'Stop'
# Import generic logging module (GitHub first, local fallback)
$loggingUrl = 'https://raw.githubusercontent.com/mytech-today-now/scripts/refs/heads/main/logging.ps1'
$script:LoggingModuleLoaded = $false
try {
Write-Host 'Loading generic logging module...' -ForegroundColor Cyan
Invoke-Expression (Invoke-WebRequest -Uri $loggingUrl -UseBasicParsing).Content
$script:LoggingModuleLoaded = $true
Write-Host '[OK] Generic logging module loaded successfully' -ForegroundColor Green
}
catch {
Write-Host "[ERROR] Failed to load generic logging module: $_" -ForegroundColor Red
Write-Host '[INFO] Falling back to local logging implementation' -ForegroundColor Yellow
$localLoggingPath = Join-Path $PSScriptRoot 'logging.ps1'
if (Test-Path $localLoggingPath) {
try {
. $localLoggingPath
$script:LoggingModuleLoaded = $true
Write-Host '[OK] Loaded logging module from local path' -ForegroundColor Green
}
catch {
Write-Host "[ERROR] Failed to load local logging module: $_" -ForegroundColor Red
}
}
}
if (-not $script:LoggingModuleLoaded) {
Write-Warning 'Logging module not available. Continuing without centralized logging.'
}
if ($script:LoggingModuleLoaded) {
$null = Initialize-Log -ScriptName 'Link-Check' -ScriptVersion '1.0.0'
Write-Log '=== Link-Check started ===' -Level INFO
}
function Initialize-LinkCheckLog {
[CmdletBinding()]
param()
$root = Join-Path $env:USERPROFILE 'myTech.Today'
$logDir = Join-Path $root 'logs'
if (-not (Test-Path $logDir)) {
New-Item -Path $logDir -ItemType Directory -Force | Out-Null
}
$currentLogPath = Join-Path $logDir 'link-check.md'
$now = Get-Date
$currentMonthStamp = $now.ToString('yyyy-MM')
if (Test-Path $currentLogPath) {
$fileInfo = Get-Item $currentLogPath
$fileMonthStamp = $fileInfo.LastWriteTime.ToString('yyyy-MM')
if ($fileMonthStamp -ne $currentMonthStamp) {
$archiveName = "link-check.$fileMonthStamp.md"
$archivePath = Join-Path $logDir $archiveName
if (Test-Path $archivePath) {
$timestamp = $fileInfo.LastWriteTime.ToString('yyyyMMdd_HHmmss')
$archiveName = "link-check.$fileMonthStamp.$timestamp.md"
$archivePath = Join-Path $logDir $archiveName
}
Move-Item -Path $currentLogPath -Destination $archivePath -Force -ErrorAction SilentlyContinue
if ($script:LoggingModuleLoaded) {
Write-Log "Archived previous link-check log to '$archivePath'." -Level INFO
}
}
}
if (-not (Test-Path $currentLogPath)) {
$header = @"
# link-check URL Validation Log
**Log Created:** $($now.ToString('yyyy-MM-dd HH:mm:ss'))
**Computer:** $env:COMPUTERNAME
**User:** $env:USERNAME
---
| Timestamp | File | Line | Status | URL |
|-----------|------|------|--------|-----|
"@
Set-Content -Path $currentLogPath -Value $header -Force
}
return $currentLogPath
}
function Test-UrlStatus {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$Url,
[int]$TimeoutSeconds = 15
)
$statusCode = $null
$statusDescription = $null
$errorMessage = $null
try {
$response = Invoke-WebRequest -Uri $Url -Method Head -UseBasicParsing -TimeoutSec $TimeoutSeconds -MaximumRedirection 5
$statusCode = [int]$response.StatusCode
$statusDescription = $response.StatusDescription
}
catch {
$resp = $_.Exception.Response
if ($null -ne $resp) {
try {
$statusCode = [int]$resp.StatusCode
$statusDescription = $resp.StatusDescription
}
catch {
$errorMessage = $_.Exception.Message
}
}
else {
$errorMessage = $_.Exception.Message
}
}
[PSCustomObject]@{
Url = $Url
StatusCode = $statusCode
StatusDescription = $statusDescription
ErrorMessage = $errorMessage
}
}
# Main processing
$linkCheckLogPath = Initialize-LinkCheckLog
if ($script:LoggingModuleLoaded) {
Write-Log "Using URL log path '$linkCheckLogPath'." -Level INFO
}
# Compile list of files
$allFiles = @()
foreach ($inputPath in $Path) {
if ([string]::IsNullOrWhiteSpace($inputPath)) { continue }
try {
$resolved = Get-ChildItem -Path $inputPath -File -ErrorAction Stop
if (-not $resolved) {
throw "No files matched path '$inputPath'."
}
$allFiles += $resolved
}
catch {
if ($script:LoggingModuleLoaded) {
Write-Log "Failed to resolve path '$inputPath': $($_.Exception.Message)" -Level WARNING
}
}
}
if (-not $allFiles -or $allFiles.Count -eq 0) {
if ($script:LoggingModuleLoaded) {
Write-Log 'No input files to process. Exiting.' -Level WARNING
}
return
}
$urlRegex = [regex]'https?://[^\s''"]+'
foreach ($file in $allFiles | Sort-Object FullName -Unique) {
if ($script:LoggingModuleLoaded) {
Write-Log "Scanning file '$($file.FullName)' for URLs..." -Level INFO
}
try {
$lines = Get-Content -Path $file.FullName -ErrorAction Stop
}
catch {
if ($script:LoggingModuleLoaded) {
Write-Log "Failed to read file '$($file.FullName)': $($_.Exception.Message)" -Level ERROR
}
continue
}
for ($i = 0; $i -lt $lines.Count; $i++) {
$lineNumber = $i + 1
$line = $lines[$i]
$urlMatches = $urlRegex.Matches($line)
if (-not $urlMatches -or $urlMatches.Count -eq 0) { continue }
foreach ($match in $urlMatches) {
$url = $match.Value
$result = Test-UrlStatus -Url $url -TimeoutSeconds $TimeoutSeconds
$statusText = if ($result.StatusCode) {
"$($result.StatusCode) $($result.StatusDescription)"
}
else {
"ERROR: $($result.ErrorMessage)"
}
$logLevel = if ($result.StatusCode -eq 200) { 'SUCCESS' } else { 'ERROR' }
if ($script:LoggingModuleLoaded) {
Write-Log "[$($file.FullName):$lineNumber] $statusText - $url" -Level $logLevel
}
$rowTimestamp = Get-Date -Format 'yyyy-MM-dd HH:mm:ss'
$tableRow = "| $rowTimestamp | $($file.FullName) | $lineNumber | $statusText | $url |"
Add-Content -Path $linkCheckLogPath -Value $tableRow -ErrorAction SilentlyContinue
}
}
}
if ($script:LoggingModuleLoaded) {
Write-Log '=== Link-Check completed ===' -Level SUCCESS
}