Get string instead of printing in terminal (info about certificates)

Hello everyone. I have a script that nicely formats the output of information about certificates on the router. It looks like this:

:for i from=0 to=([/certificate print count]-1) do={
  :local cert [/certificate get $i];
  :local name ($cert->"name");
  :local cn ($cert->"common-name");
  :local org ($cert->"organization");
  :local country ($cert->"country");
  :local state ($cert->"state");
  :local city ($cert->"locality");
  :local notBefore ($cert->"invalid-before");
  :local notAfter ($cert->"invalid-after");
  :local expiresAfter ($cert->"expires-after");
  :local digest ($cert->"digest-algorithm");
  :local keyType ($cert->"key-type");
  :local keySize ($cert->"key-size");
  :local usages ($cert->"key-usage");
  :local skid ($cert->"skid");
  :local fingerprint ($cert->"fingerprint");

  :local stateStr "";
  :if ($state != "") do={ :set stateStr (", " . $state) }

  :local cityStr "";
  :if ($city != "") do={ :set cityStr (", " . $city) }

  :local skidStr "-";
  :if ([:typeof $skid] = "str") do={ :set skidStr ([:pick $skid 0 10] . "...") }

  :local fingerprintStr "-";
  :if ([:typeof $fingerprint] = "str") do={ :set fingerprintStr ([:pick $fingerprint 0 8] . "...") }

  :local issuerStr ($org . " (" . $country . $stateStr . $cityStr . ")");
  
  :local usageStr [:pick $usages ([:find $usages ";Key Usage: "]+12) [:len $usages]];
  :if ([:len $usageStr] = 0) do={ :set usageStr $usages }

  :put (" 📄  Certificate #" . $i);
  :put ("Name:\t\t" . $name);
  :put ("Common Name (CN):\t" . $cn);
  :put ("Issuer:\t\t" . $issuerStr);
  :put ("Valid:\t\tfrom " . $notBefore . " to " . $notAfter);
  :put ("Expires in:\t" . $expiresAfter);
  :put ("Algorithm:\t" . $digest . ", " . $keyType . " " . $keySize . " bit");
  :put ("Key Usage:\t" . $usages);
  :put ("SKID:\t\t" . $skidStr);
  :put ("Fingerprint:\t" . $fingerprintStr);
  :put "";
}

But I want to rework it so that at the end I get a line with similar formatting. I try to glue it together, but I get nonsense. Then the same certificate is glued together. I have three of them. Then all three certificates are glued together 3-9 times. Can someone help?

Had to slightly rework. The appearance has changed, although it may have become more detailed, but I’m not sure it’s more readable.

:local certs [/certificate print detail as-value]
:local index 0
:local result ""

:foreach cert in=$certs do={
    :set result ($result . "   📄   Certificate #" . $index . "\r\n")

    :foreach key,value in=$cert do={
        :if ([:typeof $value] = "array") do={
            # For arrays, add key and empty line before values
            :set result ($result . "    " . [:pick ($key . ":                   ") 0 20] . "\r\n")
            :foreach item in=$value do={
                # Each array value with 20-space indentation
                :set result ($result . "                              " . $item . "\r\n")
            }
        } else={
            # For regular values just key and value
            :set result ($result . "    " . [:pick ($key . ":                   ") 0 20] . " " . $value . "\r\n")
        }
    }

    :set result ($result . "--------------------------------------------------\r\n")
    :set index ($index + 1)
}

:return $result

(reply to the first post, before the edit 2025-04-07 11:24:16 and 2nd post)
in that case the cycle is done differently, and the variable name should not be the same of one objects (already written)…
/certificate
:foreach crt in=[find] do={
:local cName [get $crt name ]
:local cCommName [get $crt common-name ]
:local cOrg [get $crt organization]
[…]
}

OR

/certificate
:foreach crt in=[find] do={
:local x [get $crt]
:local cName ($x->“name” )
:local cCommName ($x->“common-name” )
:local cOrg ($x->“organization”)
[…]
}
RouterOS do not support UTF-8 or others, only ASCII-7.
Until do not have full support, is better to put inside the string the emoji “:page_facing_up:” as “\F0\9F\93\84”