object vs string output
-
28-03-2021 - |
Pergunta
Why is it when I run this command in powershell:
$output = (svn stat) -match '^\?' # which $output is an [object] in this case.
I get this output in the PowerGUI Script Editor Console when viewing the variable $output:
? Projects\TestProject\Thisisatest.txt
? Projects\TestProject\Thisisatest - Copy.txt
but when I run this command:
[string] $output = (svn stat) -match '^\?'
I get these results:
? Projects\TestProject\Thisisatest.txt ? Projects\TestProject\Thisisatest - Copy.txt
It seems when using [string] it does some type of encoding where it takes away newlines and I'm not sure why?
UPDATED user978511 here is the the small example code and output
Clear-Host
Set-Location "C:\svn\Development\Projects\TestProjects\build"
$output = (svn stat) -match '^\?'
'$output results'
$output
'$output2 results'
[string] $output2 = (svn stat) -match '^\?'
$output2
Output:
$output results
? Projects\TestProject\Thisisatest.txt
? Projects\TestProject\Thisisatest - Copy.txt
$output2 results
? Projects\TestProject\Thisisatest.txt ? Projects\TestProject\Thisisatest - Copy.txt
Thanks for anyone that can help.
Solução
When PowerShell casts an array to a single string it converts the members to strings and concatenates them with spaces between each.
$output2 = [string]::join([environment]::newline, $output)
will do what you want.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow