2021-02-10 16:49:09 +01:00
|
|
|
#!/bin/powershell
|
|
|
|
<#
|
|
|
|
.SYNTAX ./check-symlinks.ps1 [<dir-tree>]
|
|
|
|
.DESCRIPTION checks every symlink in the given directory tree
|
|
|
|
.LINK https://github.com/fleschutz/PowerShell
|
|
|
|
.NOTES Author: Markus Fleschutz / License: CC0
|
|
|
|
#>
|
|
|
|
|
|
|
|
param($DirTree = "")
|
|
|
|
|
2021-02-18 20:17:55 +01:00
|
|
|
if ($DirTree -eq "" ) {
|
|
|
|
$DirTree = read-host "Enter the path to the directory tree"
|
|
|
|
}
|
2021-02-10 16:49:09 +01:00
|
|
|
|
2021-02-18 20:17:55 +01:00
|
|
|
try {
|
2021-02-10 18:37:55 +01:00
|
|
|
write-progress "Checking symlinks in $DirTree..."
|
2021-02-10 16:49:09 +01:00
|
|
|
[int]$SymlinksTotal = [int]$SymlinksBroken = 0
|
2021-02-10 18:37:55 +01:00
|
|
|
Get-ChildItem $DirTree -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-02-10 16:57:03 +01:00
|
|
|
write-warning "Broken symlink: $Symlink -> $Target"
|
2021-02-10 16:49:09 +01:00
|
|
|
$SymlinksBroken++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$SymlinksTotal++
|
|
|
|
}
|
2021-03-05 09:09:39 +01:00
|
|
|
write-host -foregroundColor green "OK - found $SymlinksTotal symlinks total, $SymlinksBroken symlinks are broken"
|
2021-02-10 16:49:09 +01:00
|
|
|
exit $SymlinksBroken
|
|
|
|
} catch {
|
2021-02-16 10:03:20 +01:00
|
|
|
write-error "ERROR: line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
2021-02-10 16:49:09 +01:00
|
|
|
exit 1
|
|
|
|
}
|