vstest.console $myPathToUnitTests.dll
returns an array not a string therefore you will need to join all the strings (lines) together with the -join operator, like for html:
$TestResult -Join "<br />"
Or if you want to create a multi line text file:
$TestResult -Join [System.Environment]::NewLine
Note: for this, I recommend to use the system's default newline definition ([System.Environment]::NewLine
) rather than hardcoded newlines (like: `r`n
).
How could you know this?
You can't easily predict what an unknown cmdlet or command returns but the fact that PowerShell is quiet good in dealing with multiple objects (arrays) in a pipeline at once, is a reason to assume that every unknown cmdlet or command could potentially return multiple objects (or strings) collected in an array when assigned to a variable.
Needless to say, there are already dozens of similar PowerShell StackOverflow questions like this one that have the same cause. Nevertheless, the related questions itself differ too much to flag them as duplicates. In these other questions, the native PowerShell command Get-Content
is probably the most often cause of this PowerShell programming trap.
The best way to find out what is currently returned by the unknown cmdlet is to reveal its type with the GetType()
method (which doesn't work on a $Null
) or using the PSTypeNames
property (which usually returns an array of inherited types):
PS C:\> (get-content .\SingleLine.txt).PSTypeNames
System.String
System.Object
But that doesn't guaranty that it will always return the same (single) object type:
PS C:\> (get-content .\MultipleLines.txt).PSTypeNames
System.Object[]
System.Array
System.Object
The fact that (string) arrays are usually displayed over multiple lines (separated by a newline) directly at the host where they are stocked together (separated by a space) when they are encapsulated in a string could be a trigger to recognize the issue:
PS C:\> $a = "a", "b", "c"
PS C:\> $a
a
b
c
PS C:\> "$a"
a b c
PS C:\>
Will something like this always return an array?
The uncertainty that a cmdlet or command could return an object (string) or an array of objects (strings) raises indeed a new question (as you commented).
This uncertainty is actually easy to resolve in PowerShell; just wrap the output in an array (in parentheses with an @ at the beginning, see also: How can I force Powershell to return an array when a call only returns one object?):
- If the output was not already an array it will become an array with a single object (string)
- If the output was already an array it will stay exactly the same array.
In the later condition it is important to understand that no extra
levels are added to the resulting array
This technic is commonly used in PowerShell:
$a = "a", "b", "c"
@($a)[0] # still returns: "a"
@(@(@($a)))[0] # or even this returns: "a"
'` – Mathias R. Jessen Feb 09 '18 at 10:05
"` – iRon Feb 09 '18 at 10:12
'` – iRon Feb 09 '18 at 10:19
"` it is a **yes**. Important to note here is that if it was already an array it will stay the same array and *not* create another level (this is standard PowerShell behavior). – iRon Feb 09 '18 at 11:30