PowerShell/Scripts/check-symlinks.ps1

41 lines
1.1 KiB
PowerShell
Raw Normal View History

2021-08-26 10:46:12 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
2021-09-12 21:33:46 +02:00
check-symlinks.ps1 [<DirTree>]
2021-07-13 21:10:02 +02:00
.DESCRIPTION
2021-09-12 21:33:46 +02:00
Checks every symlink in a directory tree
2021-07-13 21:10:02 +02:00
.EXAMPLE
PS> .\check-symlinks.ps1 C:\MyApp
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
2021-08-29 17:50:03 +02:00
Author: Markus Fleschutz · License: CC0
2021-02-10 16:49:09 +01:00
#>
2021-07-15 15:51:22 +02:00
param([string]$DirTree = "")
2021-02-10 16:49:09 +01:00
2021-02-18 20:17:55 +01:00
try {
2021-09-12 21:33:46 +02:00
if ($DirTree -eq "" ) { $DirTree = read-host "Enter the path to the directory tree" }
2021-02-10 18:37:55 +01:00
write-progress "Checking symlinks in $DirTree..."
2021-09-12 21:33:46 +02:00
[int]$NumTotal = [int]$NumBroken = 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-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-09-12 21:33:46 +02:00
"✔️ $NumBroken out of $NumTotal symlinks are broken in 📂$DirTree"
2021-09-12 21:37:31 +02:00
exit $NumBroken
2021-02-10 16:49:09 +01:00
} catch {
2021-05-02 21:30:48 +02:00
write-error "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2021-02-10 16:49:09 +01:00
exit 1
}