-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrionUtils.psm1
More file actions
136 lines (117 loc) · 9.04 KB
/
OrionUtils.psm1
File metadata and controls
136 lines (117 loc) · 9.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<#
================================================================================
ORION UTILS - POWERSHELL UTILITY FUNCTIONS
================================================================================
Author: Sune Alexandersen Narud
Date: February 9, 2026
Version: 1.6.0
HIGH LEVEL DESIGN (HLD):
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
PURPOSE:
OrionUtils is a collection of PowerShell utility functions for file management,
time/date operations, logging, and common administrative tasks. It provides
reliable, tested functions for everyday automation needs.
ARCHITECTURE:
┌─────────────────────────────────────────────────────────────────────────────┐
│ FILE MANAGEMENT │
├─────────────────────────────────────────────────────────────────────────────┤
│ Remove-StaleFile Keep N newest files, delete the rest │
│ Test-FileLock Check if a file is locked by another process │
├─────────────────────────────────────────────────────────────────────────────┤
│ TIME & DATE │
├─────────────────────────────────────────────────────────────────────────────┤
│ Format-TimeSpan Format TimeSpan to human-readable string │
│ Get-TimeElapsed Calculate time since a given date │
├─────────────────────────────────────────────────────────────────────────────┤
│ STOPWATCH / PERFORMANCE TIMING │
├─────────────────────────────────────────────────────────────────────────────┤
│ Start-Stopwatch Start a named stopwatch for timing │
│ Stop-Stopwatch Stop stopwatch and record elapsed time │
│ Get-Stopwatch Get running or completed stopwatch data │
│ Reset-Stopwatch Clear stopwatch data │
│ Show-StopwatchSummary Display formatted performance summary │
├─────────────────────────────────────────────────────────────────────────────┤
│ USER INTERACTION │
├─────────────────────────────────────────────────────────────────────────────┤
│ Wait-ForInput Wait for key press, or timed wait with -Timer │
├─────────────────────────────────────────────────────────────────────────────┤
│ LOGGING & METRICS │
├─────────────────────────────────────────────────────────────────────────────┤
│ Write-Log Structured logging to file │
│ Export-MetricsToCSV Export PSObject metrics to CSV with timestamps │
├─────────────────────────────────────────────────────────────────────────────┤
│ SYSTEM UTILITIES │
├─────────────────────────────────────────────────────────────────────────────┤
│ Get-WindowsVersion Convert OS version to friendly name │
├─────────────────────────────────────────────────────────────────────────────┤
│ DEMO │
├─────────────────────────────────────────────────────────────────────────────┤
│ Show-OrionUtilsDemo Interactive demo of all module functions │
├─────────────────────────────────────────────────────────────────────────────┤
│ PRIVATE FUNCTIONS │
├─────────────────────────────────────────────────────────────────────────────┤
│ Remove-FileWithLockCheck Internal helper for safe file deletion │
└─────────────────────────────────────────────────────────────────────────────┘
CORE FEATURES:
• 📁 File management utilities with lock detection
• ⏱️ Time/date formatting and elapsed time calculations
• 🔒 Safe file operations with error handling
• 📝 Structured logging with multiple levels
• 📊 Metrics export to CSV with dynamic headers
• ⚡ WhatIf/Confirm support for safe testing
• 🎨 Optional integration with OrionDesign for beautiful output
• 📚 Complete Comment-Based Help documentation
DEPENDENCIES:
• PowerShell 5.1 or later
• OrionDesign module (optional, for enhanced output formatting)
USAGE PATTERNS:
Import-Module OrionUtils
# File Management
Remove-StaleFile -Path "C:\Logs" -Filter "*.log" -Keep 10
Test-FileLock -FilePath "C:\Data\file.xlsx" -Silent
# Time Operations
Format-TimeSpan -TimeSpan $stopwatch.Elapsed
Get-TimeElapsed -DateTime $lastLogin -OutputFormat Days
# Stopwatch / Performance Timing
Start-Stopwatch -Name "Data Import"
# ... your code ...
Stop-Stopwatch -Name "Data Import" -ShowElapsed
Show-StopwatchSummary
# User Interaction
Wait-ForInput -Message "Press any key to continue..."
Wait-ForInput -Timer 10 -ShowCountdown # Timed wait with countdown
# Logging
Write-Log -Message "Process completed" -Level INFO -LogFile "C:\Logs\app.log"
Export-MetricsToCSV -Metrics $stats -Path "C:\Data\metrics.csv"
# System
Get-WindowsVersion -OSVersion "10.0.22000"
================================================================================
#>
Write-Verbose "DEBUG: OrionUtils.psm1 loaded. PSScriptRoot = $PSScriptRoot"
$root = $PSScriptRoot
# --- Load Private functions (internal only) ---
$privatePath = Join-Path $root 'Functions\Private'
if (Test-Path $privatePath) {
Get-ChildItem -Path $privatePath -Filter *.ps1 | ForEach-Object {
Write-Verbose "Loading private function: $($_.BaseName)"
. $_.FullName
}
}
# --- Load Public functions (to be exported) ---
$publicPath = Join-Path $root 'Functions\Public'
$publicFunctions = @()
if (Test-Path $publicPath) {
Get-ChildItem -Path $publicPath -Filter *.ps1 | ForEach-Object {
Write-Verbose "Loading public function: $($_.BaseName)"
. $_.FullName
$publicFunctions += $_.BaseName
}
}
# --- Export public functions and aliases ---
if ($publicFunctions.Count -gt 0) {
Write-Verbose "Exporting functions: $($publicFunctions -join ', ')"
Export-ModuleMember -Function $publicFunctions
}
else {
Write-Warning "No public functions found to export from OrionUtils"
}