Powershell Docker cmds into object array

This topic contains 7 replies, has 3 voices, and was last updated by  Edwin Ashdown 1 year, 8 months ago.

  • Author
  • #57488

    Edwin Ashdown


    I am trying to run docker commands in powershell and then record the output. The problem is that when you run a docker command in powershell for instance "docker images" , what is returned is a list strings. The structure of columns and values is not available.

    So how do i turn the below output into an object array ?
    Docker string output

    Thanks in advance.


  • #57493

    Daan Weda

    Maybe ConvertFrom-String can help you.

  • #57628

    Rob Simmers

    If ConvertFrom-String doesn't work, it's tough to really help without having the actual text. The formatting is going to be space or tab delimited, so you can use ConverFrom-CSV to manipulate the string into an object:

    $testTab = @"
    $testTab | ConvertFrom-Csv -Delimiter "`t"

    or spaces:

    $testSpaces = @"
    blue        latest    {GUID}
    red         latest    {GUID}
    purple      latest    {GUID}
    #Replace more than on space with semi-colon
    $testSpaces -replace "[ ]{2,}", ";"
    $testSpaces | ConvertFrom-CSV -Delimiter ";"
  • #57715

    Edwin Ashdown

    Hi ,

    Thanks for the reply's.

    ConvertTo-String worked a treat , just a matter of working out the format for the template.
    It will have to be unique for every command but otherwise works perfect.

  • #57845

    Edwin Ashdown


    Using a Template file works but when the template text is in a here-string it errors, no idea why it errors with a disk error.

    Any help appreciated.

    $template =@'
    {!REPOSITORY*:REPOSITORY}             TAG                 IMAGE ID            CREATED             SIZE
    {REPOSITORY*:microsoft/mssql-server-2014-express-windows}   {TAG:latest}              {IMAGEID:3576f77b8bd5}        {CREATED:2 days ago}          {SIZE:9.97 GB}
    docker images |ConvertFrom-String -TemplateFile $template -OutVariable Images | Out-Null
    ConvertFrom-String : Cannot find drive. A drive with the name '{!REPOSITORY*' does not exist.
    At line:10 char:16
    + ... ker images |ConvertFrom-String -TemplateFile $template -OutVariable I ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: ({!REPOSITORY*:String) [ConvertFrom-String], DriveNotFoundException
        + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.StringManipulation.ConvertFromStringCommand
  • #57859

    Rob Simmers

    It's expecting a file path, not a string containing a template. You'd have to save the here-string to temp file and then reference it.

    $tempFile = [IO.Path]::GetTempFileName()
    Add-Content -Path $tempFile -Value $template
    docker images | ConvertFrom-String -TemplateFile $tempFile -OutVariable Images | Out-Null
  • #57902

    Edwin Ashdown

    Thank you for the reply.
    You are correct in the that error was due to the ConvertFrom-String parameter of -TemplateFile was expecting a file path but my real issue was that i was using -TemplateFile in the first place, where as i should have been using -TemplateContent with the $variable.

    I couldn't see the forrest through the trees.
    Always learning

  • #57910

    Edwin Ashdown

    Rob ,
    I have to say that it was your post that pointed me to my problem as your answer had me wondering why i my code was using a filepath, it was then that i realized that i was using the incorrect parameter.
    So thank you for the answer and the light you shined on my mistake else i would still be wonder what was up.


You must be logged in to reply to this topic.