Write-ADTLogEntry from Pipeline

Using the old post about capturing non-ADT output into the log i tried this syntax

install-module microsoft.graph -Force -Verbose -ErrorAction continue | Out-String | Write-ADTLogEntry

Plenty of verbose output on the console but nada in the log file (plenty of other boilerplate in the log).

Am I missing something?

Replace Out-String with 4>&1.

Hmm, no dice. But i’m hearing you think this is a redirect verbose to standard issue?

Yeah it is because you PowerShell’s pipeline accepts input from stream 1 whereas verbose output is on stream 4. To get it through the pipe you have to redirect it to 1.

That was the trick, just needed to add not replace. This worked:

install-module microsoft.graph -Force -Verbose 4>&1 | out-string -stream | Write-ADTLogEntry

Thanks for the pointer!

3 Likes

The Out-String stuff really shouldn’t be necessary, however a result is a result! Here’s a link to part of our v4 codebase that does exactly what you’re trying to do, and this works well: PSAppDeployToolkit/src/PSAppDeployToolkit/Public/Unblock-ADTAppExecution.ps1 at 79684ff4fe1c19ead59086c941f449d82501df18 · PSAppDeployToolkit/PSAppDeployToolkit · GitHub

Basically, because the pipeline input is for a [System.String] object, the piped [VerboseRecord] object’s ToString() method is implicitly invoked upon passing it through, just as it would be when you’re passing it through to Out-String.

2 Likes

This topic was automatically closed after 6 days. New replies are no longer allowed.