File: exec_wrapper.ps1

package info (click to toggle)
ansible-core 2.19.0~beta6-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 32,628 kB
  • sloc: python: 180,313; cs: 4,929; sh: 4,601; xml: 34; makefile: 21
file content (717 lines) | stat: -rw-r--r-- 24,782 bytes parent folder | download | duplicates (2)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
# (c) 2025 Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

using namespace System.Collections
using namespace System.Collections.Generic
using namespace System.Diagnostics.CodeAnalysis
using namespace System.IO
using namespace System.Linq
using namespace System.Management.Automation
using namespace System.Management.Automation.Language
using namespace System.Management.Automation.Security
using namespace System.Reflection
using namespace System.Security.Cryptography
using namespace System.Text

[SuppressMessageAttribute(
    "PSUseCmdletCorrectly",
    "",
    Justification = "ConvertFrom-Json is being used in a steppable pipeline and works this way."
)]
[CmdletBinding()]
param (
    [Parameter(ValueFromPipeline)]
    [string]
    $InputObject,

    [Parameter()]
    [IDictionary]
    $Manifest,

    [Parameter()]
    [switch]
    $EncodeInputOutput,

    [Parameter()]
    [Version]
    $MinOSVersion,

    [Parameter()]
    [Version]
    $MinPSVersion,

    [Parameter()]
    [string]
    $TempPath,

    [Parameter()]
    [PSObject]
    $ActionParameters
)

begin {
    $DebugPreference = "SilentlyContinue"
    $ErrorActionPreference = "Stop"
    $ProgressPreference = "SilentlyContinue"

    if ($PSCommandPath -and (Test-Path -LiteralPath $PSCommandPath)) {
        Remove-Item -LiteralPath $PSCommandPath -Force
    }

    # Try and set the console encoding to UTF-8 allowing Ansible to read the
    # output of the wrapper as UTF-8 bytes.
    try {
        [Console]::InputEncoding = [Console]::OutputEncoding = [UTF8Encoding]::new()
    }
    catch {
        # PSRP will not have a console host so this will fail. The line here is
        # to ignore sanity checks.
        $null = $_
    }

    if ($MinOSVersion) {
        [version]$actualOSVersion = (Get-Item -LiteralPath $env:SystemRoot\System32\kernel32.dll).VersionInfo.ProductVersion

        if ($actualOSVersion -lt $MinOSVersion) {
            @{
                failed = $true
                msg = "This module cannot run on this OS as it requires a minimum version of $MinOSVersion, actual was $actualOSVersion"
            } | ConvertTo-Json -Compress
            $Host.SetShouldExit(1)
            return
        }
    }

    if ($MinPSVersion) {
        if ($PSVersionTable.PSVersion -lt $MinPSVersion) {
            @{
                failed = $true
                msg = "This module cannot run as it requires a minimum PowerShell version of $MinPSVersion, actual was ""$($PSVersionTable.PSVersion)"""
            } | ConvertTo-Json -Compress
            $Host.SetShouldExit(1)
            return
        }
    }

    # $Script:AnsibleManifest = @{}  # Defined in process/end.
    $Script:AnsibleShouldConstrain = [SystemPolicy]::GetSystemLockdownPolicy() -eq 'Enforce'
    $Script:AnsibleTrustedHashList = [HashSet[string]]::new([StringComparer]::OrdinalIgnoreCase)
    $Script:AnsibleUnsupportedHashList = [HashSet[string]]::new([StringComparer]::OrdinalIgnoreCase)
    $Script:AnsibleWrapperWarnings = [List[string]]::new()
    $Script:AnsibleTempPath = @(
        # Wrapper defined tmpdir
        [Environment]::ExpandEnvironmentVariables($TempPath)
        # Fallback to user's tmpdir
        [Path]::GetTempPath()
        # Should not happen but just in case use the current dir.
        $pwd.Path
    ) | Where-Object {
        if (-not $_) {
            return $false
        }

        try {
            Test-Path -LiteralPath $_ -ErrorAction Ignore
        }
        catch {
            # Access denied could cause Test-Path to throw an exception.
            $false
        }
    } | Select-Object -First 1
    $Script:AnsibleTempScripts = [List[string]]::new()
    $Script:AnsibleClrFacadeSet = $false

    Function Convert-JsonObject {
        param(
            [Parameter(Mandatory, ValueFromPipeline)]
            [AllowNull()]
            [object]
            $InputObject
        )

        process {
            # Using the full type name is important as PSCustomObject is an
            # alias for PSObject which all piped objects are.
            if ($InputObject -is [System.Management.Automation.PSCustomObject]) {
                $value = @{}
                foreach ($prop in $InputObject.PSObject.Properties) {
                    $value[$prop.Name] = Convert-JsonObject -InputObject $prop.Value
                }
                $value
            }
            elseif ($InputObject -is [Array]) {
                , @($InputObject | Convert-JsonObject)
            }
            else {
                $InputObject
            }
        }
    }

    Function Get-AnsibleScript {
        [CmdletBinding()]
        param (
            [Parameter(Mandatory)]
            [string]
            $Name,

            [Parameter()]
            [switch]
            $IncludeScriptBlock,

            [Parameter()]
            [switch]
            $SkipHashCheck
        )

        if (-not $Script:AnsibleManifest.scripts.Contains($Name)) {
            $err = [ErrorRecord]::new(
                [Exception]::new("Could not find the script '$Name'."),
                "ScriptNotFound",
                [ErrorCategory]::ObjectNotFound,
                $Name)
            $PSCmdlet.ThrowTerminatingError($err)
        }

        $scriptInfo = $Script:AnsibleManifest.scripts[$Name]
        $scriptBytes = [Convert]::FromBase64String($scriptInfo.script)
        $scriptContents = [Encoding]::UTF8.GetString($scriptBytes)

        $sbk = $null
        if ($IncludeScriptBlock) {
            $sbk = [Parser]::ParseInput(
                $scriptContents,
                $Name,
                [ref]$null,
                [ref]$null).GetScriptBlock()
        }

        $outputValue = [PSCustomObject]@{
            Name = $Name
            Script = $scriptContents
            Path = $scriptInfo.path
            ScriptBlock = $sbk
            ShouldConstrain = $false
        }

        if (-not $Script:AnsibleShouldConstrain) {
            $outputValue
            return
        }

        if (-not $SkipHashCheck) {
            $sha256 = [SHA256]::Create()
            $scriptHash = [BitConverter]::ToString($sha256.ComputeHash($scriptBytes)).Replace("-", "")
            $sha256.Dispose()

            if ($Script:AnsibleUnsupportedHashList.Contains($scriptHash)) {
                $err = [ErrorRecord]::new(
                    [Exception]::new("Provided script for '$Name' is marked as unsupported in CLM mode."),
                    "ScriptUnsupported",
                    [ErrorCategory]::SecurityError,
                    $Name)
                $PSCmdlet.ThrowTerminatingError($err)
            }
            elseif ($Script:AnsibleTrustedHashList.Contains($scriptHash)) {
                $outputValue
                return
            }
        }

        # If we have reached here we are running in a locked down environment
        # and the script is not trusted in the signed hashlists. Check if it
        # contains the authenticode signature and verify that using PowerShell.
        # [SystemPolicy]::GetFilePolicyEnforcement(...) is a new API but only
        # present in Server 2025+ so we need to rely on the known behaviour of
        # Get-Command to fail with CommandNotFoundException if the script is
        # not allowed to run.
        $outputValue.ShouldConstrain = $true
        if ($scriptContents -like "*`r`n# SIG # Begin signature block`r`n*") {
            Set-WinPSDefaultFileEncoding

            # If the script is manually signed we need to ensure the signature
            # is valid and trusted by the OS policy.
            # We must use '.ps1' so the ExternalScript WDAC check will apply.
            $tmpFile = [Path]::Combine($Script:AnsibleTempPath, "ansible-tmp-$([Guid]::NewGuid()).ps1")
            try {
                [File]::WriteAllBytes($tmpFile, $scriptBytes)
                $cmd = Get-Command -Name $tmpFile -CommandType ExternalScript -ErrorAction Stop

                # Get-Command caches the file contents after loading which we
                # use to verify it was not modified before the signature check.
                $expectedScript = $cmd.OriginalEncoding.GetString($scriptBytes)
                if ($expectedScript -ne $cmd.ScriptContents) {
                    $err = [ErrorRecord]::new(
                        [Exception]::new("Script has been modified during signature check."),
                        "ScriptModifiedTrusted",
                        [ErrorCategory]::SecurityError,
                        $Name)
                    $PSCmdlet.ThrowTerminatingError($err)
                }

                $outputValue.ShouldConstrain = $false
            }
            catch [CommandNotFoundException] {
                $null = $null  # No-op but satisfies the linter.
            }
            finally {
                if (Test-Path -LiteralPath $tmpFile) {
                    Remove-Item -LiteralPath $tmpFile -Force
                }
            }
        }

        if ($outputValue.ShouldConstrain -and $IncludeScriptBlock) {
            # If the script is untrusted and a scriptblock was requested we
            # error out as the sbk would have run in FLM.
            $err = [ErrorRecord]::new(
                [Exception]::new("Provided script for '$Name' is not trusted to run."),
                "ScriptNotTrusted",
                [ErrorCategory]::SecurityError,
                $Name)
            $PSCmdlet.ThrowTerminatingError($err)
        }
        else {
            $outputValue
        }
    }

    Function Get-NextAnsibleAction {
        [CmdletBinding()]
        param ()

        $action, $newActions = $Script:AnsibleManifest.actions
        $Script:AnsibleManifest.actions = @($newActions | Select-Object)

        $actionName = $action.name
        $actionParams = $action.params
        $actionScript = Get-AnsibleScript -Name $actionName -IncludeScriptBlock

        foreach ($kvp in $action.secure_params.GetEnumerator()) {
            if (-not $kvp.Value) {
                continue
            }

            $name = $kvp.Key
            $actionParams.$name = $kvp.Value | ConvertTo-SecureString -AsPlainText -Force
        }

        [PSCustomObject]@{
            Name = $actionName
            ScriptBlock = $actionScript.ScriptBlock
            Parameters = $actionParams
        }
    }

    Function Get-AnsibleExecWrapper {
        [CmdletBinding()]
        param (
            [Parameter()]
            [switch]
            $ManifestAsParam,

            [Parameter()]
            [switch]
            $EncodeInputOutput,

            [Parameter()]
            [switch]
            $IncludeScriptBlock
        )

        $scriptInfo = Get-AnsibleScript -Name exec_wrapper.ps1 -IncludeScriptBlock:$IncludeScriptBlock
        $params = @{
            # TempPath may contain env vars that change based on the runtime
            # environment. Ensure we use that and not the $script:AnsibleTempPath
            # when starting the exec wrapper.
            TempPath = $TempPath
            EncodeInputOutput = $EncodeInputOutput.IsPresent
        }

        $inputData = $null
        if ($ManifestAsParam) {
            $params.Manifest = $Script:AnsibleManifest
        }
        else {
            $inputData = ConvertTo-Json -InputObject $Script:AnsibleManifest -Depth 99 -Compress
            if ($EncodeInputOutput) {
                $inputData = [Convert]::ToBase64String([Encoding]::UTF8.GetBytes($inputData))
            }
        }

        [PSCustomObject]@{
            ScriptInfo = $scriptInfo
            Parameters = $params
            InputData = $inputData
        }
    }

    Function Import-PowerShellUtil {
        [CmdletBinding()]
        param (
            [Parameter(Mandatory)]
            [string[]]
            $Name
        )

        foreach ($moduleName in $Name) {
            $moduleInfo = Get-AnsibleScript -Name $moduleName -IncludeScriptBlock
            $moduleShortName = [Path]::GetFileNameWithoutExtension($moduleName)
            $null = New-Module -Name $moduleShortName -ScriptBlock $moduleInfo.ScriptBlock |
                Import-Module -Scope Global
        }
    }

    Function Import-CSharpUtil {
        [CmdletBinding()]
        param (
            [Parameter(Mandatory)]
            [string[]]
            $Name
        )

        Import-PowerShellUtil -Name Ansible.ModuleUtils.AddType.psm1

        $isBasicUtil = $false
        $csharpModules = foreach ($moduleName in $Name) {
            $scriptInfo = Get-AnsibleScript -Name $moduleName

            if ($scriptInfo.ShouldConstrain) {
                throw "C# module util '$Name' is not trusted and cannot be loaded."
            }
            if ($moduleName -eq 'Ansible.Basic.cs') {
                $isBasicUtil = $true
            }

            $scriptInfo.Script
        }

        $fakeModule = [PSCustomObject]@{
            Tmpdir = $Script:AnsibleTempPath
        }
        $warningFunc = [PSScriptMethod]::new('Warn', {
                param($message)
                $Script:AnsibleWrapperWarnings.Add($message)
            })
        $fakeModule.PSObject.Members.Add($warningFunc)
        Add-CSharpType -References $csharpModules -AnsibleModule $fakeModule

        if ($isBasicUtil) {
            # Ansible.Basic.cs is a special case where we need to provide it
            # with the wrapper warnings list so it injects it into the result.
            [Ansible.Basic.AnsibleModule]::_WrapperWarnings = $Script:AnsibleWrapperWarnings
        }
    }

    Function Import-SignedHashList {
        [CmdletBinding()]
        param (
            [Parameter(Mandatory, ValueFromPipeline)]
            [string]
            $Name
        )

        process {
            try {
                # We skip the hash check to ensure we verify based on the
                # authenticode signature and not whether it's trusted by an
                # existing signed hash list.
                $scriptInfo = Get-AnsibleScript -Name $Name -SkipHashCheck
                if ($scriptInfo.ShouldConstrain) {
                    throw "script is not signed or not trusted to run."
                }

                $hashListAst = [Parser]::ParseInput(
                    $scriptInfo.Script,
                    $Name,
                    [ref]$null,
                    [ref]$null)
                $manifestAst = $hashListAst.Find({ $args[0] -is [HashtableAst] }, $false)
                if ($null -eq $manifestAst) {
                    throw "expecting a single hashtable in the signed manifest."
                }

                $out = $manifestAst.SafeGetValue()
                if (-not $out.Contains('Version')) {
                    throw "expecting hash list to contain 'Version' key."
                }
                if ($out.Version -ne 1) {
                    throw "unsupported hash list Version $($out.Version), expecting 1."
                }

                if (-not $out.Contains('HashList')) {
                    throw "expecting hash list to contain 'HashList' key."
                }

                $out.HashList | ForEach-Object {
                    if ($_ -isnot [hashtable] -or -not $_.ContainsKey('Hash') -or $_.Hash -isnot [string] -or $_.Hash.Length -ne 64) {
                        throw "expecting hash list to contain hashtable with Hash key with a value of a SHA256 strings."
                    }

                    if ($_.Mode -eq 'Trusted') {
                        $null = $Script:AnsibleTrustedHashList.Add($_.Hash)
                    }
                    elseif ($_.Mode -eq 'Unsupported') {
                        # Allows us to provide a better error when trying to run
                        # something in CLM that is marked as unsupported.
                        $null = $Script:AnsibleUnsupportedHashList.Add($_.Hash)
                    }
                    else {
                        throw "expecting hash list entry for $($_.Hash) to contain a mode of 'Trusted' or 'Unsupported' but got '$($_.Mode)'."
                    }
                }
            }
            catch {
                $_.ErrorDetails = [ErrorDetails]::new("Failed to process signed manifest '$Name': $_")
                $PSCmdlet.WriteError($_)
            }
        }
    }

    Function New-TempAnsibleFile {
        [OutputType([string])]
        [CmdletBinding()]
        param (
            [Parameter(Mandatory)]
            [string]
            $FileName,

            [Parameter(Mandatory)]
            [string]
            $Content
        )

        $name = [Path]::GetFileNameWithoutExtension($FileName)
        $ext = [Path]::GetExtension($FileName)
        $newName = "$($name)-$([Guid]::NewGuid())$ext"

        $path = Join-Path -Path $Script:AnsibleTempPath $newName
        Set-WinPSDefaultFileEncoding
        [File]::WriteAllText($path, $Content, [UTF8Encoding]::new($false))

        $path
    }

    Function Set-WinPSDefaultFileEncoding {
        [CmdletBinding()]
        param ()

        # WinPS defaults to the locale encoding when loading a script from the
        # file path but in Ansible we expect it to always be UTF-8 without a
        # BOM. This lazily sets an internal field so pwsh reads it as UTF-8.
        # If we don't do this then scripts saved as UTF-8 on the Ansible
        # controller will not run as expected.
        if ($PSVersionTable.PSVersion -lt '6.0' -and -not $Script:AnsibleClrFacadeSet) {
            $clrFacade = [PSObject].Assembly.GetType('System.Management.Automation.ClrFacade')
            $defaultEncodingField = $clrFacade.GetField('_defaultEncoding', [BindingFlags]'NonPublic, Static')
            $defaultEncodingField.SetValue($null, [UTF8Encoding]::new($false))
            $Script:AnsibleClrFacadeSet = $true
        }
    }

    Function Write-AnsibleErrorJson {
        [CmdletBinding()]
        param (
            [Parameter(Mandatory)]
            [ErrorRecord]
            $ErrorRecord,

            [Parameter()]
            [string]
            $Message = "failure during exec_wrapper"
        )

        $exception = @(
            "$ErrorRecord"
            "$($ErrorRecord.InvocationInfo.PositionMessage)"
            "+ CategoryInfo          : $($ErrorRecord.CategoryInfo)"
            "+ FullyQualifiedErrorId : $($ErrorRecord.FullyQualifiedErrorId)"
            ""
            "ScriptStackTrace:"
            "$($ErrorRecord.ScriptStackTrace)"

            if ($ErrorRecord.Exception.StackTrace) {
                "$($ErrorRecord.Exception.StackTrace)"
            }
        ) -join ([Environment]::NewLine)

        @{
            failed = $true
            msg = "${Message}: $ErrorRecord"
            exception = $exception
        } | ConvertTo-Json -Compress
        $host.SetShouldExit(1)
    }

    Function Write-PowerShellClixmlStderr {
        [CmdletBinding()]
        param (
            [Parameter(Mandatory)]
            [AllowEmptyString()]
            [string]
            $Output
        )

        if (-not $Output) {
            return
        }

        # -EncodedCommand in WinPS will output CLIXML to stderr. This attempts to parse
        # it into a human readable format otherwise it'll just output the raw CLIXML.
        $wroteStderr = $false
        if ($Output.StartsWith('#< CLIXML')) {
            $clixml = $Output -split "\r?\n"
            if ($clixml.Count -eq 2) {
                try {
                    # PSSerialize.Deserialize doesn't tell us what streams each record
                    # is for so we get the S attribute manually.
                    $streams = @(([xml]$clixml[1]).Objs.GetEnumerator() | ForEach-Object { $_.S })
                    $objects = @([PSSerializer]::Deserialize($clixml[1]))

                    for ($i = 0; $i -lt $objects.Count; $i++) {
                        $msg = $objects[$i]
                        if ($msg -isnot [string] -or $streams.Length -le $i) {
                            continue
                        }

                        # Doesn't use TrimEnd() so it only removes the last newline
                        if ($msg.EndsWith([Environment]::NewLine)) {
                            $msg = $msg.Substring(0, $msg.Length - [Environment]::NewLine.Length)
                        }
                        $stream = $streams[$i]
                        switch ($stream) {
                            'error' { $host.UI.WriteErrorLine($msg) }
                            'debug' { $host.UI.WriteDebugLine($msg) }
                            'verbose' { $host.UI.WriteVerboseLine($msg) }
                            'warning' { $host.UI.WriteWarningLine($msg) }
                        }
                    }
                    $wroteStderr = $true
                }
                catch {
                    $null = $_
                }
            }
        }
        if (-not $wroteStderr) {
            $host.UI.WriteErrorLine($Output.TrimEnd())
        }
    }

    # To handle optional input for the incoming manifest and optional input to
    # the subsequent action we optionally run this step in the begin or end
    # block.
    $jsonPipeline = $null
    $actionPipeline = $null
    $setupManifest = {
        [CmdletBinding()]
        param (
            [Parameter()]
            [switch]
            $ExpectingInput
        )

        if ($jsonPipeline) {
            $Script:AnsibleManifest = $jsonPipeline.End()[0]
            $jsonPipeline.Dispose()
            $jsonPipeline = $null
        }
        else {
            $Script:AnsibleManifest = $Manifest
        }

        if ($Script:AnsibleShouldConstrain) {
            $Script:AnsibleManifest.signed_hashlist | Import-SignedHashList
        }

        $actionInfo = Get-NextAnsibleAction
        $actionParams = $actionInfo.Parameters

        if ($ActionParameters) {
            foreach ($prop in $ActionParameters.PSObject.Properties) {
                $actionParams[$prop.Name] = $prop.Value
            }
        }

        $actionPipeline = { & $actionInfo.ScriptBlock @actionParams }.GetSteppablePipeline()
        $actionPipeline.Begin($ExpectingInput)
        if (-not $ExpectingInput) {
            $null = $actionPipeline.Process()
        }
    }

    try {
        if ($Manifest) {
            # If the manifest was provided through the parameter, we can start the
            # action pipeline and all subsequent input (if any) will be sent to the
            # action.
            # It is important that $setupManifest is called by dot sourcing or
            # else the pipelines started in it loose access to all parent scopes.
            # https://github.com/PowerShell/PowerShell/issues/17868
            . $setupManifest -ExpectingInput:$MyInvocation.ExpectingInput
        }
        else {
            # Otherwise the first part of the input is the manifest json with the
            # chance for extra data afterwards.
            $jsonPipeline = { ConvertFrom-Json | Convert-JsonObject }.GetSteppablePipeline()
            $jsonPipeline.Begin($true)
        }
    }
    catch {
        Write-AnsibleErrorJson -ErrorRecord $_
    }
}

process {
    try {
        if ($actionPipeline) {
            # We received our manifest and started the action pipeline, redirect
            # all further input to that pipeline.
            $null = $actionPipeline.Process($InputObject)
        }
        elseif ([string]::Equals($InputObject, "`0`0`0`0")) {
            # Special marker used to indicate all subsequent input is for the
            # action. Setup that pipeline and finalise the manifest.
            . $setupManifest -ExpectingInput
        }
        elseif ($jsonPipeline) {
            # Data is for the JSON manifest, decode if needed.
            if ($EncodeInputOutput) {
                $jsonPipeline.Process([Encoding]::UTF8.GetString([Convert]::FromBase64String($InputObject)))
            }
            else {
                $jsonPipeline.Process($InputObject)
            }
        }
    }
    catch {
        Write-AnsibleErrorJson -ErrorRecord $_
    }
}

end {
    try {
        if ($jsonPipeline) {
            # Only manifest input was received, process it now and start the
            # action pipeline with no input being provided.
            . $setupManifest
        }

        $out = $actionPipeline.End()
        if ($EncodeInputOutput) {
            [Convert]::ToBase64String([Encoding]::UTF8.GetBytes($out))
        }
        else {
            $out
        }
    }
    catch {
        Write-AnsibleErrorJson -ErrorRecord $_
    }
    finally {
        $actionPipeline.Dispose()
        if ($Script:AnsibleTempScripts) {
            Remove-Item -LiteralPath $Script:AnsibleTempScripts -Force -ErrorAction Ignore
        }
    }
}