Skip to content

Commit 3768543

Browse files
author
Steve Lee (POWERSHELL HE/HIM) (from Dev Box)
committed
PSScript resource
1 parent efcb07c commit 3768543

File tree

3 files changed

+273
-0
lines changed

3 files changed

+273
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
{
2+
"$schema": "https://aka.ms/dsc/schemas/v3/bundled/resource/manifest.json",
3+
"type": "Microsoft.DSC.Transitional/PowerShell7Script",
4+
"description": "Enable running PS7 scripts inline",
5+
"version": "0.1.0",
6+
"get": {
7+
"executable": "pwsh",
8+
"args": [
9+
"-NoLogo",
10+
"-NonInteractive",
11+
"-NoProfile",
12+
"-ExecutionPolicy",
13+
"Bypass",
14+
"-Command",
15+
"-Command",
16+
"$Input | ./psscript.ps1 get"
17+
]
18+
},
19+
"set": {
20+
"executable": "pwsh",
21+
"args": [
22+
"-NoLogo",
23+
"-NonInteractive",
24+
"-NoProfile",
25+
"-ExecutionPolicy",
26+
"Bypass",
27+
"-Command",
28+
"-Command",
29+
"$Input | ./psscript.ps1 set"
30+
]
31+
},
32+
"test": {
33+
"executable": "pwsh",
34+
"args": [
35+
"-NoLogo",
36+
"-NonInteractive",
37+
"-NoProfile",
38+
"-ExecutionPolicy",
39+
"Bypass",
40+
"-Command",
41+
"$Input | ./psscript.ps1 test"
42+
]
43+
},
44+
"export": {
45+
"executable": "pwsh",
46+
"args": [
47+
"-NoLogo",
48+
"-NonInteractive",
49+
"-NoProfile",
50+
"-ExecutionPolicy",
51+
"Bypass",
52+
"-Command",
53+
"$Input | ./psscript.ps1 export"
54+
]
55+
},
56+
"exitCodes": {
57+
"0": "Success",
58+
"1": "PowerShell script execution failed"
59+
},
60+
"schema": {
61+
"embedded": {
62+
"type": "object",
63+
"properties": {
64+
"getScript": {
65+
"type": ["string", null]
66+
},
67+
"setScript": {
68+
"type": ["string", null]
69+
},
70+
"testScript": {
71+
"type": ["string", null]
72+
},
73+
"exportScript": {
74+
"type": ["string", null]
75+
},
76+
"output": {
77+
"type": ["object", null]
78+
}
79+
}
80+
}
81+
}
82+
}

resources/PSScript/psscript.ps1

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
[CmdletBinding()]
4+
param(
5+
[Parameter(Mandatory = $true, Position = 0)]
6+
[ValidateSet('Get', 'Set', 'Test', 'Export')]
7+
[string]$Operation,
8+
[Parameter(Mandatory = $false, Position = 1, ValueFromPipeline = $true)]
9+
[string]$jsonInput = '@{}'
10+
)
11+
12+
$traceQueue = [System.Collections.Queue]::new()
13+
14+
function Write-DscTrace {
15+
param(
16+
[Parameter(Mandatory = $true)]
17+
[ValidateSet('Error', 'Warn', 'Info', 'Debug', 'Trace')]
18+
[string]$Level,
19+
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
20+
[string]$Message
21+
)
22+
23+
$trace = @{$Level.ToLower() = $Message } | ConvertTo-Json -Compress
24+
$traceQueue.Enqueue($trace)
25+
}
26+
27+
$scriptObject = $jsonInput | ConvertFrom-Json
28+
29+
$script = switch ($Operation) {
30+
'Get' {
31+
$scriptObject.GetScript
32+
}
33+
'Set' {
34+
$scriptObject.SetScript
35+
}
36+
'Test' {
37+
$scriptObject.TestScript
38+
}
39+
'Export' {
40+
$scriptObject.ExportScript
41+
}
42+
}
43+
44+
if ($null -eq $script) {
45+
Write-DscTrace -Level Info -Message "No script found for operation '$Operation'."
46+
exit 0
47+
}
48+
49+
$ps = [PowerShell]::Create().AddScript({
50+
$DebugPreference = 'Continue'
51+
$VerbosePreference = 'Continue'
52+
$ErrorActionPreference = 'Stop'
53+
}).AddScript($script)
54+
$ps.Streams.Error.add_DataAdded({
55+
param($sender, $args)
56+
Write-DscTrace -Level Error -Message $sender.Message
57+
})
58+
$ps.Streams.Warning.add_DataAdded({
59+
param($sender, $args)
60+
Write-DscTrace -Level Warn -Message $sender.Message
61+
})
62+
$ps.Streams.Information.add_DataAdded({
63+
param($sender, $args)
64+
Write-DscTrace -Level Trace -Message $sender.Message
65+
})
66+
$ps.Streams.Verbose.add_DataAdded({
67+
param($sender, $args)
68+
Write-DscTrace -Level Info -Message $sender.Message
69+
})
70+
$ps.Streams.Debug.add_DataAdded({
71+
param($sender, $args)
72+
Write-DscTrace -Level Debug -Message $sender.Message
73+
})
74+
$outputObjects = [System.Collections.Generic.List[Object]]::new()
75+
76+
try {
77+
$asyncResult = $ps.BeginInvoke()
78+
while (-not $asyncResult.IsCompleted) {
79+
While ($traceQueue.Count -gt 0) {
80+
$trace = $traceQueue.Dequeue()
81+
$host.ui.WriteErrorLine($trace)
82+
}
83+
}
84+
$outputCollection = $ps.EndInvoke($asyncResult)
85+
foreach ($output in $outputCollection) {
86+
$outputObjects.Add($output)
87+
}
88+
}
89+
catch {
90+
Write-DscTrace -Level Error -Message $_.Exception.Message
91+
exit 1
92+
}
93+
finally {
94+
$ps.Dispose()
95+
}
96+
97+
if ($ps.HadErrors) {
98+
# If there are any errors, we will exit with an error code
99+
Write-DscTrace -Level Error -Message 'Errors occurred during script execution.'
100+
exit 1
101+
}
102+
103+
While ($traceQueue.Count -gt 0) {
104+
$trace = $traceQueue.Dequeue()
105+
$host.ui.WriteErrorLine($trace)
106+
}
107+
108+
@{
109+
output = $outputObjects
110+
} | ConvertTo-Json -Compress -Depth 10
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
{
2+
"$schema": "https://aka.ms/dsc/schemas/v3/bundled/resource/manifest.json",
3+
"type": "Microsoft.DSC.Transitional/WindowsPowerShellScript",
4+
"description": "Enable running Windows PowerShell 5.1 scripts inline",
5+
"version": "0.1.0",
6+
"get": {
7+
"executable": "powershell",
8+
"args": [
9+
"-NoLogo",
10+
"-NonInteractive",
11+
"-NoProfile",
12+
"-ExecutionPolicy",
13+
"Bypass",
14+
"-Command",
15+
"-Command",
16+
"$Input | ./psscript.ps1 get"
17+
]
18+
},
19+
"set": {
20+
"executable": "powershell",
21+
"args": [
22+
"-NoLogo",
23+
"-NonInteractive",
24+
"-NoProfile",
25+
"-ExecutionPolicy",
26+
"Bypass",
27+
"-Command",
28+
"-Command",
29+
"$Input | ./psscript.ps1 set"
30+
]
31+
},
32+
"test": {
33+
"executable": "powershell",
34+
"args": [
35+
"-NoLogo",
36+
"-NonInteractive",
37+
"-NoProfile",
38+
"-ExecutionPolicy",
39+
"Bypass",
40+
"-Command",
41+
"$Input | ./psscript.ps1 test"
42+
]
43+
},
44+
"export": {
45+
"executable": "powershell",
46+
"args": [
47+
"-NoLogo",
48+
"-NonInteractive",
49+
"-NoProfile",
50+
"-ExecutionPolicy",
51+
"Bypass",
52+
"-Command",
53+
"$Input | ./psscript.ps1 export"
54+
]
55+
},
56+
"exitCodes": {
57+
"0": "Success",
58+
"1": "PowerShell script execution failed"
59+
},
60+
"schema": {
61+
"embedded": {
62+
"type": "object",
63+
"properties": {
64+
"getScript": {
65+
"type": ["string", null]
66+
},
67+
"setScript": {
68+
"type": ["string", null]
69+
},
70+
"testScript": {
71+
"type": ["string", null]
72+
},
73+
"exportScript": {
74+
"type": ["string", null]
75+
},
76+
"output": {
77+
"type": ["object", null]
78+
}
79+
}
80+
}
81+
}}

0 commit comments

Comments
 (0)