Repository navigation
Expand file tree
/
Copy pathAdd-ScriptMetadata.ps1
More file actions
202 lines (165 loc) · 4.96 KB
/
Copy pathAdd-ScriptMetadata.ps1
File metadata and controls
202 lines (165 loc) · 4.96 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<#
.SYNOPSIS
Add standardized metadata headers to all PowerShell scripts in the categories directory.
.DESCRIPTION
This script scans all .ps1 files in the categories directory and adds standardized
comment-based help metadata if not already present. It preserves existing code while
adding proper .SYNOPSIS, .DESCRIPTION, .NOTES, .REQUIREMENTS, .EXAMPLE, .OUTPUTS,
.EXITCODES, and .CHANGELOG sections.
.NOTES
Author : Mattias Magnusson
Project : DeploySmart
Created : 2026-12-02
Modified : 2026-12-02
Version : 1.0.0
License : MIT
.REQUIREMENTS
- PowerShell 5.1 or later
.EXAMPLE
.\Add-ScriptMetadata.ps1
Processes all .ps1 files in categories subdirectories.
.EXAMPLE
.\Add-ScriptMetadata.ps1 -WhatIf
Shows what would be changed without making modifications.
.OUTPUTS
None
.EXITCODES
0 = Success
1 = General Error
#>
[CmdletBinding(SupportsShouldProcess = $true)]
param(
[Parameter()]
[string]$RootPath = "J:\dev\DeploySmart-Scripts\categories",
[Parameter()]
[switch]$Force
)
# Metadata template
$metadataTemplate = @'
<#
.SYNOPSIS
{0}
.DESCRIPTION
{1}
.NOTES
Author : Mattias Magnusson
Project : DeploySmart
Created : {2}
Modified : {3}
Version : {4}
License : MIT
.REQUIREMENTS
- PowerShell 5.1 or later
- Administrator privileges
- Internet connection
.EXAMPLE
.\{5}
{6}
.OUTPUTS
None
.EXITCODES
0 = Success
1 = General Error
2 = Download Failed
3 = Installation Failed
.CHANGELOG
{4}
- Initial release
#>
'@
# Function to extract basic info from existing script
function Get-ScriptInfo {
param([string]$FilePath)
$content = Get-Content $FilePath -Raw
$fileName = [IO.Path]::GetFileNameWithoutExtension($FilePath)
$category = Split-Path (Split-Path $FilePath -Parent) -Leaf
# Try to extract existing synopsis/description from first comment line
$synopsis = ""
$description = ""
$created = (Get-Item $FilePath).CreationTime.ToString('yyyy-MM-dd')
$modified = (Get-Item $FilePath).LastWriteTime.ToString('yyyy-MM-dd')
$version = "1.0.0"
# Look for existing comment-based help
if ($content -match '<#\s*(.+?)\s*#>') {
$help = $matches[1]
if ($help -match '\.SYNOPSIS\s*(.+?)(?:\n\.\w+|$)') {
$synopsis = $matches[1].Trim()
}
if ($help -match '\.DESCRIPTION\s*(.+?)(?:\n\.\w+|$)') {
$description = $matches[1].Trim()
}
if ($help -match '\.NOTES.*?Created\s*:\s*(\S+)') {
$created = $matches[1].Trim()
}
if ($help -match '\.NOTES.*?Modified\s*:\s*(\S+)') {
$modified = $matches[1].Trim()
}
if ($help -match '\.NOTES.*?Version\s*:\s*(\S+)') {
$version = $matches[1].Trim()
}
}
# Fallback: look for simple comment line
elseif ($content -match '^#\s*(.+)$') {
$synopsis = $matches[1].Trim()
}
# Generate defaults if not found
if (-not $synopsis) {
$synopsis = "Automate the installation of $fileName."
}
if (-not $description) {
$description = "Downloads and installs the latest available version of $fileName. Designed for unattended deployments and remote management systems."
}
$example = "Installs the latest version of $fileName using default settings."
return @{
FileName = $fileName
Category = $category
Synopsis = $synopsis
Description = $description
Created = $created
Modified = $modified
Version = $version
Example = $example
}
}
# Function to check if script already has proper metadata
function Has-Metadata {
param([string]$Content)
return $Content -match '^\s*<#\s*\.SYNOPSIS'
}
# Main processing
$files = Get-ChildItem -Path $RootPath -Recurse -Filter "*.ps1" -File
$processed = 0
$skipped = 0
foreach ($file in $files) {
$info = Get-ScriptInfo $file.FullName
$content = Get-Content $file.FullName -Raw
if (Has-Metadata $content -and -not $Force) {
Write-Host "Skipping (already has metadata): $($file.Name)" -ForegroundColor Yellow
$skipped++
continue
}
# Remove existing simple comment header if present
if ($content -match '^#\s*.+?\n') {
$content = $content -replace '^#\s*.+?\n', ''
}
# Build new metadata header
$metadata = [string]::Format($metadataTemplate,
$info.Synopsis,
$info.Description,
$info.Created,
$info.Modified,
$info.Version,
$info.FileName + ".ps1",
$info.Example
)
$newContent = $metadata + "`n" + $content.TrimStart()
if ($PSCmdlet.ShouldProcess($file.FullName, "Add metadata header")) {
Set-Content -Path $file.FullName -Value $newContent -Encoding UTF8
Write-Host "Updated: $($file.Name)" -ForegroundColor Green
$processed++
}
}
Write-Host "`nSummary:" -ForegroundColor Cyan
Write-Host " Processed: $processed"
Write-Host " Skipped: $skipped"
Write-Host " Total: $($files.Count)"