{"id":534,"date":"2013-03-19T23:06:37","date_gmt":"2013-03-19T22:06:37","guid":{"rendered":"http:\/\/sccmfaq.wordpress.com\/?p=534"},"modified":"2013-03-19T23:06:37","modified_gmt":"2013-03-19T22:06:37","slug":"sccm-2012-application-detection-with-powershell-and-code-signing","status":"publish","type":"post","link":"https:\/\/blog.hosebei.ch\/?p=534","title":{"rendered":"SCCM 2012 &#8211; Application detection with Powershell &#8230; and code-signing"},"content":{"rendered":"<p>With System Center 2012 Configuration Manager, we all know it, the new Application model was released. As long as you deploy MSI, the detection method, is very easy to define. But what, if you want to check a registry key, as needed with .Net installations? No Problem. What, if you want to check if the Installation was successfull with a VB-Script? It&#8217;s that easy (Look at this very good Blog Post about the Topic to deploy the App-V 5 Client: http:\/\/scug.be\/sccm\/category\/detection-methods). And with a Powershell script? Uhm&#8230; Yes surprisingly, the block of\u00a0scripts also applied to the detection method, and honestly, in my home environment, there is absolutely no doubt to decrease this behaviour below AllSigned, my home is my Castle \ud83d\ude09 I need to code-sign the script!<!--more--><\/p>\n<p>So i just made a simple application, it copies a file called eins.txt to c:\\temp\\test\\eins.txt, and the Powershell script then checks the existence of the file. But lets get first to some screenshots, and after that to the codes \ud83d\ude42<\/p>\n<p>First, you will need to create the Application, and a deployment type, this is how my deployment type is looking:<br \/>\n<a href=\"http:\/\/hosebei.wordpress.com\/wp-content\/uploads\/2013\/03\/detection-method01.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-539\" alt=\"Deployment Type\" src=\"http:\/\/hosebei.wordpress.com\/wp-content\/uploads\/2013\/03\/detection-method01.png?w=300\" width=\"300\" height=\"257\" srcset=\"https:\/\/blog.hosebei.ch\/wp-content\/uploads\/2013\/03\/detection-method01.png 647w, https:\/\/blog.hosebei.ch\/wp-content\/uploads\/2013\/03\/detection-method01-300x258.png 300w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a>The detection method is now configured with a Powershell script, without any code-signing:<br \/>\n<a href=\"http:\/\/hosebei.wordpress.com\/wp-content\/uploads\/2013\/03\/detection-method02.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-542\" alt=\"Powershell detection\" src=\"http:\/\/hosebei.wordpress.com\/wp-content\/uploads\/2013\/03\/detection-method02.png?w=300\" width=\"300\" height=\"140\" srcset=\"https:\/\/blog.hosebei.ch\/wp-content\/uploads\/2013\/03\/detection-method02.png 1178w, https:\/\/blog.hosebei.ch\/wp-content\/uploads\/2013\/03\/detection-method02-300x141.png 300w, https:\/\/blog.hosebei.ch\/wp-content\/uploads\/2013\/03\/detection-method02-1024x480.png 1024w, https:\/\/blog.hosebei.ch\/wp-content\/uploads\/2013\/03\/detection-method02-768x360.png 768w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a>Here is this simple code snippet:<br \/>\n<code>$TestFile = Get-ChildItem -Path \"C:\\temp\\test\\eins.txt\"<br \/>\n$TestFile.Exists<\/code><\/p>\n<p>If you deploy an application like this, and the Powershell Execution Policy is set to run only signed scripts, you will find the following error message in the AppDiscovery.log:<br \/>\n<a href=\"http:\/\/hosebei.wordpress.com\/wp-content\/uploads\/2013\/03\/detection-method03.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-549\" alt=\"AppDiscovery.log\" src=\"http:\/\/hosebei.wordpress.com\/wp-content\/uploads\/2013\/03\/detection-method03.png?w=300\" width=\"300\" height=\"130\" srcset=\"https:\/\/blog.hosebei.ch\/wp-content\/uploads\/2013\/03\/detection-method03.png 1191w, https:\/\/blog.hosebei.ch\/wp-content\/uploads\/2013\/03\/detection-method03-300x130.png 300w, https:\/\/blog.hosebei.ch\/wp-content\/uploads\/2013\/03\/detection-method03-1024x445.png 1024w, https:\/\/blog.hosebei.ch\/wp-content\/uploads\/2013\/03\/detection-method03-768x333.png 768w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><code>In-line script returned error output: &amp; : File C:\\Windows\\CCM\\System\\Temp\\49e933fa-9975-42dd-b248-36689fc53149.ps1<br \/>\ncannot be loaded. The file<br \/>\nC:\\Windows\\CCM\\System\\Temp\\49e933fa-9975-42dd-b248-36689fc53149.ps1 is not<br \/>\ndigitally signed. The script will not execute on the system. For more<br \/>\ninformation, see about_Execution_Policies at<br \/>\n<a href=\"http:\/\/go.microsoft.com\/fwlink\/?LinkID=135170\">http:\/\/go.microsoft.com\/fwlink\/?LinkID=135170<\/a>.<br \/>\nAt line:1 char:3<br \/>\n+ &amp; 'C:\\Windows\\CCM\\System\\Temp\\49e933fa-9975-42dd-b248-36689fc53149.ps1'<br \/>\n+\u00a0\u00a0 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br \/>\n+ CategoryInfo\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : SecurityError: (:) [], PSSecurityException<br \/>\n+ FullyQualifiedErrorId : UnauthorizedAccess<br \/>\n<\/code><br \/>\nSo what&#8217;s next? You&#8217;ve got to get a code-signing certificate, you can get it easily from your PKI, hopefully you got one.<\/p>\n<p>After this, you can sign your Powershell script with the following command:<br \/>\n<code>Set-AuthenticodeSignature c:\\temp\\test-file.ps1 @(Get-ChildItem cert:CurrentUserMy -codesigning)[0]<\/code><br \/>\nIf there are more than one code-signing capable certificates in the store, you have to use another term, in my case:<br \/>\n<code>Set-AuthenticodeSignature C:\\temp\\test-file.ps1 @(Get-ChildItem cert:CurrentUserMy1A7D1E2B6<br \/>\nD87D6B2DD06C2A59106FE91BE11D02E)[0]<\/code><\/p>\n<p>You should not receive any error messages. I used ISE to make and save the script on Windows 8. Be Aware, in earlier Versions of ISE, there was a Problem with signing script saved from ISE: http:\/\/connect.microsoft.com\/PowerShell\/feedback\/details\/483431\/set-authenticodesignature-fails-on-scripts-created-from-ise<br \/>\nThe Workaround was, to open the script from\u00a0ISE in Notepad, copy and paste it in a new Notepad document, then save, sign and use the new script.<\/p>\n<p>When the script is saved, you can Import it to the detection method:<br \/>\n<a href=\"http:\/\/hosebei.wordpress.com\/wp-content\/uploads\/2013\/03\/detection-method04.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-552\" alt=\"Import Powershell Script\" src=\"http:\/\/hosebei.wordpress.com\/wp-content\/uploads\/2013\/03\/detection-method04.png?w=300\" width=\"300\" height=\"296\" srcset=\"https:\/\/blog.hosebei.ch\/wp-content\/uploads\/2013\/03\/detection-method04.png 527w, https:\/\/blog.hosebei.ch\/wp-content\/uploads\/2013\/03\/detection-method04-300x296.png 300w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a>Navigate to your script and open it:<br \/>\n<a href=\"http:\/\/hosebei.wordpress.com\/wp-content\/uploads\/2013\/03\/detection-method05.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-553\" alt=\"import\" src=\"http:\/\/hosebei.wordpress.com\/wp-content\/uploads\/2013\/03\/detection-method05.png?w=300\" width=\"300\" height=\"294\" srcset=\"https:\/\/blog.hosebei.ch\/wp-content\/uploads\/2013\/03\/detection-method05.png 529w, https:\/\/blog.hosebei.ch\/wp-content\/uploads\/2013\/03\/detection-method05-300x294.png 300w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a>Now, the detection method is working, when your have imported the public key of the code-signing certificate to the Trusted Publisher store of the Destination Computer. And then, you will get your detection mehtod with powershell working:<br \/>\n<a href=\"http:\/\/hosebei.wordpress.com\/wp-content\/uploads\/2013\/03\/detection-method06.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-556\" alt=\"software center\" src=\"http:\/\/hosebei.wordpress.com\/wp-content\/uploads\/2013\/03\/detection-method06.png?w=300\" width=\"300\" height=\"251\" srcset=\"https:\/\/blog.hosebei.ch\/wp-content\/uploads\/2013\/03\/detection-method06.png 763w, https:\/\/blog.hosebei.ch\/wp-content\/uploads\/2013\/03\/detection-method06-300x251.png 300w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>\/Update:<br \/>\nThis will not work while on OSD \ud83d\ude41<br \/>\nSee this TechNet Post: <a href=\"http:\/\/social.technet.microsoft.com\/Forums\/en-US\/configmanagerosd\/thread\/59a35100-4772-4c99-a1d9-058ac77be1ba\">http:\/\/social.technet.microsoft.com\/Forums\/en-US\/configmanagerosd\/thread\/59a35100-4772-4c99-a1d9-058ac77be1ba<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>With System Center 2012 Configuration Manager, we all know it, the new Application model was released. As long as you deploy MSI, the detection method, is very easy to define. But what, if you want to check a registry key, as needed with .Net installations? No Problem. What, if you want to check if the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,40],"tags":[],"class_list":["post-534","post","type-post","status-publish","format-standard","hentry","category-application-management","category-tools"],"_links":{"self":[{"href":"https:\/\/blog.hosebei.ch\/index.php?rest_route=\/wp\/v2\/posts\/534","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.hosebei.ch\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.hosebei.ch\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.hosebei.ch\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.hosebei.ch\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=534"}],"version-history":[{"count":0,"href":"https:\/\/blog.hosebei.ch\/index.php?rest_route=\/wp\/v2\/posts\/534\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.hosebei.ch\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=534"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.hosebei.ch\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=534"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.hosebei.ch\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=534"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}