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
|
|
|
|
|
2021-10-04 21:29:23 +02:00
|
|
|
|
$FullPath = Resolve-Path "$folder"
|
2021-10-01 13:03:02 +02:00
|
|
|
|
write-progress "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) {
|
2021-10-01 14:56:16 +02:00
|
|
|
|
write-warning "Broken symlink $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
|
|
|
|
|
2021-10-01 13:03:02 +02:00
|
|
|
|
"✔️ $NumBroken out of $NumTotal symlinks are broken in 📂$FullPath"
|
2021-09-12 21:37:31 +02:00
|
|
|
|
exit $NumBroken
|
2021-02-10 16:49:09 +01:00
|
|
|
|
} catch {
|
2021-09-16 20:19:10 +02:00
|
|
|
|
"⚠️ Error: $($Error[0]) ($($MyInvocation.MyCommand.Name):$($_.InvocationInfo.ScriptLineNumber))"
|
2021-02-10 16:49:09 +01:00
|
|
|
|
exit 1
|
|
|
|
|
}
|