Powershell Docker cmds into object array

This topic contains 7 replies, has 3 voices, and was last updated by Profile photo of Edwin Ashdown Edwin Ashdown 2 weeks, 3 days ago.

  • Author
    Posts
  • #57488
    Profile photo of Edwin Ashdown
    Edwin Ashdown
    Participant

    Hi,

    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.

    Edwin

  • #57493
    Profile photo of Daan Weda
    Daan Weda
    Participant

    Maybe ConvertFrom-String can help you.

  • #57628
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    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 = @"
    REPOSITORY`tTAG`tIMAGEID
    blue`tlatest`t{GUID}
    red`tlatest`t{GUID}
    purple`tlatest`t{GUID}
    "@
    
    $testTab | ConvertFrom-Csv -Delimiter "`t"
    

    or spaces:

    $testSpaces = @"
    REPOSITORY  TAG       IMAGEID
    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
    Profile photo of Edwin Ashdown
    Edwin Ashdown
    Participant

    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
    Profile photo of Edwin Ashdown
    Edwin Ashdown
    Participant

    Update:

    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
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    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
    Profile photo of Edwin Ashdown
    Edwin Ashdown
    Participant

    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
    Profile photo of Edwin Ashdown
    Edwin Ashdown
    Participant

    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.

    Cheers
    Edwin

You must be logged in to reply to this topic.