-
Notifications
You must be signed in to change notification settings - Fork 0
/
psCheckLinks.ps1
51 lines (48 loc) · 1.5 KB
/
psCheckLinks.ps1
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
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]$FilePath,
[Parameter()]
[string]$OutputFile = "link_check_results.txt"
)
# Search for all hyperlinks in the markdown file
$md_file_content = Get-Content $FilePath
$links = [regex]::Matches($md_file_content, '\[(.*?)\]\((https?://|www\.)(.*?)\)') | ForEach-Object {
#$link = $_.Value
$link = $_.Groups[2].Value + $_.Groups[3].Value + $_.Groups[4].Value
if ($link.StartsWith("www.")) {
$link = "http://${link}"
}
$link
}
# Check each link for a valid response and save the result to the output file
$results = @()
$errorOccurred = $false
foreach ($link in $links) {
try {
$uri = New-Object System.Uri($link)
$response = Invoke-WebRequest $uri
if ($response.StatusCode -ne 200) {
$results += "Link $link is not accessible (status code $($response.StatusCode))"
$errorOccurred = $true
}
else {
$results += "Link ok: $link"
}
}
catch {
$results += "Error accessing link ${link}" #: $_ --> Error-Message removed, since it sometimes contains html-elements
#$results += "Status: $($response.StatusCode)"
$errorOccurred = $true
}
}
$results | Out-File $OutputFile
if ($errorOccurred) {
Write-Output "Error: One or more links are not accessible. See $OutputFile for details."
#Write-Output "Links: $links"
Exit 1
}
else {
Write-Output "All links were successfully checked."
Exit 0
}