0

The following code:

$values = Get-Content ./values.yaml
Write-Host $values

Gives the error:

Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Message'. Specified method is not supported.

Running Write-Host $values.ToString() does not give an error, but it only outputs the text of "System.Object[]".

How can I use Write-Host to output the text of this file?

NOTE: I would also like to preserve the line endings in the file. (I just found that Write-Host "$values" will give the text, but the line endings are gone.)

(Note, I really am going to be using Write-Verbose, but Write-Host is similar and easier to test and ask about.)

Vaccano
  • 78,325
  • 149
  • 468
  • 850
  • Any particular reason why you're using `Write-Host`? – Santiago Squarzon Jun 01 '21 at 19:47
  • @SantiagoSquarzon - As I said in the bottom of my post, I really plan to use `Write-Verbose`. The 3rd party system I am integrating this with will log it at a "Verbose" level if I use `Write-Verbose`. But it is easier to write and debug the question with `Write-Host`. – Vaccano Jun 01 '21 at 19:49
  • 1
    The short answer is that `Get-Content` alone returns an array of lines. You can use `-Raw` to return the contents as a single string. More detail available in the linked question's answers! – briantist Jun 01 '21 at 19:53
  • 1
    @braintist- Thank you! Sad thing is I think this has got me before! I am going to leave this here as a "signpost" for my future self rather than delete it. – Vaccano Jun 01 '21 at 19:55
  • 1
    @Vaccano happens to the best of us, I still sometimes put commas between arguments to commands in PowerShell ‍♂️and other mistakes I should really know better about – briantist Jun 01 '21 at 19:57
  • 2
    For your future self, as previous comments `-Raw` works perfect for what you need, in addition if you want to convert objects into a `multiline-string` you can use `Out-String`. i.e.: `Write-Host (Get-Service | Out-String)` will return a string representation of the object instead of the object type `System.Object[]`. – Santiago Squarzon Jun 01 '21 at 20:00

0 Answers0