From 43eb1283db51b5ca6fd8cc52023cdd18e6106531 Mon Sep 17 00:00:00 2001 From: Markus Fleschutz Date: Wed, 10 Feb 2021 16:49:09 +0100 Subject: [PATCH] Added check-symlinks.ps1 --- Data/scripts.csv | 1 + README.md | 1 + Scripts/check-symlinks.ps1 | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100755 Scripts/check-symlinks.ps1 diff --git a/Data/scripts.csv b/Data/scripts.csv index 1048486a..ee26f2b2 100644 --- a/Data/scripts.csv +++ b/Data/scripts.csv @@ -2,6 +2,7 @@ Filename,Description add-firewall-rules.ps1, adds firewall rules to the given executables (requires admin rights) check-ipv4-address.ps1, checks the given IPv4 address for validity check-mac-address.ps1, checks the given MAC address for validity +check-symlinks.ps1, checks every symlink in the given directory tree check-xml-file.ps1, checks the given XML file for validity clone-repos.ps1, clones well-known Git repositories close-calculator.ps1, closes the calculator program gracefully diff --git a/README.md b/README.md index e59569b9..936ba3a4 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ Table of Contents * [add-firewall-rules.ps1](Scripts/add-firewall-rules.ps1) - adds firewall rules for the given executables (requires admin rights) * [check-ipv4-address.ps1](Scripts/check-ipv4-address.ps1) - checks the given IPv4 address for validity * [check-mac-address.ps1](Scripts/check-mac-address.ps1) - checks the given MAC address for validity +* [check-symlinks.ps1](Scripts/check-symlinks.ps1) - checks every symlink in the given directory tree * [check-xml-file.ps1](Scripts/check-xml-file.ps1) - checks the given XML file for validity * [clone-repos.ps1](Scripts/clone-repos.ps1) - clones well-known Git repositories * [close-calculator.ps1](Scripts/close-calculator.ps1) - closes the calculator program gracefully diff --git a/Scripts/check-symlinks.ps1 b/Scripts/check-symlinks.ps1 new file mode 100755 index 00000000..4c26e279 --- /dev/null +++ b/Scripts/check-symlinks.ps1 @@ -0,0 +1,35 @@ +#!/bin/powershell +<# +.SYNTAX ./check-symlinks.ps1 [] +.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 +}