Displaying array with headers causes PS script to end when called from a cmd script

Hi,
I have a test.bat script with the following 2 lines:

powershell "&{&'C:\tmp\test.ps1';exit $LastExitCode}" -Noninteractive 
echo %errorlevel%

my C:\tmp\test.ps1 script contains the following lines:

[System.Collections.ArrayList]$ArrayWithHeader = @()
$ArrayOfValues = @('a@b.com','b@c.com','c@d.com','d@e.com')
foreach ($obj in $ArrayOfValues) 
{ 
     $val = [pscustomobject]@{'mail'=$obj;'date'=(Get-Date)}
     $ArrayWithHeader.add($val) | Out-Null
     $val=$null
}
$ArrayWithHeader
"after array"

When I call the test.bat script from a cmd Window (or from a task), “after array” is never displayed, nor the $ArrayWithHeader array.
Strangely, if I add the command “$PSVersiontable.PSversion” as first line of my script everything is displayed as expected.
I tried adding some try{}catch{} code to it but it didn’t change anything. Calling the ps script directly works fine of course.
I am using Powershell version 5.1.19041.1320 and saw the same behavior on Windows 10 and Windows 2016 server.

Any idea why displaying $PSVersiontable.PSversion would cause the script behavior to change and why it doesn’t work without it?

thanks,

l. stoeckli (… sounds swiss … :wink: )
Welcome to the forum. :wave:t4:

Please do us a favour and format the code you post in this forum as code using the preformatted text button (</>).

Thanks in advance.

The command line to call PowerShell with a file looks a little weird to me. I’d write it this way:

PowerShell.exe -File 'C:\tmp\test.ps1' -Noninteractive

Your PowerShellCode looks a little convoluted as well. You may try it this way:

$ArrayOfValues = @('a@b.com', 'b@c.com', 'c@d.com', 'd@e.com')
$ArrayWithHeader =
foreach ($obj in $ArrayOfValues) {
    [pscustomobject]@{
        mail = $obj
        date = Get-Date
    }
}
$ArrayWithHeader

Hi Olaf,

thanks for your response even if it doesn’t tell me why my code is failing without the “$PSVerstionTable.PSVersion” command. I have no control over the .bat script as it is a tool I am using and my Powershell script is also more complicated, I only created the simplest example I could to reproduce the problem, as it seems that any array with headers will cause the problem. I believe the .bat script, which is also much longer, calls PS scripts that way to catch any error not handled correctly in the PS scripts.

How about this?

$ArrayOfValues = @('a@b.com', 'b@c.com', 'c@d.com', 'd@e.com')

$ArrayOfValues |
    Select-Object -Property @{Name = 'mail'; Expression={$_}},
                            @{Name = 'date'; Expression={Get-Date}}

My question was not about creating the array, your code works fine but is still not displayed when called from my bat script.
My problem is that when my PS script is called from a bat script containing

powershell "&{&'C:\tmp\test.ps1';exit $LastExitCode}" -Noninteractive 
echo %errorlevel%

nothing is displayed, except if I use the command “$PSVersiontable.PSversion” first. It seems to be the case for any array with column headers.

I understand that it could have been more appropriate to report is as a bug to Microsoft but I would probably wait very long before it is answered, if at all.
thanks

I don’t know what to advice for you. I cannot explain. I just can suggest some fixes to your issue.

The output will show up when you call PowerShell in your CMD with the parameter -file instead of the call operator &.
The output will show up when you call PowerShell in your CMD without the parameter -NonInteractive.
The output will show up when you pipe your array inside your PowerShell script (or results) to Out-String.