YoavKlein
YoavKlein

Reputation: 2703

Running Powershell command Out-File in Azure Pipeline cuts lines

As part of a CI pipeline, I need to generate a list of all the files in the repository, including certain properties of them, and to Out-File it into a file. The command I use:

Get-ChildItem $Path -File -Recurse | Select-Object -Property LastWriteTime, @
 {
  label = "Size(KB)"
  expr = {  [string]::Format("{0:0.00}", $_.Length/1KB) } 
 }, FullName, <some_other_property> | Out-File $OutputFile
 

My problem is, that running this script from the command line gives the desired result.

However, running this during a Azure Pipeline build does 2 bad things:

  1. cuts the lines in the FullName column when they are long:
LastWriteTime     Size(KB)       Name
------------      -------        ----
<some_date>       <some size>   ASomeWhatLong...
  1. Doesn't display the rest of the properties such as <some_other_property>

If I turn FullName into Name it all goes OK, but I really need the FullName property.

Because I'm working in a Air-gapped environment I can't copy all the outputs and everything.

I've tried using the -Width flag for Out-File with no success.

Upvotes: 0

Views: 543

Answers (1)

Vivere
Vivere

Reputation: 2270

I believe what's happening under the hood, ps uses the ToString() method of the object created which outputs it as the Format-Table cmdlet does. You get truncated properties because of the Window's size. To look at it you could use:

(Get-Host).ui.RawUI.WindowSize

Probably this is too small.

What I would suggest is the following:

  • Pipe the object into Format-Table
Get-ChildItem $Path -File -Recurse | Select-Object -Property LastWriteTime, @
 {
  label = "Size(KB)"
  expr = {  [string]::Format("{0:0.00}", $_.Length/1KB) } 
 }, FullName, <some_other_property> | Format-Table | Out-String | Out-File $OutputFile

This probably won't work as it is, but you could play with the Format-Table's properties like: -Wrap. By default it will allocate enough space for the first properties, and the last one it would try to 'fit' it, which might look as:

LastWriteTime     Size(KB)       Name
------------      -------        ----
<some_date>       <some size>   ASomeWhatLong
                                foobarfoobarfo
                                foobarfoobarfo

To solve this, you can use the -Property argument, which needs to be as:

$propertWidth = [int]((Get-Host).ui.RawUI.WindowSize.Width / 3)
$property = @(
    @{ Expression = 'LastWriteTime'; Width = $propertWidth; },
    @{ Expression = 'Size(KB)'; Width = $propertWidth; },
    @{ Expression = 'FullName'; Width = $propertWidth; }
)

... | Format-Table -Property $property -Wrap | ...
  • If you don't mind having a JSON into your file, you could use:
Get-ChildItem $Path -File -Recurse | Select-Object -Property LastWriteTime, @
 {
  label = "Size(KB)"
  expr = {  [string]::Format("{0:0.00}", $_.Length/1KB) } 
 }, FullName, <some_other_property> | ConvertTo-Json | Out-File $OutputFile

But take into account that ConvertTo-Json has a default Depth of 2. This will also truncate your objects if you have nested objects. But as far as property lengths, it will do fine.

Upvotes: 1

Related Questions