{"id":3931,"date":"2013-03-30T00:01:00","date_gmt":"2013-03-30T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/03\/30\/weekend-scripter-run-c-code-from-within-powershell\/"},"modified":"2013-03-30T00:01:00","modified_gmt":"2013-03-30T00:01:00","slug":"weekend-scripter-run-c-code-from-within-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-run-c-code-from-within-powershell\/","title":{"rendered":"Weekend Scripter: Run C# Code from Within PowerShell"},"content":{"rendered":"<p><strong style=\"font-size: 12px\">Summary<\/strong><span style=\"font-size: 12px\">: Learn how to execute C# programs from source without a compiled binary by using Windows PowerShell.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Today guest blogger, Ingo Karstein, is back with us to share his knowledge. You can also read <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/ingo+karstein\/\" target=\"_blank\">previous guest blogs by Ingo<\/a>. Take it away Ingo&hellip;<\/p>\n<p>Some time ago, I created a script called PS2EXE that creates EXE files out of Windows PowerShell script files. It is posted on the Hey, Scripting Guy! Blog: <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/07\/22\/learn-about-two-codeplex-projects-ps2exe-and-robopowercopy.aspx\" target=\"_blank\">Learn About Two CodePlex Projects: PS2EXE and RoboPowerCopy<\/a>.<\/p>\n<p>Now I have created another script, rather the opposite of PS2EXE: <strong>C#Script<\/strong>. This script is able to execute C# programs from source code without a compiled binary.<\/p>\n<p>I&rsquo;ve done this because I have several C# tools that I always use for my daily business. Some of them are not easily convertible to Windows PowerShell. But I like the fact that script files are always readable because you only need the source code, no compiler. There is no need for binaries with separate source projects somewhere on the hard disk, so I decided to create a script with the purpose of running C# code inside Windows PowerShell.<\/p>\n<p>The idea is simple:<\/p>\n<ol>\n<li>Take a C# program file and compile it into the memory.<\/li>\n<li>Search for the <strong>Main<\/strong> method and call them using .NET reflection.<\/li>\n<li>Add some basic .NET console support to write output from the C# program to the Windows PowerShell environment.<\/li>\n<\/ol>\n<p>The C# program will be executed in a real .NET thread that is created in the Windows PowerShell script by using a helper class that is compiled in memory at runtime too. This helper class provides some synchronous .NET events that can be subscribed in Windows PowerShell to handle the console output.<\/p>\n<p style=\"padding-left: 30px\"><strong>Note<\/strong>&nbsp;&nbsp;&nbsp;You can download C#Script from the Microsoft TechNet Gallery: <a href=\"http:\/\/gallery.technet.microsoft.com\/C-Script-Execute-plain-a9eae961\" target=\"_blank\">C#Script: Execute source code C# programs from PowerShell<\/a>. You should be aware of the following limitations:<\/p>\n<ul>\n<li>This project is in the alpha state! There will be errors in it. So please be careful, especially in a production environment.<\/li>\n<li>Console input not implemented. Therefore, we need a custom class derived from System.IO.TextReader.<\/li>\n<li>There is no resource file support! It is just plain C#.<\/li>\n<\/ul>\n<p>To demonstrate the script, I created two C# demo projects: &ldquo;Test&rdquo; and &ldquo;TestWin.&rdquo; The first one is a console application, the second is a Windows Forms application.<\/p>\n<p>The following screenshot shows my &ldquo;TestWin&rdquo; demo project. My csscript.ps1 file is in the folder &ldquo;C:\\source2\\csscript,&rdquo; and &ldquo;testwin&rdquo; is in the subfolder.&nbsp;(The Windows 8 operating system is in German, but you get the point, I&rsquo;m sure.)&nbsp;<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3808.wes-3-30-13-1.png\"><img decoding=\"async\" title=\"Image of menu\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3808.wes-3-30-13-1.png\" alt=\"Image of menu\" \/><\/a><\/p>\n<p><span style=\"font-size: 12px\">Let&rsquo;s have a look into &ldquo;TestWin.&rdquo; The following screenshot is from the Visual Studio 2012 project.<\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7725.wes-3-30-13-2.png\"><img decoding=\"async\" title=\"Image of menu\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7725.wes-3-30-13-2.png\" alt=\"Image of menu\" \/><\/a><\/p>\n<p>Of course it can be run in Visual Studio 2012 or as standalone .NET assembly (EXE file). The list box is empty. It would show the program&#8217;s arguments if there were any.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2211.wes-3-30-13-3.png\"><img decoding=\"async\" title=\"Image of dialog box\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2211.wes-3-30-13-3.png\" alt=\"Image of dialog box\" \/><\/a><\/p>\n<p><span style=\"font-size: 12px\">At the beginning of the Program.cs file, there is an XML configuration section for C#Script:<\/span><\/p>\n<p style=\"padding-left: 30px\">\/\/&lt;csscript&gt;<\/p>\n<p style=\"padding-left: 30px\">\/\/&nbsp; &lt;nodebug\/&gt;<\/p>\n<p style=\"padding-left: 30px\">\/\/&nbsp; &lt;references&gt;<\/p>\n<p style=\"padding-left: 30px\">\/\/&nbsp;&nbsp;&nbsp; &lt;reference&gt;System&lt;\/reference&gt;<\/p>\n<p style=\"padding-left: 30px\">\/\/&nbsp;&nbsp;&nbsp; &lt;reference&gt;System.Core&lt;\/reference&gt;<\/p>\n<p style=\"padding-left: 30px\">\/\/&nbsp;&nbsp;&nbsp; &lt;reference&gt;System.Data&lt;\/reference&gt;<\/p>\n<p style=\"padding-left: 30px\">\/\/&nbsp;&nbsp;&nbsp; &lt;reference&gt;System.Data.DataSetExtensions&lt;\/reference&gt;<\/p>\n<p style=\"padding-left: 30px\">\/\/&nbsp;&nbsp;&nbsp; &lt;reference&gt;System.Xml&lt;\/reference&gt;<\/p>\n<p style=\"padding-left: 30px\">\/\/&nbsp;&nbsp;&nbsp; &lt;reference&gt;System.Xml.Linq&lt;\/reference&gt;<\/p>\n<p style=\"padding-left: 30px\">\/\/&nbsp;&nbsp;&nbsp; &lt;reference&gt;System.Windows.Forms&lt;\/reference&gt;<\/p>\n<p style=\"padding-left: 30px\">\/\/&nbsp;&nbsp;&nbsp; &lt;reference&gt;System.Drawing&lt;\/reference&gt;<\/p>\n<p style=\"padding-left: 30px\">\/\/&nbsp; &lt;\/references&gt;<\/p>\n<p style=\"padding-left: 30px\">\/\/&nbsp; &lt;mode&gt;winexe&lt;\/mode&gt;<\/p>\n<p style=\"padding-left: 30px\">\/\/&nbsp; &lt;files&gt;<\/p>\n<p style=\"padding-left: 30px\">\/\/&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;file&gt;Form1.cs&lt;\/file&gt;<\/p>\n<p style=\"padding-left: 30px\">\/\/&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;file&gt;Form1.Designer.cs&lt;\/file&gt;<\/p>\n<p style=\"padding-left: 30px\">\/\/\/\/&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;file&gt;Test&lt;\/file&gt;<\/p>\n<p style=\"padding-left: 30px\">\/\/&nbsp; &lt;\/files&gt;<\/p>\n<p style=\"padding-left: 30px\">\/\/&lt;\/csscript&gt;<\/p>\n<p>This rules the compilation of the program when using C#Script. Here, you specify the .NET assembly references, the execution mode, and the source files. By using four slash characters, the line will be ignored. In the &ldquo;files&rdquo; section, you specify all necessary C# files if there are more than one. The additional files do not&nbsp; need XML configuration.<\/p>\n<p>In my demo, the project needs three C# files to run: Program.cs, Form1.cs, and Form1.Designer.cs. The configuration XML is stored only at the beginning of Program.cs.&rdquo;&nbsp;<span style=\"font-size: 12px\">By using <\/span><strong style=\"font-size: 12px\">&lt;debug\/&gt;,<\/strong><span style=\"font-size: 12px\"> it&rsquo;s possible to debug the C# program file. I will show that later.<\/span><\/p>\n<p>Now let&rsquo;s go to the Windows PowerShell command line and use C#Script:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5241.wes-3-30-13-4.png\"><img decoding=\"async\" title=\"Image of command\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5241.wes-3-30-13-4.png\" alt=\"Image of command\" \/><\/a><\/p>\n<p>At the command line, I type:<\/p>\n<p style=\"padding-left: 30px\">.\\csscript.ps1 .\\testwin\\testwin\\Program.cs &#8220;Greetings&#8221; &#8220;from&#8221; &#8220;germany&#8221; &#8220;!&#8221;<\/p>\n<p>That&rsquo;s it.<\/p>\n<p>Let&rsquo;s have a look at the console application demo.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7271.wes-3-30-13-5.png\"><img decoding=\"async\" title=\"Image of menu\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7271.wes-3-30-13-5.png\" alt=\"Image of menu\" \/><\/a><\/p>\n<p>At the Windows PowerShell command line, I type:<\/p>\n<p style=\"padding-left: 30px\">.\\csscript.ps1 .\\test\\test\\Program.cs &#8220;Greetings&#8221; &#8220;from&#8221; &#8220;germany&#8221; &#8220;!&#8221;<\/p>\n<p>Here I use the <strong>&lt;debug\/&gt;<\/strong> configuration to be able to debug the program. This gives me the Debugger Attach dialog from Visual Studio&nbsp;2012 when I run the previous command-line statement:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3073.wes-3-30-13-6.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3073.wes-3-30-13-6.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>You will automatically get the source file of the C#Script internal helper class with hard coded breakpoints:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3750.wes-3-30-13-7.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3750.wes-3-30-13-7.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>Here you can see how it works inside: Internally it creates a thread and executes the original program by reflection. The <strong>Main<\/strong> method of the C# program is given in <strong>Method<\/strong> parameter, and the command-line arguments are in the <strong>prms<\/strong> parameter.<\/p>\n<p>The next hard coded breakpoint is specified in the Program.cs file. The file will have a new name. (Here the name is <strong>n_dspgoj.0.cs<\/strong>.)<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2625.wes-3-30-13-8.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2625.wes-3-30-13-8.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>This is the output in the console:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0601.wes-3-30-13-9.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0601.wes-3-30-13-9.png\" alt=\"Image of command output\" \/><\/a>&gt;<\/p>\n<p>In the C#Script package, I&rsquo;ve included a file named &ldquo;csscript.bat&rdquo; that helps you execute csscript from the traditional Windows shell:&nbsp;&nbsp;&nbsp;<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1205.wes-3-30-13-10.png\"><img decoding=\"async\" title=\"Image of script\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1205.wes-3-30-13-10.png\" alt=\"Image of script\" \/><\/a><\/p>\n<p>You can use it like this:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0317.wes-3-30-13-11.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0317.wes-3-30-13-11.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>I&rsquo;ve tested this C#Script with one of my favorite tools: the SharePoint Feature Administration and Clean Up Tool (it&rsquo;s a Windows Forms application).<\/p>\n<p><span style=\"font-size: 12px\">1. I downloaded the code from CodePlex: <\/span><a style=\"font-size: 12px\" href=\"http:\/\/featureadmin.codeplex.com\/SourceControl\/changeset\/view\/78751\" target=\"_blank\">SharePoint Feature Administration and Clean Up Tool<\/a><br \/><span style=\"font-size: 12px\">2. Unzip to a folder.<\/span><br \/><span style=\"font-size: 12px\">3. Create a batch file &ldquo;run.bat.&rdquo;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: 12px\">@echo off<\/span><\/p>\n<p style=\"padding-left: 30px\">call csscript.bat &#8220;FeatureAdmin2013-VisualStudio2012\\Program.cs&#8221;<\/p>\n<p>&nbsp;4.&nbsp;<span style=\"font-size: 12px\">Copy &ldquo;csscript.ps1&rdquo; and &ldquo;csscript.bat&rdquo; into the folder.<\/span><\/p>\n<p>&nbsp;<a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8228.wes-3-30-13-12.png\"><img decoding=\"async\" title=\"Image of folder\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8228.wes-3-30-13-12.png\" alt=\"Image of folder\" \/><\/a><\/p>\n<p><span style=\"font-size: 12px\">5. Modify the file &ldquo;Program.cs&rdquo; to contain the config XML structure.<\/span><\/p>\n<p>&nbsp;<a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6371.wes-3-30-13-13.png\"><img decoding=\"async\" title=\"Image of script\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6371.wes-3-30-13-13.png\" alt=\"Image of script\" \/><\/a><\/p>\n<p style=\"padding-left: 30px\"><strong>Note<\/strong>&nbsp;&nbsp;&nbsp;&ldquo;requiredframework&rdquo; and &ldquo;requiredplatform&rdquo; are set because SharePoint 2013 needs .NET Framework&nbsp;4.0 and 64-bit processes.<\/p>\n<p><span style=\"font-size: 12px\">6. Execute the program with &ldquo;run.bat&rdquo; without compilation in VS2012.<\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0310.wes-3-30-13-14.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0310.wes-3-30-13-14.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>For this program, I&rsquo;ll not need a compiled EXE anymore! Now I&rsquo;d like to get your response! If you found any errors, please report them on the TechNet Gallery page: <a href=\"http:\/\/gallery.technet.microsoft.com\/C-Script-Execute-plain-a9eae961\" target=\"_blank\">C#Script: Execute source code C# programs from PowerShell<\/a>. Please feel free to modify C#Script and send me your changes.<\/p>\n<p>~Ingo<\/p>\n<p>Ingo, thank you for sharing this with us today. I love it.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to execute C# programs from source without a compiled binary by using Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, is here. Today guest blogger, Ingo Karstein, is back with us to share his knowledge. You can also read previous guest blogs by Ingo. Take it away Ingo&hellip; Some time ago, I created [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[56,274,3,61,45],"class_list":["post-3931","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-guest-blogger","tag-ingo-karstein","tag-scripting-guy","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to execute C# programs from source without a compiled binary by using Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, is here. Today guest blogger, Ingo Karstein, is back with us to share his knowledge. You can also read previous guest blogs by Ingo. Take it away Ingo&hellip; Some time ago, I created [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3931","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=3931"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3931\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=3931"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=3931"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=3931"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}