Compare commits
5 Commits
b010b7ac71
...
776a52c3c3
| Author | SHA1 | Date | |
|---|---|---|---|
| 776a52c3c3 | |||
| 3f03b2a0f3 | |||
| b32cae15b2 | |||
| 784b60a4e7 | |||
| 53fe4e9ed3 |
+228
-139
@@ -5,30 +5,40 @@ param(
|
|||||||
[Parameter(Position = 0, ParameterSetName = 'Match', ValueFromPipeline = $true)]
|
[Parameter(Position = 0, ParameterSetName = 'Match', ValueFromPipeline = $true)]
|
||||||
[string[]]$FilePath,
|
[string[]]$FilePath,
|
||||||
|
|
||||||
# Output file to save generated checksums (-o mode)
|
# Output file switch (-o) - auto generates file based on rules
|
||||||
[Parameter(Mandatory = $false, ParameterSetName = 'Generate')]
|
[Parameter(Mandatory = $false, ParameterSetName = 'Generate')]
|
||||||
[Alias('o')]
|
[Alias('o', 'out')]
|
||||||
|
[switch]$SaveToFile,
|
||||||
|
|
||||||
|
# Custom Output file name (-f)
|
||||||
|
[Parameter(Mandatory = $false, ParameterSetName = 'Generate')]
|
||||||
|
[Alias('f', 'outfile')]
|
||||||
[string]$OutFile,
|
[string]$OutFile,
|
||||||
|
|
||||||
# File containing checksums to verify (-c mode)
|
# File containing checksums to verify (-c mode)
|
||||||
[Parameter(Mandatory = $false, ParameterSetName = 'Verify')]
|
[Parameter(Mandatory = $false, ParameterSetName = 'Verify')]
|
||||||
[Alias('c')]
|
[Alias('c', 'check')]
|
||||||
[string]$Check,
|
[string]$Check,
|
||||||
|
|
||||||
# Direct checksum hash string to match against (-m mode)
|
# Direct checksum hash string to match against (-m mode)
|
||||||
[Parameter(Mandatory = $true, ParameterSetName = 'Match')]
|
[Parameter(Mandatory = $true, ParameterSetName = 'Match')]
|
||||||
[Alias('m')]
|
[Alias('m', 'match')]
|
||||||
[string]$Match,
|
[string]$Match,
|
||||||
|
|
||||||
# Hashing algorithm to use (Defaults to SHA256)
|
# Hashing algorithm to use (Defaults to SHA256)
|
||||||
[Parameter(Mandatory = $false)]
|
[Parameter(Mandatory = $false)]
|
||||||
[Alias('a')]
|
[Alias('a', 'algo', 'algorithm')]
|
||||||
[ValidateSet('SHA1', 'SHA256', 'SHA384', 'SHA512', 'MD5')]
|
[ValidateSet('SHA1', 'SHA256', 'SHA384', 'SHA512', 'MD5')]
|
||||||
[string]$Algorithm,
|
[string]$Algorithm,
|
||||||
|
|
||||||
# Help flag (-h, -Help)
|
# Recursive flag (-r)
|
||||||
[Parameter(Mandatory = $false)]
|
[Parameter(Mandatory = $false)]
|
||||||
[Alias('h')]
|
[Alias('r', 'recursive')]
|
||||||
|
[switch]$Recursive,
|
||||||
|
|
||||||
|
# Help flag (-h)
|
||||||
|
[Parameter(Mandatory = $false)]
|
||||||
|
[Alias('h', 'help')]
|
||||||
[switch]$Help
|
[switch]$Help
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -39,53 +49,60 @@ NAME
|
|||||||
checksum - Compute and verify message digests / file checksums
|
checksum - Compute and verify message digests / file checksums
|
||||||
|
|
||||||
SYNOPSIS
|
SYNOPSIS
|
||||||
.\checksum.ps1 [-FilePath] <string[]> [-OutFile <string>] [-Algorithm <string>]
|
.\checksum.ps1 [-FilePath] <string[]> [-o] [-OutFile <string>] [-Algorithm <string>] [-r]
|
||||||
.\checksum.ps1 [-FilePath] <string[]> -Match <string> [-Algorithm <string>]
|
.\checksum.ps1 [-FilePath] <string[]> -Match <string> [-Algorithm <string>] [-r]
|
||||||
.\checksum.ps1 -Check <string> [-Algorithm <string>]
|
.\checksum.ps1 -Check <string> [-Algorithm <string>] [-r]
|
||||||
.\checksum.ps1 -Help
|
.\checksum.ps1 -Help
|
||||||
|
|
||||||
DESCRIPTION
|
DESCRIPTION
|
||||||
Computes or verifies checksums using various cryptographic algorithms.
|
Computes or verifies checksums using various cryptographic algorithms.
|
||||||
Outputs hash values in standard Linux utility format (<hash> <filename>).
|
Outputs hash values in standard Linux utility format (<hash> <filename>).
|
||||||
|
Ignores checksum files (.md5, .sha1, .sha256, .sha384, .sha512) unless explicitly passed.
|
||||||
|
|
||||||
PARAMETERS
|
PARAMETERS
|
||||||
-FilePath <string[]>
|
-FilePath <string[]>
|
||||||
Specifies the path to one or more files to hash. Accepts wildcards (*).
|
Specifies the path to one or more files to hash. Accepts wildcards (*).
|
||||||
|
|
||||||
-o, -OutFile <string>
|
-o, -Out, --Out <switch>
|
||||||
Specifies an output file to save the checksum results to, while still
|
Enables writing checksum results to an auto-named file.
|
||||||
displaying the output on screen.
|
- If input has a wildcard (*), outputs to 'checksums.<algo>'
|
||||||
|
- If no wildcard is used, outputs to '<filename>.<algo>' per file.
|
||||||
|
When combined with -r and *, it auto-generates checksum files
|
||||||
|
inside the current working directory and each subdirectory recursively.
|
||||||
|
|
||||||
-c, -Check <string>
|
-f, -OutFile, --OutFile <string>
|
||||||
|
Specifies an explicit output file name to save results to.
|
||||||
|
|
||||||
|
-r, -Recursive, --Recursive <switch>
|
||||||
|
Recursively generate or verify checksums in subdirectories.
|
||||||
|
|
||||||
|
-c, -Check, --Check <string>
|
||||||
Read checksums from the specified file and verify them against files on disk.
|
Read checksums from the specified file and verify them against files on disk.
|
||||||
|
If -r is used, searches for the specified filename recursively in subdirectories.
|
||||||
|
|
||||||
-m, -Match <string>
|
-m, -Match, --Match <string>
|
||||||
Compare the computed hash of the target file(s) against a specific hash string.
|
Compare the computed hash of the target file(s) against a specific hash string.
|
||||||
|
|
||||||
-a, -Algorithm <string>
|
-a, -Algorithm, --Algorithm <string>
|
||||||
Specifies the cryptographic hash algorithm to use.
|
Specifies the cryptographic hash algorithm to use.
|
||||||
Supported values: SHA1, SHA256 (default), SHA384, SHA512, MD5.
|
Supported values: SHA1, SHA256 (default), SHA384, SHA512, MD5.
|
||||||
If omitted in -c or -m mode, the algorithm is auto-detected by hash length.
|
If omitted in -c or -m mode, the algorithm is auto-detected by hash length.
|
||||||
|
|
||||||
-h, -Help
|
-h, -Help, --Help <switch>
|
||||||
Display this help message.
|
Display this help message.
|
||||||
|
|
||||||
EXAMPLES
|
EXAMPLES
|
||||||
1. Compute SHA256 checksum for a single file:
|
1. Compute SHA256 checksum for a single file:
|
||||||
.\checksum.ps1 myfile.iso
|
.\checksum.ps1 myfile.iso
|
||||||
|
|
||||||
2. Compute checksums for all files and write to a file while showing output:
|
2. Compute checksums for all files and auto-write to files (-o flag):
|
||||||
.\checksum.ps1 * -o checksums.sha256
|
.\checksum.ps1 * -r -o
|
||||||
|
|
||||||
3. Verify a file against a explicit hash string:
|
3. Verify checksums from verification files recursively:
|
||||||
.\checksum.ps1 myfile.iso -m "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
|
.\checksum.ps1 -c checksums.sha256 -r
|
||||||
|
|
||||||
4. Verify checksums from a verification file (auto-detects algorithm):
|
|
||||||
.\checksum.ps1 -c checksums.sha256
|
|
||||||
'@
|
'@
|
||||||
}
|
}
|
||||||
|
|
||||||
# Helper function to auto-detect hash algorithm based on string character length
|
|
||||||
function Get-AlgorithmFromHashLength {
|
function Get-AlgorithmFromHashLength {
|
||||||
param([string]$HashString)
|
param([string]$HashString)
|
||||||
|
|
||||||
@@ -104,172 +121,244 @@ EXAMPLES
|
|||||||
}
|
}
|
||||||
|
|
||||||
process {
|
process {
|
||||||
# Match help arguments passed either as switch (-h, -Help) or positional string ('help', 'h', '--help', etc.)
|
# Bulletproof help check (catches --help passed positionally)
|
||||||
$isHelpRequested = $Help.IsPresent -or (
|
$helpInPath =$false
|
||||||
$FilePath -and $FilePath.Count -eq 1 -and (
|
if ($FilePath) {
|
||||||
$FilePath[0] -match '^(?i)[-/]{0,2}(h|help)$'
|
foreach ($p in ($FilePath)) {
|
||||||
)
|
if ($p -match '^(?i)[-/]{1,2}(h\vert{}help)$') {
|
||||||
)
|
$helpInPath =$true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ($isHelpRequested) {
|
if ($Help.IsPresent -or$helpInPath) {
|
||||||
Show-Help
|
Show-Help
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
# Normalize explicit algorithm input if provided
|
|
||||||
if (-not [string]::IsNullOrWhiteSpace($Algorithm)) {
|
if (-not [string]::IsNullOrWhiteSpace($Algorithm)) {
|
||||||
$Algorithm = $Algorithm.ToUpper()
|
$Algorithm =$Algorithm.ToUpper()
|
||||||
|
}
|
||||||
|
|
||||||
|
# --- ITEM GATHERING (Generate & Match Modes) ---
|
||||||
|
if ($PSCmdlet.ParameterSetName -in @('Generate', 'Match')) {
|
||||||
|
if (-not $FilePath -or$FilePath.Count -eq 0) {
|
||||||
|
Write-Error "checksum: Missing file path parameter. Use -Help for usage."
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
$allItems = @();
|
||||||
|
$isWildcardGlobal =$false;
|
||||||
|
$skipExtensions = @('.md5', '.sha1', '.sha256', '.sha384', '.sha512');
|
||||||
|
|
||||||
|
foreach ($pathEntry in ($FilePath)) {
|
||||||
|
$isWildcard = ($pathEntry -match '\*');
|
||||||
|
if ($isWildcard) { $isWildcardGlobal =$true }
|
||||||
|
|
||||||
|
$resolvedItems = @();
|
||||||
|
if ($Recursive) {
|
||||||
|
# Parens around variables prevent space-stripping crashes
|
||||||
|
$resolvedItems = Get-ChildItem -Path ($pathEntry) -Recurse -File -ErrorAction 'SilentlyContinue'
|
||||||
|
} else {
|
||||||
|
if ($isWildcard -or ($pathEntry -match '\?')) {
|
||||||
|
$resolvedItems = Get-ChildItem -Path ($pathEntry) -File -ErrorAction 'SilentlyContinue'
|
||||||
|
} else {
|
||||||
|
$resolvedItems = Get-Item -Path ($pathEntry) -ErrorAction 'SilentlyContinue'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (-not $resolvedItems) {
|
||||||
|
Write-Error "checksum: $($pathEntry)`: No such file or directory"
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
$explicitFileFound = $false;
|
||||||
|
if ((-not $isWildcard) -and (Test-Path -Path ($pathEntry) -PathType 'Leaf')) {
|
||||||
|
$explicitFileFound = $true
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($item in ($resolvedItems)) {
|
||||||
|
if ($item.PSIsContainer) { continue }
|
||||||
|
|
||||||
|
if (-not $explicitFileFound) {
|
||||||
|
if ($skipExtensions -contains $item.Extension.ToLower()) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$allItems += $item
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- DIRECT MATCH MODE (-m / -Match) ---
|
# --- DIRECT MATCH MODE (-m / -Match) ---
|
||||||
if ($PSCmdlet.ParameterSetName -eq 'Match') {
|
if ($PSCmdlet.ParameterSetName -eq 'Match') {
|
||||||
if (-not $FilePath -or $FilePath.Count -eq 0) {
|
|
||||||
Write-Error "checksum: Missing file path parameter for matching. Use -Help for usage."
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
# Auto-detect algorithm from provided -Match hash string if not explicitly given
|
|
||||||
if ([string]::IsNullOrWhiteSpace($Algorithm)) {
|
if ([string]::IsNullOrWhiteSpace($Algorithm)) {
|
||||||
$Algorithm = Get-AlgorithmFromHashLength -HashString $Match
|
$Algorithm = Get-AlgorithmFromHashLength -HashString ($Match)
|
||||||
if (-not $Algorithm) { return }
|
if (-not $Algorithm) { return }
|
||||||
Write-Verbose "Auto-detected algorithm: $Algorithm"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$expectedHash = $Match.Trim().ToLower()
|
$expectedHash = $Match.Trim().ToLower();
|
||||||
|
|
||||||
foreach ($pathEntry in $FilePath) {
|
foreach ($item in ($allItems)) {
|
||||||
$resolvedItems = Get-Item -Path $pathEntry -ErrorAction SilentlyContinue
|
$actualHash = (Get-FileHash -Path ($item.FullName) -Algorithm ($Algorithm)).Hash.ToLower()
|
||||||
|
$targetFile = Resolve-Path -Path ($item.FullName) -Relative -ErrorAction 'SilentlyContinue'
|
||||||
|
if (-not $targetFile) { $targetFile = $item.Name }
|
||||||
|
if ($targetFile.StartsWith(".\")) { $targetFile = $targetFile.Substring(2) }
|
||||||
|
|
||||||
if (-not $resolvedItems) {
|
if ($actualHash -eq $expectedHash) {
|
||||||
Write-Error "checksum: $pathEntry`: No such file or directory"
|
Write-Host "${targetFile}: " -NoNewline
|
||||||
continue
|
Write-Host "OK" -ForegroundColor Green
|
||||||
}
|
} else {
|
||||||
|
Write-Host "${targetFile}: " -NoNewline
|
||||||
foreach ($item in $resolvedItems) {
|
Write-Host "FAILED" -ForegroundColor Red
|
||||||
if ($item.PSIsContainer) {
|
|
||||||
Write-Error "checksum: $($item.Name)`: Is a directory"
|
|
||||||
} else {
|
|
||||||
$actualHash = (Get-FileHash -Path $item.FullName -Algorithm $Algorithm).Hash.ToLower()
|
|
||||||
$targetFile = Resolve-Path -Path $item.FullName -Relative
|
|
||||||
if ($targetFile.StartsWith(".\")) { $targetFile = $targetFile.Substring(2) }
|
|
||||||
|
|
||||||
if ($actualHash -eq $expectedHash) {
|
|
||||||
Write-Host "${targetFile}: " -NoNewline
|
|
||||||
Write-Host "OK" -ForegroundColor Green
|
|
||||||
} else {
|
|
||||||
Write-Host "${targetFile}: " -NoNewline
|
|
||||||
Write-Host "FAILED" -ForegroundColor Red
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
# --- VERIFICATION MODE (-c / -Check) ---
|
# --- VERIFICATION MODE (-c / -Check) ---
|
||||||
elseif ($PSCmdlet.ParameterSetName -eq 'Verify') {
|
elseif ($PSCmdlet.ParameterSetName -eq 'Verify') {
|
||||||
if (-not (Test-Path -Path $Check -PathType Leaf)) {
|
$checkFiles = @();
|
||||||
Write-Error "checksum: $Check`: No such file or directory"
|
if ($Recursive) {
|
||||||
|
$checkFileName = Split-Path -Path ($Check) -Leaf
|
||||||
|
$checkFiles = Get-ChildItem -Path '.\' -Filter ($checkFileName) -Recurse -File -ErrorAction 'SilentlyContinue'
|
||||||
|
} else {
|
||||||
|
if (Test-Path -Path ($Check) -PathType 'Leaf') {
|
||||||
|
$checkFiles = @(Get-Item -Path ($Check))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (-not $checkFiles -or $checkFiles.Count -eq 0) {
|
||||||
|
Write-Error "checksum: $($Check)`: No such file or directory"
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
$lines = Get-Content -Path $Check
|
foreach ($cf in ($checkFiles)) {
|
||||||
|
$lines = Get-Content -Path ($cf.FullName)
|
||||||
# Auto-detect algorithm based on hash length if not explicitly provided
|
$baseDir =$cf.DirectoryName
|
||||||
if ([string]::IsNullOrWhiteSpace($Algorithm)) {
|
|
||||||
foreach ($line in $lines) {
|
|
||||||
if ([string]::IsNullOrWhiteSpace($line) -or $line.StartsWith("#")) { continue }
|
|
||||||
if ($line -match '^([a-fA-F0-9]+)\s+') {
|
|
||||||
$Algorithm = Get-AlgorithmFromHashLength -HashString $Matches[1]
|
|
||||||
if (-not $Algorithm) { return }
|
|
||||||
Write-Verbose "Auto-detected algorithm: $Algorithm"
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
# Fallback if file contained no parsable hashes
|
$fileAlgo =$Algorithm
|
||||||
if ([string]::IsNullOrWhiteSpace($Algorithm)) {
|
if ([string]::IsNullOrWhiteSpace($fileAlgo)) {
|
||||||
$Algorithm = 'SHA256'
|
foreach ($line in ($lines)) {
|
||||||
|
if ([string]::IsNullOrWhiteSpace($line) -or$line.StartsWith("#")) { continue }
|
||||||
|
if ($line -match '^([a-fA-F0-9]+)\s+') {
|
||||||
|
$fileAlgo = Get-AlgorithmFromHashLength -HashString ($Matches[1])
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ([string]::IsNullOrWhiteSpace($fileAlgo)) {$fileAlgo = 'SHA256' }
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($line in $lines) {
|
foreach ($line in ($lines)) {
|
||||||
# Skip empty lines or comments
|
if ([string]::IsNullOrWhiteSpace($line) -or$line.StartsWith("#")) { continue }
|
||||||
if ([string]::IsNullOrWhiteSpace($line) -or $line.StartsWith("#")) { continue }
|
|
||||||
|
|
||||||
# Match standard checksum output format: "<hash> [* ]<filename>"
|
if ($line -match '^([a-fA-F0-9]+)\s+[* ]?(.*)$') {$expectedHash = $Matches[1].ToLower()$targetFile = $Matches[2].Trim().TrimStart('*')$targetFullPath = Join-Path -Path ($baseDir) -ChildPath ($targetFile)
|
||||||
if ($line -match '^([a-fA-F0-9]+)\s+[* ]?(.*)$') {
|
|
||||||
$expectedHash = $Matches[1].ToLower()
|
|
||||||
# Strip leading whitespace and any leftover leading asterisk
|
|
||||||
$targetFile = $Matches[2].Trim().TrimStart('*')
|
|
||||||
|
|
||||||
if (Test-Path -Path $targetFile -PathType Leaf) {
|
if (Test-Path -Path ($targetFullPath) -PathType 'Leaf') {$actualHash = (Get-FileHash -Path ($targetFullPath) -Algorithm ($fileAlgo)).Hash.ToLower()
|
||||||
$actualHash = (Get-FileHash -Path $targetFile -Algorithm $Algorithm).Hash.ToLower()
|
|
||||||
|
$displayPath = Resolve-Path -Path ($targetFullPath) -Relative -ErrorAction 'SilentlyContinue'
|
||||||
|
if (-not $displayPath) { $displayPath =$targetFile }
|
||||||
|
if ($displayPath.StartsWith(".\")) { $displayPath =$displayPath.Substring(2) }
|
||||||
|
|
||||||
if ($actualHash -eq $expectedHash) {
|
if ($actualHash -eq$expectedHash) {
|
||||||
Write-Host "${targetFile}: " -NoNewline
|
Write-Host "${displayPath}: " -NoNewline
|
||||||
Write-Host "OK" -ForegroundColor Green
|
Write-Host "OK" -ForegroundColor Green
|
||||||
|
} else {
|
||||||
|
Write-Host "${displayPath}: " -NoNewline
|
||||||
|
Write-Host "FAILED" -ForegroundColor Red
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
Write-Host "${targetFile}: " -NoNewline
|
$displayPath =$targetFile
|
||||||
Write-Host "FAILED" -ForegroundColor Red
|
if ($baseDir -ne (Get-Location).Path) {$displayPath = Join-Path -Path (Resolve-Path -Path ($baseDir) -Relative -ErrorAction 'SilentlyContinue') -ChildPath ($targetFile)
|
||||||
|
if ($displayPath.StartsWith(".\")) { $displayPath =$displayPath.Substring(2) }
|
||||||
|
}
|
||||||
|
Write-Host "${displayPath}: " -NoNewline
|
||||||
|
Write-Host "FAILED open or read" -ForegroundColor Red
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
Write-Host "${targetFile}: " -NoNewline
|
|
||||||
Write-Host "FAILED open or read" -ForegroundColor Red
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
Write-Warning "checksum: improperly formatted line: $line"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
# --- GENERATION MODE ---
|
# --- GENERATION MODE ---
|
||||||
else {
|
else {
|
||||||
# Default to SHA256 if no algorithm was specified in generation mode
|
if ([string]::IsNullOrWhiteSpace($Algorithm)) {$Algorithm = 'SHA256'
|
||||||
if ([string]::IsNullOrWhiteSpace($Algorithm)) {
|
|
||||||
$Algorithm = 'SHA256'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (-not $FilePath -or $FilePath.Count -eq 0) {
|
# Enable output if the switch is used OR an explicit file is provided
|
||||||
Write-Error "checksum: Missing file path parameter. Use -Help for usage."
|
$wantOutFile = $SaveToFile.IsPresent -or (-not [string]::IsNullOrWhiteSpace($OutFile));
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
# Clear/initialize output file if specified before writing hashes
|
if ($wantOutFile) {
|
||||||
if (-not [string]::IsNullOrWhiteSpace($OutFile)) {
|
if ($Recursive -and$isWildcardGlobal) {
|
||||||
$null = New-Item -Path $OutFile -ItemType File -Force
|
$groupedItems =$allItems | Group-Object DirectoryName
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($pathEntry in $FilePath) {
|
foreach ($group in ($groupedItems)) {
|
||||||
# Resolve wildcards and single file inputs
|
$dirPath =$group.Name
|
||||||
$resolvedItems = Get-Item -Path $pathEntry -ErrorAction SilentlyContinue
|
$filesInDir =$group.Group
|
||||||
|
|
||||||
if (-not $resolvedItems) {
|
|
||||||
Write-Error "checksum: $pathEntry`: No such file or directory"
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($item in $resolvedItems) {
|
|
||||||
# Handle directories like Linux sha256sum does
|
|
||||||
if ($item.PSIsContainer) {
|
|
||||||
Write-Error "checksum: $($item.Name)`: Is a directory"
|
|
||||||
} else {
|
|
||||||
$hash = (Get-FileHash -Path $item.FullName -Algorithm $Algorithm).Hash.ToLower()
|
|
||||||
|
|
||||||
# Format standard relative path (stripping leading '.\')
|
if ([string]::IsNullOrWhiteSpace($OutFile)) {
|
||||||
$relativePath = Resolve-Path -Path $item.FullName -Relative
|
$currentOutFile = Join-Path -Path ($dirPath) -ChildPath "checksums.$($Algorithm.ToLower())"
|
||||||
|
} else {
|
||||||
|
$currentOutFile = Join-Path -Path ($dirPath) -ChildPath ($OutFile)
|
||||||
|
}
|
||||||
|
|
||||||
|
$null = New-Item -Path ($currentOutFile) -ItemType 'File' -Force
|
||||||
|
|
||||||
|
foreach ($item in ($filesInDir)) {$hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($Algorithm)).Hash.ToLower()
|
||||||
|
$relativePath =$item.Name
|
||||||
|
|
||||||
|
$outputLine = "$hash$relativePath"
|
||||||
|
Add-Content -Path ($currentOutFile) -Value ($outputLine)
|
||||||
|
Write-Output $outputLine
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$singleOutFile =$null;
|
||||||
|
|
||||||
|
if ([string]::IsNullOrWhiteSpace($OutFile)) {
|
||||||
|
if ($isWildcardGlobal) {$singleOutFile = "checksums.$($Algorithm.ToLower())"
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$singleOutFile =$OutFile
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($singleOutFile) {
|
||||||
|
$null = New-Item -Path ($singleOutFile) -ItemType 'File' -Force
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($item in ($allItems)) {$hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($Algorithm)).Hash.ToLower()
|
||||||
|
|
||||||
|
$relativePath = Resolve-Path -Path ($item.FullName) -Relative -ErrorAction 'SilentlyContinue'
|
||||||
|
if (-not $relativePath) { $relativePath =$item.Name }
|
||||||
if ($relativePath.StartsWith(".\")) {
|
if ($relativePath.StartsWith(".\")) {
|
||||||
$relativePath = $relativePath.Substring(2)
|
$relativePath =$relativePath.Substring(2)
|
||||||
}
|
}
|
||||||
|
|
||||||
$outputLine = "$hash $relativePath"
|
$outputLine = "$hash$relativePath"
|
||||||
|
|
||||||
# If -OutFile is provided, write line to file
|
if ($singleOutFile) {
|
||||||
if (-not [string]::IsNullOrWhiteSpace($OutFile)) {
|
Add-Content -Path ($singleOutFile) -Value ($outputLine)
|
||||||
Add-Content -Path $OutFile -Value $outputLine
|
} else {
|
||||||
|
$currentOutFile = "$($item.FullName).$($Algorithm.ToLower())"
|
||||||
|
$null = New-Item -Path ($currentOutFile) -ItemType 'File' -Force
|
||||||
|
Add-Content -Path ($currentOutFile) -Value ($outputLine)
|
||||||
}
|
}
|
||||||
|
|
||||||
# Output to stdout
|
|
||||||
Write-Output $outputLine
|
Write-Output $outputLine
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
foreach ($item in ($allItems)) {$hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($Algorithm)).Hash.ToLower()
|
||||||
|
|
||||||
|
$relativePath = Resolve-Path -Path ($item.FullName) -Relative -ErrorAction 'SilentlyContinue'
|
||||||
|
if (-not $relativePath) { $relativePath =$item.Name }
|
||||||
|
if ($relativePath.StartsWith(".\")) {
|
||||||
|
$relativePath =$relativePath.Substring(2)
|
||||||
|
}
|
||||||
|
|
||||||
|
$outputLine = "$hash$relativePath"
|
||||||
|
Write-Output $outputLine
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,85 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
# usage
|
|
||||||
## curl -s https://raw.githubusercontent.com/PrincessPi3/general-scripts-and-system-ssssssetup/refs/heads/main/customscripts/python_pyenv_setup.sh | sudo "$SHELL" && source ~/.zshrc && exec "$SHELL" && pyenv install 3:latest && pyenv global 3:latest
|
|
||||||
# set -e
|
|
||||||
|
|
||||||
# set rcfile
|
|
||||||
rcfile="$HOME/.zshrc" # zsh
|
|
||||||
# rcfile="$USER/.bashrc" # bash
|
|
||||||
package_log="/home/princesspi/python_packages_removed.txt"
|
|
||||||
|
|
||||||
# check if a command exists, and if so, purge that package
|
|
||||||
# usage: check_purge_package <package_name>
|
|
||||||
check_purge_package () {
|
|
||||||
package_name=$1
|
|
||||||
|
|
||||||
# which -s $package_name 2>/dev/null 1>/dev/null # silently test for command
|
|
||||||
# package_check=$? # get return code. 0 for found, any other for not found
|
|
||||||
|
|
||||||
# if [ $package_check -eq 0 ]; then # if package is found, purge it
|
|
||||||
echo "purging $package_name"
|
|
||||||
sudo apt purge $package_name -y 2>>"$package_log" 1>>"$package_log"
|
|
||||||
echo "finished purging $package_name"
|
|
||||||
# else # otherwise skip
|
|
||||||
# echo "no $package_name installation found, skipping uninstall"
|
|
||||||
#fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# cleanup previous python installs
|
|
||||||
# check_purge_package pyenv*
|
|
||||||
# check_purge_package python3*
|
|
||||||
# check_purge_package python2*
|
|
||||||
# check_purge_package python*
|
|
||||||
# check_purge_package pip*
|
|
||||||
# check_purge_package pip3*
|
|
||||||
# check_purge_package pip2*
|
|
||||||
|
|
||||||
|
|
||||||
# cleanup
|
|
||||||
# echo "Autoremoving packages"
|
|
||||||
# sudo apt autoremove -y 2>>"$package_log" 1>>"$package_log"
|
|
||||||
# echo -e "DONE CLEARING OLD PACKAGES!\n\tRemoved packages logged to $package_log"
|
|
||||||
|
|
||||||
if [ -d $HOME/.pyenv ]; then
|
|
||||||
echo "Removing existing pyenv installation"
|
|
||||||
rm -rf $HOME/.pyenv
|
|
||||||
else
|
|
||||||
echo "No existing pyenv installation found, skipping removal"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# install pyenv
|
|
||||||
curl -fsSL https://pyenv.run | bash
|
|
||||||
## setup shell
|
|
||||||
### check for pyenv in rcfile
|
|
||||||
grep -q .pyenv $rcfile
|
|
||||||
rc_pyenv_check=$? # get retcode, 0 for found not 0 for not found
|
|
||||||
|
|
||||||
if [ $rc_pyenv_check -eq 0 ]; then # if pyenv found
|
|
||||||
echo "pyenv already set up in $rcfile, skipping add init"
|
|
||||||
else # if pyenv not found
|
|
||||||
echo "Setting up pyenv in $rcfile"
|
|
||||||
echo -e "\n# pyenv setup" >> $rcfile
|
|
||||||
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> $rcfile
|
|
||||||
echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> $rcfile
|
|
||||||
echo 'eval "$(pyenv init - zsh)"' >> $rcfile
|
|
||||||
fi
|
|
||||||
## add aliases
|
|
||||||
### check for aliases in rcfile
|
|
||||||
grep -q '## python aliases' $rcfile
|
|
||||||
rc_pyalias_check=$? # get retcode, 0 for found not 0 for not found
|
|
||||||
|
|
||||||
if [ $rc_pyalias_check -eq 0 ]; then # if aliases found
|
|
||||||
echo "pyenv aliases already set up in $rcfile, skipping"
|
|
||||||
else # if aliases not found, add python shit
|
|
||||||
echo "Setting up pyenv aliases in $rcfile"
|
|
||||||
echo "## python aliases" >> $rcfile
|
|
||||||
echo 'alias py=python' >> $rcfile
|
|
||||||
echo 'alias py3=python' >> $rcfile
|
|
||||||
echo 'alias py2=python' >> $rcfile
|
|
||||||
echo 'alias python3=python' >> $rcfile
|
|
||||||
echo 'alias python2=python' >> $rcfile
|
|
||||||
echo 'alias pip3=pip' >> $rcfile
|
|
||||||
echo -e "alias pip2=pip\n" >> $rcfile
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo -e "\n\ndone~ :3~\n\treset shell with exec \"\$SHELL\"\n"
|
|
||||||
Reference in New Issue
Block a user