{"id":151,"date":"2022-10-10T11:40:23","date_gmt":"2022-10-10T03:40:23","guid":{"rendered":"https:\/\/aabirtech.com\/shiblenomanblog\/?p=151"},"modified":"2022-10-10T11:40:24","modified_gmt":"2022-10-10T03:40:24","slug":"code-sign-using-powershell","status":"publish","type":"post","link":"https:\/\/aabirtech.com\/shiblenomanblog\/2022\/10\/10\/code-sign-using-powershell\/","title":{"rendered":"Code sign using PowerShell"},"content":{"rendered":"\n<p>Code signing is important before releasing our software to the client. In the <a rel=\"noreferrer noopener\" href=\"https:\/\/aabirtech.com\/shiblenomanblog\/2022\/09\/26\/net-publish-using-powershell\/\" data-type=\"URL\" data-id=\"https:\/\/aabirtech.com\/shiblenomanblog\/2022\/09\/26\/net-publish-using-powershell\/\" target=\"_blank\">previous post<\/a>, we use a PowerShell script to publish our code. After publishing the code, we need to sign the code before release. We must have code cert for signing. Install the code sign in your machine. Certificate installs process is simple using certificate import wizard. <br><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/aabirtech.com\/shiblenomanblog\/wp-content\/uploads\/2022\/10\/image.png\" alt=\"\" class=\"wp-image-152\" width=\"353\" height=\"343\" srcset=\"https:\/\/aabirtech.com\/shiblenomanblog\/wp-content\/uploads\/2022\/10\/image.png 668w, https:\/\/aabirtech.com\/shiblenomanblog\/wp-content\/uploads\/2022\/10\/image-300x292.png 300w\" sizes=\"auto, (max-width: 353px) 100vw, 353px\" \/><\/figure><\/div>\n\n\n<p>Windows Certificate installation wizard. <\/p>\n\n\n\n<p>Once the certificate is install, we can procced to code signing process. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cd src\n$paths = Get-ChildItem -include *.csproj -Recurse\nforeach($pathobject in $paths)\n{\n     cd $pathobject.directory.fullName\n     dotnet publish -o ..\\..\\__DEPLOY__\\Program\n}\ncd ..\\..<\/code><\/pre>\n\n\n\n<p>This part of the code from <a rel=\"noreferrer noopener\" href=\"https:\/\/aabirtech.com\/shiblenomanblog\/2022\/09\/26\/net-publish-using-powershell\/\" data-type=\"URL\" data-id=\"https:\/\/aabirtech.com\/shiblenomanblog\/2022\/09\/26\/net-publish-using-powershell\/\" target=\"_blank\">previous post<\/a> this script publish all the project in the solution. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$cert = Get-ChildItem -Path Cert:\\CurrentUser\\My -CodeSigningCert\n\nGet-ChildItem -recurse -path '__DEPLOY__\\' -Include *.exe, *.dll | ForEach-Object {\t\n\t$signingParameters = @{\n\t\tFilePath      =  $_.FullName\n\t\tCertificate   = $cert\n\t\tHashAlgorithm = 'SHA256'\n\t\tTimestampServer = 'http:\/\/timestamp.digicert.com'\n\t}\n\tSet-AuthenticodeSignature @signingParameters\n\t\n}<\/code><\/pre>\n\n\n\n<p>The first line gets the signing cert path. <\/p>\n\n\n\n<p>Next line, navigate to the code that needed to sign, in this example path is &#8216;__DEPLOY__&#8217;, sign will only the files with exe and dll extension. Subsequent lines shows different params for signing. You can find more about the code signing params from PowerShell <a rel=\"noreferrer noopener\" href=\"https:\/\/learn.microsoft.com\/en-us\/powershell\/module\/microsoft.powershell.security\/set-authenticodesignature?view=powershell-7.2\" data-type=\"URL\" data-id=\"https:\/\/learn.microsoft.com\/en-us\/powershell\/module\/microsoft.powershell.security\/set-authenticodesignature?view=powershell-7.2\" target=\"_blank\">documentation<\/a>. <\/p>\n\n\n\n<p>If you want to exclude any folder and files from signing, you can define like this <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;string&#91;]]$Exclude = @('ExcudeFolderName')\n\nGet-ChildItem -recurse -path '__DEPLOY__\\Program\\' -Include *.exe, *.dll | Where-Object  { $_.DirectoryName -notmatch $Exclude } | ForEach-Object<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>cd src\n$paths = Get-ChildItem -include *.csproj -Recurse\nforeach($pathobject in $paths)\n{\n     cd $pathobject.directory.fullName\n     dotnet publish -o ..\\..\\__DEPLOY__\\Program\n}\ncd ..\\..\n\n&#91;string&#91;]]$Exclude = @('ExcudeFolderName')\n\n$cert = Get-ChildItem -Path Cert:\\CurrentUser\\My -CodeSigningCert\n\nGet-ChildItem -recurse -path '__DEPLOY__\\Program\\' -Include *.exe, *.dll | Where-Object  { $_.DirectoryName -notmatch $Exclude } | ForEach-Object {\t\n\t$signingParameters = @{\n\t\tFilePath      =  $_.FullName\n\t\tCertificate   = $cert\n\t\tHashAlgorithm = 'SHA256'\n\t\tTimestampServer = 'http:\/\/timestamp.digicert.com'\n\t}\n\tSet-AuthenticodeSignature @signingParameters\n\t\n}<\/code><\/pre>\n\n\n\n<p>Above snippet is the entire script. <\/p>\n\n\n\n<p>Happy coding \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Code signing is important before releasing our software to the client. In the previous post, we use a PowerShell script to publish our code. After publishing the code, we need to sign the code before release. We must have code cert for signing. Install the code sign in your machine. Certificate installs process is simple [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[23],"tags":[],"class_list":["post-151","post","type-post","status-publish","format-standard","hentry","category-powershell"],"acf":[],"_links":{"self":[{"href":"https:\/\/aabirtech.com\/shiblenomanblog\/wp-json\/wp\/v2\/posts\/151","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aabirtech.com\/shiblenomanblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aabirtech.com\/shiblenomanblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aabirtech.com\/shiblenomanblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/aabirtech.com\/shiblenomanblog\/wp-json\/wp\/v2\/comments?post=151"}],"version-history":[{"count":2,"href":"https:\/\/aabirtech.com\/shiblenomanblog\/wp-json\/wp\/v2\/posts\/151\/revisions"}],"predecessor-version":[{"id":154,"href":"https:\/\/aabirtech.com\/shiblenomanblog\/wp-json\/wp\/v2\/posts\/151\/revisions\/154"}],"wp:attachment":[{"href":"https:\/\/aabirtech.com\/shiblenomanblog\/wp-json\/wp\/v2\/media?parent=151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aabirtech.com\/shiblenomanblog\/wp-json\/wp\/v2\/categories?post=151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aabirtech.com\/shiblenomanblog\/wp-json\/wp\/v2\/tags?post=151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}