{"id":3931,"date":"2009-06-03T08:49:00","date_gmt":"2009-06-03T08:49:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2009\/06\/03\/peering-into-script-modules\/"},"modified":"2019-02-18T13:12:35","modified_gmt":"2019-02-18T20:12:35","slug":"peering-into-script-modules","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/peering-into-script-modules\/","title":{"rendered":"Peering into script modules"},"content":{"rendered":"<p>Sometimes it\u2019s handy to access internal elements of script modules. For instance you may be using a 3rd party module in your application and would like to see the internal state for debugging purposes.<\/p>\n<p>This can be accomplished with the invoke operator (&amp;) which allows you to access a modules session state:<\/p>\n<blockquote>\n<p>&amp; $module {script block}<\/p>\n<\/blockquote>\n<p>The script block is executed in the context of the module.<\/p>\n<p>Create a script module that exports nothing:<\/p>\n<div id=\"codeSnippetWrapper\">\n<div id=\"codeSnippetWrapper\">\n<pre id=\"codeSnippet\"><blockquote><p>[string] $foo = <span style=\"color: #006080\">&quot;IamFoo&quot;<\/span><br \/><br \/><span style=\"color: #0000ff\">function<\/span> Hidden($zarg) {<br \/>    <span style=\"color: #006080\">&quot;$zarg was passed to me&quot;<\/span><br \/>}<br \/><br \/>Export-ModuleMember -Function @()<br \/><\/p><\/blockquote><\/pre>\n<p><\/div>\n<\/div>\n<p>Save to a file \u2018NoExport.psm1\u2019, import the module and assign to a variable:<\/p>\n<div id=\"codeSnippetWrapper\">\n<pre id=\"codeSnippet\">PS&gt; $m = import-module .\\NoExport.psm1 -passthru<\/pre>\n<p>If we try to call function \u2018Hidden\u2019 we get an error because it was not exported, but we can call it with the operator &amp; .<\/div>\n<div>&#160;<\/div>\n<div id=\"codeSnippetWrapper\">\n<pre id=\"codeSnippet\">PS&gt;&amp; $m {Hidden <span style=\"color: #006080\">'bar'<\/span>}<br \/>bar was passed to me<\/pre>\n<p><\/div>\n<\/p>\n<p>Likewise, we can also retrieve and set the module variable $foo:<\/p>\n<div id=\"codeSnippetWrapper\">\n<pre id=\"codeSnippet\">PS&gt;&amp; $m {$foo}<br \/>IamFoo<br \/>PS&gt;&amp; $m {$<span style=\"color: #0000ff\">script<\/span>:foo = <span style=\"color: #006080\">'NewFoo'<\/span>}<br \/>PS&gt;&amp; $m {$foo}<br \/>NewFoo<\/pre>\n<p>Note that in order to set variable foo you must use script scope.<\/div>\n<p>&#160;<\/p>\n<p>Cheers, <\/p>\n<p>Nigel Sharples [MSFT]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes it\u2019s handy to access internal elements of script modules. For instance you may be using a 3rd party module in your application and would like to see the internal state for debugging purposes. This can be accomplished with the invoke operator (&amp;) which allows you to access a modules session state: &amp; $module {script [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[217,248],"class_list":["post-3931","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-modules","tag-powershell"],"acf":[],"blog_post_summary":"<p>Sometimes it\u2019s handy to access internal elements of script modules. For instance you may be using a 3rd party module in your application and would like to see the internal state for debugging purposes. This can be accomplished with the invoke operator (&amp;) which allows you to access a modules session state: &amp; $module {script [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/3931","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=3931"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/3931\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=3931"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=3931"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=3931"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}