2021-09-27 10:38:12 +02:00
|
|
|
|
<#
|
2021-07-13 21:10:02 +02:00
|
|
|
|
.SYNOPSIS
|
2022-01-29 12:47:46 +01:00
|
|
|
|
Checks every symlink in a folder
|
2021-07-13 21:10:02 +02:00
|
|
|
|
.DESCRIPTION
|
2022-01-29 12:47:46 +01:00
|
|
|
|
This PowerShell script checks every symlink in a folder (including subfolders).
|
2021-10-04 21:29:23 +02:00
|
|
|
|
Returns the number of broken symlinks as exit value.
|
2021-10-16 16:50:10 +02:00
|
|
|
|
.PARAMETER folder
|
|
|
|
|
Specifies the path to the directory tree
|
2021-07-13 21:10:02 +02:00
|
|
|
|
.EXAMPLE
|
2021-10-01 14:56:16 +02:00
|
|
|
|
PS> ./check-symlinks .
|
|
|
|
|
✔️ 0 out of 10 symlinks are broken in 📂/home/markus
|
2021-07-13 21:10:02 +02:00
|
|
|
|
.LINK
|
|
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
|
.NOTES
|
2022-01-29 12:47:46 +01:00
|
|
|
|
Author: Markus Fleschutz / License: CC0
|
2021-02-10 16:49:09 +01:00
|
|
|
|
#>
|
|
|
|
|
|
2021-10-04 21:29:23 +02:00
|
|
|
|
param([string]$folder = "")
|
2021-02-10 16:49:09 +01:00
|
|
|
|
|
2021-02-18 20:17:55 +01:00
|
|
|
|
try {
|
2021-10-04 21:29:23 +02:00
|
|
|
|
if ($folder -eq "" ) { $folder = read-host "Enter the path to the folder" }
|
2021-09-12 21:33:46 +02:00
|
|
|
|
|
2022-03-28 13:26:23 +02:00
|
|
|
|
$StopWatch = [system.diagnostics.stopwatch]::startNew()
|
2021-10-04 21:29:23 +02:00
|
|
|
|
$FullPath = Resolve-Path "$folder"
|
2022-03-28 13:26:23 +02:00
|
|
|
|
"⏳ Checking every symlink in 📂$FullPath ..."
|
|
|
|
|
|
2021-09-12 21:33:46 +02:00
|
|
|
|
[int]$NumTotal = [int]$NumBroken = 0
|
2021-10-01 13:03:02 +02:00
|
|
|
|
Get-ChildItem $FullPath -recurse | Where { $_.Attributes -match "ReparsePoint" } | ForEach-Object {
|
2021-02-10 16:49:09 +01:00
|
|
|
|
$Symlink = $_.FullName
|
|
|
|
|
$Target = ($_ | Select-Object -ExpandProperty Target -ErrorAction Ignore)
|
|
|
|
|
if ($Target) {
|
|
|
|
|
$path = $_.FullName + "\..\" + ($_ | Select-Object -ExpandProperty Target)
|
|
|
|
|
$item = Get-Item $path -ErrorAction Ignore
|
|
|
|
|
if (!$item) {
|
2022-03-28 13:26:23 +02:00
|
|
|
|
"Bad $Symlink 🠆 $Target"
|
2021-09-12 21:33:46 +02:00
|
|
|
|
$NumBroken++
|
2021-02-10 16:49:09 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-09-12 21:33:46 +02:00
|
|
|
|
$NumTotal++
|
2021-02-10 16:49:09 +01:00
|
|
|
|
}
|
2021-08-24 20:46:03 +02:00
|
|
|
|
|
2022-03-28 13:26:23 +02:00
|
|
|
|
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
|
|
|
|
|
"✔️ checked $NumTotal symlinks in 📂$FullPath ($NumBroken are broken) in $Elapsed sec"
|
2021-09-12 21:37:31 +02:00
|
|
|
|
exit $NumBroken
|
2021-02-10 16:49:09 +01:00
|
|
|
|
} catch {
|
2022-04-13 12:06:32 +02:00
|
|
|
|
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
2021-02-10 16:49:09 +01:00
|
|
|
|
exit 1
|
|
|
|
|
}
|