mirror of
https://github.com/fleschutz/PowerShell.git
synced 2025-08-12 14:07:20 +02:00
Added check-symlinks.ps1
This commit is contained in:
35
Scripts/check-symlinks.ps1
Executable file
35
Scripts/check-symlinks.ps1
Executable file
@ -0,0 +1,35 @@
|
||||
#!/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 = "")
|
||||
|
||||
try {
|
||||
if ($DirTree -eq "" ) {
|
||||
$DirTree = read-host "Enter the path to the directory tree"
|
||||
}
|
||||
|
||||
[int]$SymlinksTotal = [int]$SymlinksBroken = 0
|
||||
gci $DirTree -Recurse | Where { $_.Attributes -match "ReparsePoint" } | ForEach-Object {
|
||||
$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) {
|
||||
write-output "Symlink $Symlink -> $Target is broken"
|
||||
$SymlinksBroken++
|
||||
}
|
||||
}
|
||||
$SymlinksTotal++
|
||||
}
|
||||
echo "Done - found $SymlinksTotal symlinks total, $SymlinksBroken are broken"
|
||||
exit $SymlinksBroken
|
||||
} catch {
|
||||
write-error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
||||
exit 1
|
||||
}
|
Reference in New Issue
Block a user