{"id":1628,"date":"2009-09-02T14:57:44","date_gmt":"2009-09-02T22:57:44","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/?p=1628"},"modified":"2009-09-02T14:58:30","modified_gmt":"2009-09-02T22:58:30","slug":"powershell-hosting-and-myinvocation","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2009\/09\/02\/powershell-hosting-and-myinvocation\/","title":{"rendered":"PowerShell hosting and $MyInvocation"},"content":{"rendered":"<p>Many PowerShell users utilize code as shown below to determine where the running script resides:<\/p>\n<p>function get-scriptdirectory{<br \/>&nbsp; $invocation=(get-variable MyInvocation -Scope 1).Value<br \/>&nbsp; Split-Path $invocation.myinvocation.path<br \/>}<\/p>\n<p>Unfortunately, if the script runs inside a custom host, such as PrimalScript&#8217;s internal host, for a good number of debuggers and the SAPIEN PowerShell host from the PrimalForms packager, the MyInvocation variable contains an empty path member.<\/p>\n<p>There is no documented way to set MyInvocation from a custom host and any attempts contacting Microsoft in this matter only resulted in confirmation that any attempt to do so would be overwritten anyway.<\/p>\n<p>It seems unreasonable to expect the PowerShell team to change the API anytime soon. And even then, changing the current behavior might prove undesirable since it could potentially break existing scripts.<\/p>\n<p>That of course leaves everyone implementing a custom host with a dilemma. The SAPIEN PowerShell hosts now all contain a variable called $HostInvocation which has the same interface as MyInvocation and provides the desired information.<\/p>\n<p>So if you are running your script inside any SAPIEN PowerShell host you can use code like this to determine where your script is:<\/p>\n<p>function get-scriptdirectory{<br \/>&nbsp; if($hostinvocation -ne null) {<br \/>&nbsp;&nbsp;&nbsp; Split-Path $hostinvocation.MyCommand.path<br \/>&nbsp; }<br \/>&nbsp; else {<br \/>&nbsp;&nbsp;&nbsp; $invocation=(get-variable MyInvocation -Scope 1).Value<br \/>&nbsp;&nbsp;&nbsp; Split-Path $invocation.myinvocation.path<br \/>&nbsp; }<br \/>}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Many PowerShell users utilize code as shown below to determine where the running script resides: function get-scriptdirectory{&nbsp; $invocation=(get-variable MyInvocation -Scope 1).Value&nbsp; Split-Path $invocation.myinvocation.path} Unfortunately, if the script runs inside a custom host, such as PrimalScript&#8217;s internal host, for a good number of debuggers and the SAPIEN PowerShell host from the PrimalForms packager, the MyInvocation variable [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[283,25],"tags":[521,522,28],"class_list":["post-1628","post","type-post","status-publish","format-standard","hentry","category-howto","category-windows-powershell","tag-hosting","tag-myinvocation","tag-powershell"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/1628","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=1628"}],"version-history":[{"count":7,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/1628\/revisions"}],"predecessor-version":[{"id":1716,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/1628\/revisions\/1716"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=1628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=1628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=1628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}