Skip to content

Output Handling in ManagementExtension-Samples/IntunePSTemplate.ps1 #11

@persident

Description

@persident

For the x86 -> x64 context switch, the IntunePSTemplate.ps1 sample sets the RedirectStandardOutput property for the ProcessStartInfo object to $true, but does not consume the StandardOutput. This can cause the new x64 child process to hang indefinitely:

>> (..) When the child process writes enough data to fill its redirected stream, it is dependent on the parent. The child process waits for the next write operation until the parent reads from the full stream or closes the stream. (..) <<
Source: ProcessStartInfo.RedirectStandardOutput Property

When the StandardOutput is not consumed by the parent script, the $pinfo.RedirectStandardOutput should be set to $false.

There is another issue: $exitCode = $p.ExitCode gets called without waiting for the child process to exit. Therefore, $exitCode will come back as $NULL most of the time. Better code would look like this:

$stderr = $p.StandardError.ReadToEnd()
$p.WaitForExit()
$exitCode = $p.ExitCode

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions