16
16
{
17
17
[Include('*-*.ps1')]$psScriptRoot
18
18
} | .>PipeScript
19
+ . EXAMPLE
20
+ {
21
+ [Include('https://pssvg.start-automating.com/Examples/PowerShellChevron.svg')]$PSChevron
22
+ } | .>PipeScript
19
23
#>
20
- [ValidateScript ({
21
- if ($_ -is [Management.Automation.Language.CommandAst ]) {
22
- return $_.CommandsElements [0 ].Value -in ' include' , ' includes'
24
+ [ValidateScript ({
25
+ $validating = $_
26
+ if ($validating -is [Management.Automation.Language.CommandAst ]) {
27
+ return $validating.CommandElements [0 ].Value -in ' include' , ' includes'
23
28
}
24
29
})]
25
30
[Alias (' Includes' )]
26
31
param (
27
32
# The File Path to Include
28
- [Parameter (Mandatory , Position = 0 )]
33
+ [Parameter (Mandatory , ParameterSetName = ' VariableAST' , Position = 0 )]
34
+ [Parameter (ParameterSetName = ' CommandAst' , Position = 0 )]
35
+ [Alias (' FullName' , ' Uri' , ' Url' )]
29
36
[string ]
30
37
$FilePath ,
31
38
@@ -42,11 +49,16 @@ $Passthru,
42
49
[string []]
43
50
$Exclude = ' \.[^\.]+\.ps1$' ,
44
51
52
+ # The variable that include will be applied to.
53
+ # If including files with wildcards, this will be the base path.
54
+ # Otherwise, this variable will be assigned to the included value.
45
55
[Parameter (Mandatory , ParameterSetName = ' VariableAST' , ValueFromPipeline )]
46
56
[Management.Automation.Language.VariableExpressionast ]
47
57
$VariableAst ,
48
58
49
59
60
+ # The CommandAST.
61
+ # This is provided by the transpiler when include is used as a keyword.
50
62
[Parameter (Mandatory , ParameterSetName = ' CommandAst' , ValueFromPipeline )]
51
63
[Management.Automation.Language.CommandAst ]
52
64
$CommandAst
@@ -74,14 +86,22 @@ process {
74
86
$ExecutionContext.SessionState.PSVariable.Set ($paramName , $mySentence.Parameters [$paramName ])
75
87
}
76
88
}
89
+
90
+ if ($mySentence.ArgumentList -and -not $FilePath ) {
91
+ $FilePath = $mySentence.ArgumentList [0 ]
92
+ }
77
93
}
78
94
79
95
# Determine the command we would be including (relative to the current path)
80
96
$includingCommand = $ExecutionContext.SessionState.InvokeCommand.GetCommand ($FilePath , ' All' )
81
97
if (-not $includingCommand ) { # if we could not determine the command, we may need to error out.
82
- if (-not $FilePath.Contains (' *' )) {
83
- Write-Error " Could not resolve $ ( $FilePath ) "
84
- return
98
+ if ($FilePath -match ' ^https?://' ) {
99
+ $includingUrl = $FilePath -as [uri ]
100
+ } else {
101
+ if (-not $FilePath.Contains (' *' )) {
102
+ Write-Error " Could not resolve $ ( $FilePath ) . Must be a command, path, or URI."
103
+ return
104
+ }
85
105
}
86
106
}
87
107
@@ -167,6 +187,52 @@ if ($Passthru) { [Environment]::NewLine + "`${$($includingCommand.Name)}"}
167
187
} elseif ($includingCommand ) {
168
188
IncludeFileContents $includingCommand.Source
169
189
}
190
+ elseif ($includingUrl ) {
191
+ $webResponse = Invoke-WebRequest - Uri $includingUrl
192
+ if ($webResponse.Content -is [string ]) {
193
+ $restResponse = Invoke-RestMethod - Uri $includingUrl
194
+ if (
195
+ (
196
+ ($restResponse -is [PSObject ]) -or
197
+ ($restResponse -is [Object []])
198
+ ) -and (
199
+ -not ($webResponse.Content -as [xml ])
200
+ ) -and (
201
+ $restResponse -isnot [string ]
202
+ )
203
+ ) {
204
+ [ScriptBlock ]::Create((@ (
205
+ " @'"
206
+ $ ($restResponse | ConvertTo-Json - Depth 100 )
207
+ " '@ |"
208
+ " ConvertFrom-JSON"
209
+ ) -join [Environment ]::NewLine))
210
+ }
211
+ else {
212
+ if ($restResponse -is [string ] -and $ (
213
+ $restScript = try { [scriptblock ]::Create($restResponse ) } catch { $null }
214
+ $restScript
215
+ )) {
216
+ $restScript
217
+ }
218
+ else {
219
+ [ScriptBlock ]::Create((@ (
220
+ " $ ( if ($webResponse.Content -as [xml ]) { ' [xml]' }) @'"
221
+ $ (if ($restResponse -is [xml ]) { $restResponse.OuterXML } else { $restResponse })
222
+ " '@"
223
+ ) -join [Environment ]::NewLine))
224
+ }
225
+ }
226
+ } elseif ($webResponse.Content -is [byte []]) {
227
+ [ScriptBlock ]::Create(
228
+ " @'" + [Environment ]::NewLine +
229
+ [Convert ]::ToBase64String(
230
+ $webResponse.Content ,
231
+ ' InsertLineBreaks'
232
+ ) + [Environment ]::NewLine +
233
+ " '@" )
234
+ }
235
+ }
170
236
171
237
if ($psCmdlet.ParameterSetName -eq ' ScriptBlock' -or
172
238
$VariableAst.VariablePath -match ' ^null$' ) {
@@ -176,9 +242,13 @@ if ($psCmdlet.ParameterSetName -eq 'ScriptBlock' -or
176
242
$includedScript
177
243
}
178
244
179
- } elseif ($VariableAst.VariablePath -and $includingCommand ) {
245
+ } elseif ($VariableAst.VariablePath -and $IncludedScript ) {
180
246
[ScriptBlock ]::Create(" $ ( $VariableAst ) = $IncludedScript " )
181
- } elseif ($VariableAst.VariablePath -notmatch ' ^null$' ) {
247
+ }
248
+ elseif ($CommandAst ) {
249
+ $IncludedScript
250
+ }
251
+ elseif ($VariableAst.VariablePath -notmatch ' ^null$' ) {
182
252
[ScriptBlock ]::Create(@"
183
253
:ToIncludeFiles foreach (`$ file in (Get-ChildItem -Path "$ ( $VariableAst ) " -Filter "$FilePath " -Recurse)) {
184
254
if (`$ file.Extension -ne '.ps1') { continue } # Skip if the extension is not .ps1
0 commit comments