{"id":1276,"date":"2014-05-09T08:42:23","date_gmt":"2014-05-09T06:42:23","guid":{"rendered":"http:\/\/hen.co.za\/blog\/?p=1276"},"modified":"2014-05-09T08:42:23","modified_gmt":"2014-05-09T06:42:23","slug":"a-little-versioning-pain-with-powershell","status":"publish","type":"post","link":"https:\/\/hen.co.za\/blog\/2014\/05\/a-little-versioning-pain-with-powershell\/","title":{"rendered":"A little (versioning) pain with Powershell"},"content":{"rendered":"<p>First thing to mention is I&#8217;m not complaining about PowerShell itself or functionality provided with PowerShell. Rather, this is a problem with referencing it from .Net (C#) and versioning when used on another machine where the .Net App was deployed and happen to have an older version of PowerShell (PS for short).<\/p>\n<h2>Scenario<\/h2>\n<p>If you want to run PowerShell scripts from a C# program like I&#8217;m doing in <a title=\"QuickMon\" href=\"https:\/\/quickmon.codeplex.com\" target=\"_blank\">QuickMon<\/a> now, you have to add a reference to &#8220;<strong>System.Management.Automation.dll<\/strong>&#8220;. I happen to have Version 4.0 of PowerShell on the computer where the C# app is compiled. Now, this is all fun and games until you want to run the same app on another machine that only have, say Version 2.0 of PowerShell. PS 2 does have a version of System.Management.Automation.dll but somehow it is not compatible (at run time). When I compiled the same app on a computer that only has V2.0 of PowerShell and deploy that app to machines that has either PS v2.0 or v4.0 it works on all of them.<\/p>\n<p>If this was C++ it would have made sense (dll versioning very strict) but this is a C# app that supposively should not care about the version of the referenced assembly (and yes, this is a .Net assembly). The methods (I) used in the dll are all the same of both versions.<\/p>\n<p>I even tried generating the System.Management.Automation.dll file manually on the &#8216;target&#8217; machine and copy that dll to the App&#8217;s directory (using the PS &#8220;<em>Copy ([PSObject].Assembly.Location) C:\\<\/em>&#8221; command) but this gave other versioning referencing errors.<\/p>\n<h2>Possible solution<\/h2>\n<p>The solution that seems to work for now is to set the &#8216;<strong>Specific Version<\/strong>&#8216; property on the referenced dll in the C# project to true (making sure it points to the PS 2.0 related version &#8211; dll version 6.1.7601.17514). Interestingly enough when switching the project to &#8216;release&#8217; configuration it display pointing to &#8216;C:\\Windows\\assembly\\GAC_MSIL\\System.Management.Automation\\1.0.0.0__31bf3856ad364e35\\System.Management.Automation.dll&#8217;. That tells me the PS version &#8216;2.0&#8217; of that assembly is still available (on my PS 4.0 computer). As a safety precaution I also set the &#8216;Copy Local&#8217; property on this referenced dll to True to have it deployed with the app.<\/p>\n<p>Deploying this combination seems to work on all &#8216;target&#8217; machines &#8211; I even saw it working on a computer that only has PS 1.0!<\/p>\n<p>All fun and joy&#8230; as long as there&#8217;s no lawyer sitting on a loo in the bush&#8230; with an angry T-Rex nearby&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>First thing to mention is I&#8217;m not complaining about PowerShell itself or functionality provided with PowerShell. Rather, this is a problem with referencing it from .Net (C#) and versioning when used on another machine where the .Net App was deployed &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/hen.co.za\/blog\/2014\/05\/a-little-versioning-pain-with-powershell\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,13],"tags":[8,240,277],"class_list":["post-1276","post","type-post","status-publish","format-standard","hentry","category-development","category-thoughts","tag-c","tag-powershell","tag-system-management-automation"],"_links":{"self":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/1276","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/comments?post=1276"}],"version-history":[{"count":3,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/1276\/revisions"}],"predecessor-version":[{"id":1279,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/1276\/revisions\/1279"}],"wp:attachment":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/media?parent=1276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/categories?post=1276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/tags?post=1276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}