Update check-symlinks.ps1

This commit is contained in:
Markus Fleschutz 2022-05-30 10:22:51 +02:00
parent e71741d12a
commit 7fb2915036

View File

@ -1,28 +1,27 @@
<#
.SYNOPSIS
Checks symlinks in a directory tree
Checks symlinks in a folder
.DESCRIPTION
This PowerShell script checks every symlink in a folder (including subfolders).
This PowerShell script checks every symbolic link in a folder (including subfolders).
It returns the number of broken symlinks as exit value.
.PARAMETER folder
Specifies the path to the folder
.EXAMPLE
PS> ./check-symlinks .
found 2 broken symlinks at 📂/home/markus (10 total) in 17 sec
PS> ./check-symlinks C:\Users
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$folder = "")
param([string]$Folder = "")
try {
if ($folder -eq "" ) { $folder = read-host "Enter the path to the folder" }
if ($Folder -eq "" ) { $Folder = read-host "Enter the path to the folder" }
$StopWatch = [system.diagnostics.stopwatch]::startNew()
$FullPath = Resolve-Path "$folder"
"⏳ Checking symlinks under 📂$FullPath ..."
$FullPath = Resolve-Path "$Folder"
"⏳ Checking symlinks at 📂$FullPath including subfolders..."
[int]$NumTotal = [int]$NumBroken = 0
Get-ChildItem $FullPath -recurse | Where { $_.Attributes -match "ReparsePoint" } | ForEach-Object {
@ -32,8 +31,8 @@ try {
$path = $_.FullName + "\..\" + ($_ | Select-Object -ExpandProperty Target)
$item = Get-Item $path -ErrorAction Ignore
if (!$item) {
"$Symlink 🠆 $Target is broken"
$NumBroken++
"Broken symlink #$($NumBroken): $Symlink$Target"
}
}
$NumTotal++
@ -44,8 +43,10 @@ try {
"✔️ found no symlink at 📂$FullPath in $Elapsed sec"
} elseif ($NumBroken -eq 0) {
"✔️ found $NumTotal valid symlinks at 📂$FullPath in $Elapsed sec"
} elseif ($NumBroken -eq 1) {
"✔️ found $NumBroken broken symlink out of $NumTotal at 📂$FullPath in $Elapsed sec"
} else {
"✔️ found $NumBroken broken symlinks at 📂$FullPath ($NumTotal total) in $Elapsed sec"
"✔️ found $NumBroken broken symlinks out of $NumTotal at 📂$FullPath in $Elapsed sec"
}
exit $NumBroken
} catch {