{"id":4377,"date":"2022-10-17T14:07:50","date_gmt":"2022-10-17T21:07:50","guid":{"rendered":"https:\/\/SUMMALAI.COM\/?p=4377"},"modified":"2022-10-17T14:07:52","modified_gmt":"2022-10-17T21:07:52","slug":"how-to-generate-a-code-signing-certificate-and-sign-powershell-ps1-script","status":"publish","type":"post","link":"https:\/\/SUMMALAI.COM\/?p=4377","title":{"rendered":"How to Generate a Code Signing Certificate and Sign PowerShell .ps1 Script"},"content":{"rendered":"\n<p>This guide will show you how to issue a code signing certificate from your internal Certificate Authority, and how to use it to sign your code.<\/p>\n\n\n\n<p><strong>Topics<\/strong>&nbsp;covered in this article:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Trying to sign code with no certificate available<\/li><li>How to get a code signing certificate<\/li><li>Requesting a certificate once issued from the Local CA<\/li><li>How to code sign<a href=\"https:\/\/patchmypc.com\/generate-signing-cert-and-sign-powershell-ps1-script#topic6\"><\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Trying to sign code with no certificate available<\/h2>\n\n\n\n<p>If you would like to sign your own files, and this is done on a machine where no code signing certificate is present, you will get an error.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>If you are using&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/seccrypto\/signtool\" target=\"_blank\" rel=\"noreferrer noopener\">SignTool<\/a>&nbsp;to automatically select the best signing certificate, the verbose output will say:<br>SignTool Error: No certificates were found that met all the given criteria.<br><br><img fetchpriority=\"high\" decoding=\"async\" src=\"https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/01-codesign.png\" width=\"809\" height=\"125\" alt=\"\" srcset=\"https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/01-codesign.png 975w, https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/01-codesign-300x46.png 300w\"><\/li><li>If you are using the&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/microsoft.powershell.security\/set-authenticodesignature?view=powershell-7.2\" target=\"_blank\" rel=\"noreferrer noopener\">Set-AuthenticodeSignature<\/a>&nbsp;PowerShell cmdlet to do the signing you get the error message:<br>Set-AuthenticodeSignature : Cannot bind argument to parameter \u2018Certificate\u2019 because it is null.<br><img decoding=\"async\" src=\"https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/02-codesign.png\" width=\"808\" height=\"157\" alt=\"\" srcset=\"https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/02-codesign.png 1284w, https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/02-codesign-300x58.png 300w\"><\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">How to get a code signing certificate<\/h2>\n\n\n\n<p>One option would be to purchase a code signing certificate online from authorities such as&nbsp;<a href=\"https:\/\/www.digicert.com\/signing\/code-signing-certificates\" target=\"_blank\" rel=\"noreferrer noopener\">DigiCert<\/a>.<\/p>\n\n\n\n<p>Another one would be to issue one from your internal Certificate Authority, which you can do by following these steps:<\/p>\n\n\n\n<p><strong>Create and issue a signing certificate<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Open&nbsp;<strong>Certification Authority<\/strong>&nbsp;(<strong>certsrv.msc<\/strong>) on a machine where you have installed the certification authority.<\/li><li>Expand the name of the&nbsp;<strong>Certification Authority<\/strong>, then right-click on&nbsp;<strong>Certificate Templates<\/strong>&nbsp;and choose&nbsp;<strong>Manage<\/strong><br><img decoding=\"async\" src=\"https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/03-codesign.png\" width=\"424\" height=\"299\" alt=\"\" srcset=\"https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/03-codesign.png 424w, https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/03-codesign-300x212.png 300w\"><\/li><li>Right-Click on&nbsp;<strong>Code Signing<\/strong>&nbsp;under the&nbsp;<strong>Template Display Name&nbsp;<\/strong>column and choose&nbsp;<strong>Duplicate template<\/strong>.<br><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/04-codesign.png\" width=\"1560\" height=\"924\" alt=\"\" srcset=\"https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/04-codesign.png 1560w, https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/04-codesign-300x178.png 300w\"><\/li><li>On the properties of the new template, click on the&nbsp;<strong>General<\/strong>&nbsp;tab and give it the name you want. Also, choose the validity period. Save change with&nbsp;<strong>Apply<\/strong>.<br><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/05-codesign.png\" width=\"400\" height=\"560\" alt=\"\" srcset=\"https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/05-codesign.png 400w, https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/05-codesign-214x300.png 214w\"><\/li><li>Go on the&nbsp;<strong>Request Handling<\/strong>&nbsp;tab, and make sure&nbsp;<strong>Allow private key to be exported<\/strong>&nbsp;is enabled.<br><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/06-codesign.png\" width=\"399\" height=\"560\" alt=\"\" srcset=\"https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/06-codesign.png 399w, https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/06-codesign-214x300.png 214w\"><\/li><li>On the&nbsp;<strong>Subject Name<\/strong>&nbsp;tab, set the&nbsp;<strong>Subject name format<\/strong>&nbsp;to&nbsp;<strong>Common Name<\/strong>.<br><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/07-codesign.png\" width=\"401\" height=\"561\" alt=\"\" srcset=\"https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/07-codesign.png 401w, https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/07-codesign-214x300.png 214w\"><\/li><li>On the&nbsp;<strong>Extensions&nbsp;<\/strong>tab, make sure that the description of&nbsp;<strong>Key Usage<\/strong>&nbsp;contains&nbsp;<strong>Digital Signature<\/strong>.<br><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/08-codesign.png\" width=\"400\" height=\"560\" alt=\"\" srcset=\"https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/08-codesign.png 400w, https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/08-codesign-214x300.png 214w\"><\/li><li>On the Security tab, ensure that \u201c<strong>Authenticated Users<\/strong>\u201d have&nbsp;<strong>Read<\/strong>&nbsp;and&nbsp;<strong>Enroll<\/strong>&nbsp;permissions.<br><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/09-codesign.png\" width=\"400\" height=\"558\" alt=\"\" srcset=\"https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/09-codesign.png 400w, https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/09-codesign-215x300.png 215w\"><\/li><\/ol>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"alignleft\"><a href=\"https:\/\/patchmypc.com\/wp-content\/uploads\/2020\/05\/more-info-icon.svg\"><img decoding=\"async\" src=\"https:\/\/patchmypc.com\/wp-content\/uploads\/2020\/05\/more-info-icon.svg\" alt=\"More Information\" class=\"wp-image-50669\"\/><\/a><\/figure><\/div>\n\n\n<p><strong>Note:&nbsp;<\/strong>You might not want all Authenticated Users to be able to Enroll the certificate, you can add your own custom security group which should have these permissions.<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"9\"><li>You can now click OK and then close the Certificate Templates Console.<\/li><li>Back to&nbsp;<strong>Certification Authority<\/strong>, right click&nbsp;<strong>Certificate Templates<\/strong>, choose&nbsp;<strong>New&nbsp;<\/strong>and then select&nbsp;<strong>Certificate Template to Issue<\/strong>.<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/10-codesign.png\" width=\"784\" height=\"580\" alt=\"\" srcset=\"https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/10-codesign.png 784w, https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/10-codesign-300x222.png 300w\"><\/li><li>From the&nbsp;<strong>Enable Certificate Templates list<\/strong>, select your certificate template and confirm with&nbsp;<strong>OK<\/strong>.<br><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/11-codesign.png\" width=\"583\" height=\"373\" alt=\"\" srcset=\"https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/11-codesign.png 583w, https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/11-codesign-300x192.png 300w\"><\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Requesting a certificate on a machine to use it for code signing<\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li>On a domain joined machine, open&nbsp;<strong>mmc.exe<\/strong>.<\/li><li>Click&nbsp;<strong>File<\/strong>&nbsp;and then&nbsp;<strong>Add\/Remove Snap-in<\/strong>.<\/li><li>Select&nbsp;<strong>Certificates<\/strong>&nbsp;and then click on&nbsp;<strong>Add<\/strong>.<\/li><li>In the dialog box which appears, select&nbsp;<strong>My user account<\/strong>, then confirm with&nbsp;<strong>Finish<\/strong>.<\/li><li>In the console, expand&nbsp;<strong>Certificates \u2013 Current User<\/strong>, then expand&nbsp;<strong>Personal<\/strong>.<\/li><li>Right-click&nbsp;<strong>Certificates<\/strong>, then go to&nbsp;<strong>All Tasks<\/strong>&nbsp;and select&nbsp;<strong>Request New Certificate<\/strong>.<br><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/12-codesign.png\" width=\"602\" height=\"624\" alt=\"\" srcset=\"https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/12-codesign.png 602w, https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/12-codesign-289x300.png 289w\"><\/li><li>In the<strong>&nbsp;Certificate Enrollment<\/strong>&nbsp;window, click&nbsp;<strong>Next&nbsp;<\/strong>until you see a list of certificates to request. The issues certificate should appear in this list. Select it and confirm with&nbsp;<strong>Enroll<\/strong>.<br><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/13-codesign.png\" width=\"628\" height=\"459\" alt=\"\" srcset=\"https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/13-codesign.png 628w, https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/13-codesign-300x219.png 300w\"><\/li><li>The certificate should now be enrolled on that device and can be used for code signing.<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">How to sign your code<\/h2>\n\n\n\n<p>In terms of signing, you can either use&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/seccrypto\/signtool\" target=\"_blank\" rel=\"noreferrer noopener\">SignTool<\/a>&nbsp;or the&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/microsoft.powershell.security\/set-authenticodesignature\" target=\"_blank\" rel=\"noreferrer noopener\">Set-AuthenticodeSignature<\/a>&nbsp;PowerShell cmdlet.<\/p>\n\n\n\n<p><strong>Example 1<\/strong>: SignTool<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/14-codesign.png\" alt=\"\" class=\"wp-image-80493\"\/><\/figure>\n\n\n\n<p>In this example, we used the following arguments:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>sign = Digitally signs files. Digital signatures protect files from tampering, and enable users to verify the signer based on a signing certificate.<\/li><li>\/a = Automatically selects the best signing certificate. Sign Tool will find all valid certificates that satisfy all specified conditions and select the one that is valid for the longest time. In our case, it automatically selected the code signing certificate we enrolled.<\/li><li>\/v = Displays verbose output<\/li><li>\/fd = specifies the digest algorithm. In our case, we went with SHA256.<\/li><\/ul>\n\n\n\n<p><strong>Example 2<\/strong>: PowerShell cmdlet&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/microsoft.powershell.security\/set-authenticodesignature\">Set-AuthenticodeSignature<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/patchmypc.com\/wp-content\/uploads\/2022\/06\/15-codesign.png\" alt=\"\" class=\"wp-image-80494\"\/><\/figure>\n\n\n\n<p>In this example, on the server where the Patch My PC Publishing service is installed, from an elevated PowerShell ISE instance:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>On line 1, we get the code signing certificate we enrolled.<\/li><li>On line 3, we used the Set-AuthenticodeSignature cmdlet to sign our&nbsp;<strong>C:\\test.ps1<\/strong>&nbsp;file using the WSUS code signing certificate.<\/li><\/ul>\n\n\n\n<p>Ref: https:\/\/patchmypc.com\/generate-signing-cert-and-sign-powershell-ps1-script<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This guide will show you how to issue a code signing certificate from your internal Certificate Authority, and how to use it to sign your code. Topics&nbsp;covered in this article: Trying to sign code with no certificate available How to get a code signing certificate Requesting a certificate once issued from the Local CA How <a class=\"read-more\" href=\"https:\/\/SUMMALAI.COM\/?p=4377\">Read More<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[10,1224],"tags":[1340,1338,1339],"class_list":["post-4377","post","type-post","status-publish","format-standard","hentry","category-microsoft","category-powershell","tag-generate-a-code-signing-certificate-for-powershell-script","tag-sign-powershell-ps1-script","tag-sign-powershell-script"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts\/4377","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4377"}],"version-history":[{"count":2,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts\/4377\/revisions"}],"predecessor-version":[{"id":4379,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts\/4377\/revisions\/4379"}],"wp:attachment":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4377"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4377"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}