Powershell Docker cmds into object array

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

Maybe ConvertFrom-String can help you.

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 ";"

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.

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

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

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

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