{"id":8449,"date":"2022-02-01T23:38:13","date_gmt":"2022-02-01T22:38:13","guid":{"rendered":"https:\/\/michlstechblog.info\/blog\/?p=8449"},"modified":"2022-02-03T08:43:01","modified_gmt":"2022-02-03T07:43:01","slug":"powershell-define-a-x509-certificate-in-a-script","status":"publish","type":"post","link":"https:\/\/michlstechblog.info\/blog\/powershell-define-a-x509-certificate-in-a-script\/","title":{"rendered":"Powershell: Define a x509 certificate in a script"},"content":{"rendered":"<div class=\"twoclick_social_bookmarks_post_8449 social_share_privacy clearfix 1.6.4 locale-en_US sprite-en_US\"><\/div><div class=\"twoclick-js\"><script type=\"text\/javascript\">\/* <![CDATA[ *\/\njQuery(document).ready(function($){if($('.twoclick_social_bookmarks_post_8449')){$('.twoclick_social_bookmarks_post_8449').socialSharePrivacy({\"services\":{\"flattr\":{\"uid\":\"Michl\",\"status\":\"on\",\"the_title\":\"Powershell%3A%20Define%20a%20x509%20certificate%20in%20a%20script\",\"the_excerpt\":\"Hi%2C%0D%0A%0D%0Athe%20best%20way%20to%20store%20a%20certificate%20in%20a%20powershell%20script%20is%20in%20an%20byte%20array.%20But%20how%20to%20get%20such%20a%20byte%20array%20as%20string%20from%20an%20existing%20certificate%3F%0D%0A%20%28more%26hellip%3B%29\",\"txt_info\":\"2 clicks for more data protection:\\r\\n\\r\\nOnly when you click here, the button will be come active and you can send your recommendation to Flattr. When activating, data are transmitted to third parties. \",\"perma_option\":\"off\"}},\"txt_help\":\"When you activate these fields by clicking, information to Flattr may be transferred abroad, and probably may also stored there.\",\"settings_perma\":\"Enable permanently and accept data transmission. \",\"info_link\":\"http:\\\/\\\/www.heise.de\\\/ct\\\/artikel\\\/2-Klicks-fuer-mehr-Datenschutz-1333879.html\",\"uri\":\"https:\\\/\\\/michlstechblog.info\\\/blog\\\/powershell-define-a-x509-certificate-in-a-script\\\/\",\"post_id\":8449,\"post_title_referrer_track\":\"Powershell%3A+Define+a+x509+certificate+in+a+script\",\"display_infobox\":\"on\"});}});\n\/* ]]> *\/<\/script><\/div><p>Hi,<\/p>\n<p>the best way to store a certificate in a powershell script is in an byte array. But how to get such a byte array as string from an existing certificate?<br \/>\n<!--more--><br \/>\nFirst load the certificate from a file<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\nPS D:\\&gt; $oMyCert=new-object System.Security.Cryptography.X509Certificates.X509Certificate2(&quot;D:\\user\\myCert.cer&quot;)\r\n<\/pre>\n<p>Then print out the array as string (Output shortend)<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\nPS D:\\&gt; &#x5B;string]::Join(&quot;,&quot;,$oMyCert.RawData)\r\n48,130,4,151,48,130,3,127,160,3,2,1,2,2,20,37,238,178,117,203,149,76,144,234,220,33,66,40,173,193,73,57,107,211,105,48,13,6,9,42,134,72,134,247,13,1,1,11,5,0,48,129,173,49,11,48,9,6,3,85,4,6,19,2,68,69,49,16,48,14,6,3,85,4,8,12,7,71,101,114,109,97,110,.....\r\n<\/pre>\n<p>Copy the output from the console to define a byte array variable in your script (certificate shortend). To select a text in powershell by lines hold the ALT key pressed while selecting.<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n&#x5B;byte&#x5B;]]$aMyCert=@(48,130,4,151,48,130,3,127,......)\r\n<\/pre>\n<p>In your script create a X509 object by<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n$oMyCert=new-object System.Security.Cryptography.X509Certificates.X509Certificate2 -ArgumentList @(,$aMyCert)\r\n<\/pre>\n<p>Importend is to submit the byte array argument by the -ArgumentList switch otherwise the X509Certificate2 constructor recognizes each item of the array as argument. This would result in an error:<br \/>\n<strong><br \/>\nCannot find an overload for &#8220;X509Certificate2&#8221; and the argument count: &#8220;1120&#8221;<\/strong><\/p>\n<p>Michael<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, the best way to store a certificate in a powershell script is in an byte array. But how to get such a byte array as string from an existing certificate?<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[344,5,4],"tags":[84,673,133,72,1434],"class_list":["post-8449","post","type-post","status-publish","format-standard","hentry","category-powershell-scripting","category-windowsknowhow","category-windowsscripts","tag-certificate","tag-define","tag-powershell","tag-script","tag-x509"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/8449","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/comments?post=8449"}],"version-history":[{"count":10,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/8449\/revisions"}],"predecessor-version":[{"id":8459,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/8449\/revisions\/8459"}],"wp:attachment":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/media?parent=8449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/categories?post=8449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/tags?post=8449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}