{"id":3239,"date":"2021-01-22T17:31:21","date_gmt":"2021-01-22T16:31:21","guid":{"rendered":"https:\/\/www.mariotti.de\/?p=3239"},"modified":"2021-01-22T17:31:21","modified_gmt":"2021-01-22T16:31:21","slug":"simpler-powershell-host-zum-ausfuehren-von-powershell-skripten-ohne-stoerendes-konsolenfenster","status":"publish","type":"post","link":"https:\/\/dev.mariotti.de\/?p=3239","title":{"rendered":"Simpler PowerShell Host zum Ausf\u00fchren von PowerShell Skripten ohne st\u00f6rendes Konsolenfenster"},"content":{"rendered":"<p>Ich stand vor kurzem vor der Problematik, dass ich ein PowerShell Skript geschrieben habe, welches bei Endbenutzer w\u00e4hren der Windows Anmeldung ausgef\u00fchrt wurde, und ich wollte verhindert, dass der Endanwender das PowerShell Konsolenfenster zu Gesicht bekommt. Die PowerShell.exe selbst bietet hierf\u00fcr den &#8222;-WindowStyle Hidden&#8220; Parameter, aber dieser funktioniert nicht zu meiner Zufriedenheit, denn dieser l\u00e4sst trotzdem kurz das PowerShell Konsolenfenster erscheinen bevor er das Fenster ausblendet. Wer noch Windows Script Host kennt, wird sich sicher an die WScript.exe und die CScript.exe erinnern, bei der WScript.exe handelt es sich um eine Windows Script Host ohne Konsole, und genau diese Funktionalit\u00e4t habe ich mir f\u00fcr die Powershell.exe gew\u00fcnscht. Meine L\u00f6sung f\u00fcr diese Problematik bestand dann letztendlich darin, dass ich mir sozusagen eine WScript.exe f\u00fcr PowerShell selbst geschrieben habe, dies ist mit ganz wenigen Zeilen C# Code m\u00f6glich. Hier mein entsprechender Code.<\/p>\n<p>Ich habe den C# Code so gestaltet, dass es Inline im PowerShell Script enthalten ist und sich selbst kompiliert, im unteren Teil des Scriptes sieht man dann die beispielhafte Verwendung:<\/p>\n<pre><code class=\"language-powershell\"># Inline C# code for a simple PowerShell host and compile\n$code = @'\nusing System.Management.Automation;  \nusing System.Collections.ObjectModel;\nnamespace PowerShellHost {  \n  class Program {\n    static void Main(string[] args) {\n      if (args.Length == 0) return;\n      using (PowerShell ps = PowerShell.Create()) {\n        string code = System.IO.File.ReadAllText(args[0]);\n        ps.AddScript(code);\n        Collection &lt;psobject&gt; psobject = ps.Invoke();\n      }\n    }\n  }\n}\n'@\n# Compile\nAdd-Type -OutputAssembly \"$home\\Desktop\\PowerShellHost.exe\" -OutputType 'WindowsApplication'  -TypeDefinition $code\n\n\n# Generate an example PowerShell script\n\"Add-Type -AssemblyName 'Microsoft.VisualBasic' ; [Microsoft.VisualBasic.Interaction]::MsgBox('My MsgBox Text', 'YesNoCancel,Question', 'My Title')\" | Out-File \"$home\\Desktop\\Example.ps1\"\n\n# Example how run the example script via the PowerShell host \n&amp; \"$home\\Desktop\\PowerShellHost.exe\" \"$home\\Desktop\\Example.ps1\"\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Simpler PowerShell Host zum Ausf\u00fchren von PowerShell Skripten ohne st\u00f6rendes Konsolenfenster, sozusagen das PowerShell Gegenst\u00fcck zur WScript.exe des Windows Scripting Hosts<\/p>\n","protected":false},"author":2,"featured_media":2425,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[],"class_list":["post-3239","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"_links":{"self":[{"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=\/wp\/v2\/posts\/3239","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3239"}],"version-history":[{"count":0,"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=\/wp\/v2\/posts\/3239\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=\/wp\/v2\/media\/2425"}],"wp:attachment":[{"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3239"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}