{"id":3701,"date":"2009-07-22T13:47:32","date_gmt":"2009-07-22T13:47:32","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2009\/07\/22\/dynamic-binary-modules\/"},"modified":"2019-02-18T13:12:30","modified_gmt":"2019-02-18T20:12:30","slug":"dynamic-binary-modules","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/dynamic-binary-modules\/","title":{"rendered":"Dynamic binary modules"},"content":{"rendered":"<p>Using add-type and import-module you can dynamically compile and load an assembly without any intermediate assembly files to cleanup. <\/p>\n<p>For instance, to run a cmdlet on a remote machine you could send over the cmdlet source code, compile, import, and run it all on the fly.<\/p>\n<p>Let\u2019s say you have your cmdlet called Use-MyCmdlet as C# code in the file mycode.cs:<\/p>\n<div id=\"codeSnippetWrapper\">\n<pre id=\"codeSnippet\">PS&gt; [<span style=\"color: #0000ff\">string<\/span>] $code = Get-Content mycode.cs<br \/>PS&gt; $s = New-PSSession remoteHost<br \/>PS&gt; Invoke-Command $s `<br \/>    {(Add-Type -TypeDefinition $args[0] -PassThru).assembly | Import-Module} `<br \/>    -ArgumentList $code<br \/>PS&gt; Invoke-Command $s {Use-MyCmdlet}<br \/>PS&gt; Remove-PSSession $s<br \/><\/pre>\n<p><\/div>\n<div id=\"codeSnippetWrapper\"><\/div>\n<div id=\"codeSnippetWrapper\">This example gets the C# code as a string and starts a remote session.<\/div>\n<div>It then compiles the code in the remote session with Add-Type and passes the resulting assembly object to import-module.<\/div>\n<div>&#160;<\/div>\n<div>The cmdlet Use-MyCmdlet is invoked in the remote session which is then removed, with no need to remove any intermediate files on the remote system.\n  <\/div>\n<p>Nigel Sharples [MSFT]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using add-type and import-module you can dynamically compile and load an assembly without any intermediate assembly files to cleanup. For instance, to run a cmdlet on a remote machine you could send over the cmdlet source code, compile, import, and run it all on the fly. Let\u2019s say you have your cmdlet called Use-MyCmdlet as [&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":[],"class_list":["post-3701","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>Using add-type and import-module you can dynamically compile and load an assembly without any intermediate assembly files to cleanup. For instance, to run a cmdlet on a remote machine you could send over the cmdlet source code, compile, import, and run it all on the fly. Let\u2019s say you have your cmdlet called Use-MyCmdlet as [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/3701","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=3701"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/3701\/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=3701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=3701"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=3701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}