PowerShell/Scripts/check-symlinks.ps1

44 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
check-symlinks.ps1 [<dir-tree>]
.DESCRIPTION
Checks every symlink in the given directory tree
.EXAMPLE
PS> .\check-symlinks.ps1 C:\MyApp
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
2021-08-03 15:53:57 +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
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-08-24 20:46:03 +02:00
"✔️ found $SymlinksTotal symlinks total, $SymlinksBroken symlinks are broken"
2021-02-10 16:49:09 +01:00
exit $SymlinksBroken
} 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
}