Want Text instead of XML When Executing Powershell Snippet in build.gradle on Windows

Hallo everyone,

I am trying to write a task which executes a couple of Powershell lines.

class PowerShellUtil {

    def execBlock(debug, block){
        println block
        // if one does not specify Little Endian or Big Endian here, the bytes contain the BOM.
        // but powershell does not like the BOM.
        // The Base64-encoded string passed to -EncodedCommand must encode the byte representation of a UTF-16LE ("Unicode") string - UTF-8 is not supported:
        def encodedBlock = block.getBytes("UTF-16LE").encodeBase64().toString()
        println encodedBlock
        //whether it is a list or a single string does not matter I think
        //def shellCommand = ["powershell.exe","-EncodedCommand",encodedBlock]
        def shellCommand = "powershell.exe -NonInteractive -NoProfile -OutputFormat Text  -EncodedCommand $encodedBlock"
        println shellCommand
        def process = shellCommand.execute()
        def out = new StringBuffer()
        def err = new StringBuffer()
        process.consumeProcessOutput(out, err)
        if(out.size() > 0 && debug) log.debug out
        if(err.size() > 0) log.error err

But if I call this (for simplicity, for testing) somewhere in the build.gradle file using

PowerShellUtil psUtil = new PowerShellUtil()
psUtil.execBlock(true, 'Write-Host "Test $(4+5)"')

I do not observe “Test 9” on the standard output but some CLI XML (containing “Test 9”, but unreadable for a human).

> Configure project :
Write-Host "Test $(4+5)"
powershell.exe -NonInteractive -NoProfile -OutputFormat Text  -EncodedCommand VwByAGkAdABlAC0ASABvAHMAdAAgACIAVABlAHMAdAAgACQAKAA0ACsANQApACIA
<Objs Version="" xmlns="http://schemas.microsoft.com/powershell/2004/04"><Obj S="progress" RefId="0"><TN RefId="0"><T>System.Management.Automation.PSCustomObject</T><T>System.Object</T></TN><MS><I64 N="SourceId">1</I64><PR N="Record"><AV>Module werden f?r erstmalige Verwendung vorbereitet.</AV><AI>0</AI><Nil /><PI>-1</PI><PC>-1</PC><T>Completed</T><SR>-1</SR><SD> </SD></PR></MS></Obj><Obj S="information" RefId="1"><TN RefId="1"><T>System.Management.Automation.InformationRecord</T><T>System.Object</T></TN><ToString>Test 9</ToString><Props><Obj N="MessageData" RefId="2"><TN RefId="2"><T>System.Management.Automation.HostInformationMessage</T><T>System.Object</T></TN><ToString>Test 9</ToString><Props><S N="Message">Test 9</S><B N="NoNewLine">false</B><S N="ForegroundColor">DarkYellow</S><S N="BackgroundColor">DarkMagenta</S></Props></Obj><S N="Source">Write-Host</S><DT N="TimeGenerated">2021-06-15T14:09:33.4225216+02:00</DT><Obj N="Tags" RefId="3"><TN RefId="3"><T>System.Collections.Generic.List`1[[System.String, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089]]</T><T>System.Object</T></TN><LST><S>PSHOST</S></LST></Obj><S N="User">ADHB\srg</S><S N="Computer">C001.adhb.local</S><U32 N="ProcessId">11072</U32><U32 N="NativeThreadId">7392</U32><U32 N="ManagedThreadId">14</U32></Props></Obj></Objs>

Executing the result of println shellCommand which is

powershell.exe -NonInteractive -NoProfile -OutputFormat Text  -EncodedCommand VwByAGkAdABlAC0ASABvAHMAdAAgACIAVABlAHMAdAAgACQAKAA0ACsANQApACIA

directly on the Powershell prompt yields “Test 9”, as expected.

PS > powershell.exe -NonInteractive -NoProfile -OutputFormat Text  -EncodedCommand VwByAGkAdABlAC0ASABvAHMAdAAgACIAVABlAHMAdAAgACQAKAA0ACsANQApACIA
Test 9
PS >

I was not able to find out how I can prevent powershell from printing CLI XML when called from within build.gradle.

Can someone maybe give me some advice on how I can obtain simple plain text standard output if I call Powershell snippets in build.gradle?
How do other people do that if they want to leverage the capabilities of powershell in their build scripts?


(gradle newbie)


PS > $host.Version

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      19041  610