-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathInvoke-PSNote.ps1
More file actions
64 lines (53 loc) · 2.03 KB
/
Invoke-PSNote.ps1
File metadata and controls
64 lines (53 loc) · 2.03 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
<#
.SYNOPSIS
Invokes a PSNote by executing its script or snippet.
.DESCRIPTION
Internal function that executes a PSNote based on its Kind property.
For Script notes, it runs the referenced script file.
For Snippet notes, it executes the snippet as a script block.
.PARAMETER Note
The PSNote object to invoke. Must be of Kind 'Script' or 'Snippet'.
.EXAMPLE
Invoke-PSNote -Note $note
Executes the provided note.
.NOTES
This is an internal function. Validates that the note store is initialized
before execution.
.OUTPUTS
System.Object
Returns any output from the executed script or snippet.
#>
Function Invoke-PSNote {
[cmdletbinding(DefaultParameterSetName = "Note")]
param(
[parameter(Mandatory = $true, ParameterSetName = "Note", Position = 0)]
[PSNote]$Note
)
Test-PSNotesInitalize
switch ($Note.Kind) {
Script {
$scriptPath = $Note.Snippet
if (-not [string]::IsNullOrWhiteSpace($scriptPath)) {
$scriptPath = $scriptPath.Trim()
}
if ([string]::IsNullOrWhiteSpace($scriptPath)) {
throw "Cannot invoke Script note '$($Note.Name)': Path is empty."
}
if (-not (Test-Path -LiteralPath $scriptPath -PathType Leaf)) {
throw "Cannot invoke Script note '$($Note.Name)': Script file not found at path: $scriptPath"
}
$resolvedScriptPath = (Get-Item -LiteralPath $scriptPath -ErrorAction Stop).FullName
& $resolvedScriptPath
}
Snippet {
if ([string]::IsNullOrWhiteSpace($Note.Snippet)) {
throw "Cannot invoke Snippet note '$($Note.Name)': Snippet is empty."
}
$scriptBlock = [ScriptBlock]::Create($Note.Snippet)
Invoke-Command -ScriptBlock $scriptBlock
}
default {
throw "Cannot invoke note '$($Note.Name)': Unknown Kind: $($Note.Kind)"
}
}
}