{"id":106,"date":"2011-01-28T17:09:28","date_gmt":"2011-01-29T00:09:28","guid":{"rendered":"http:\/\/ryan.witschger.net\/?p=106"},"modified":"2011-01-28T17:09:28","modified_gmt":"2011-01-29T00:09:28","slug":"106","status":"publish","type":"post","link":"http:\/\/www.Get-Blog.com\/?p=106","title":{"rendered":"Creating a Pop-up Message in Powershell"},"content":{"rendered":"<p>A co-worker of mine posed the question of how to get the same type of pop-up that you get when you are using a simple wscript.echo in VBS. Since I am always telling him that PowerShell is typically easier than VBS to do something I was a bit annoyed, because he found one case where VB is easier.<\/p>\n<p>Searching online you find that most people try to create a new object that is a VBS session and then pass the wscript.echo command over there. I don&#8217;t like that solution because it&#8217;s a bit kludgy. Instead I figured that there was a .NET object that you could use to do this, and indeed there is.<\/p>\n<p>Here is a link to the MSDN Article for System.Windows.Forms.MessageBox:<br \/>\n<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.windows.forms.messagebox.aspx\">System.Windows.Forms.Messagebox<\/a><\/p>\n<p>Alright, so we know that there is a .NET class, so we can just make a new object, right? Well, normally yes. But there is a bit of an annoying statement on that site, &#8220;You cannot create a new instance of the MessageBox class.&#8221; So that means that I have to call the methods with a static call. Here&#8217;s the basic gist<\/p>\n<div class=\"codecolorer-container powershell solarized-dark\" style=\"overflow:auto;white-space:nowrap;width:680px;\"><div class=\"powershell codecolorer\"><span class=\"br0\">&#91;<\/span><span class=\"re4\">System.Reflection.Assembly<\/span><span class=\"br0\">&#93;<\/span>::<span class=\"re5\">LoadWithPartialName<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;System.Windows.Forms&quot;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"br0\">&#91;<\/span>System.Windows.Forms.MessageBox<span class=\"br0\">&#93;<\/span>::Show<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Hello, World.&quot;<\/span><span class=\"br0\">&#41;<\/span><\/div><\/div>\n<p><a href=\"http:\/\/192.168.1.102\/wordpress\/wp-content\/uploads\/2011\/05\/HelloWorld_Popup.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-100;padding:1px;     border:1px solid #021a40;\" title=\"Hello World Popup\" src=\"http:\/\/192.168.1.102\/wordpress\/wp-content\/uploads\/2011\/05\/HelloWorld_Popup.jpg\" alt=\"Made with powershell and .NET object\" width=\"144\" height=\"144\" \/><\/a><\/p>\n<p>So that is the addition of the assembly to our current shell session and then calling it via a static method. As you can see it gives a really ugly window. We can add to that, though by overloading the show method with more stuff. Specifically, let&#8217;s fill out the title, the type of buttons we&#8217;ll use and the icon it will show.<\/p>\n<div class=\"codecolorer-container powershell solarized-dark\" style=\"overflow:auto;white-space:nowrap;width:680px;\"><div class=\"powershell codecolorer\"><span class=\"br0\">&#91;<\/span>System.Windows.Forms.MessageBox<span class=\"br0\">&#93;<\/span>::Show<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;This is the Text&quot;<\/span><span class=\"sy0\">,<\/span><span class=\"st0\">&quot;This is the title&quot;<\/span><span class=\"sy0\">,<\/span><span class=\"st0\">&quot;AbortRetryIgnore&quot;<\/span><span class=\"sy0\">,<\/span><span class=\"st0\">&quot;Warning&quot;<\/span><span class=\"br0\">&#41;<\/span><\/div><\/div>\n<p><a href=\"http:\/\/192.168.1.102\/wordpress\/wp-content\/uploads\/2011\/05\/AbortRetryIgnore_Popup.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-medium wp-image-109\" title=\"AbortRetryIgnore_Popup\" src=\"http:\/\/192.168.1.102\/wordpress\/wp-content\/uploads\/2011\/05\/AbortRetryIgnore_Popup-300x142.jpg\" alt=\"\" width=\"300\" height=\"142\" \/><\/a><\/p>\n<p>Alright, so how did I know what text to type to make those buttons and icon appear? What others are available to me? Well you can refer to Microsoft&#8217;s site to find out:<br \/>\n<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.windows.forms.messageboxbuttons.aspx\">Message Box Buttons<\/a><br \/>\n<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.windows.forms.messageboxicon.aspx\">Message Box Icons<\/a><\/p>\n<p>So, you can see that this can be fairly powerful, but not quite as &#8220;easy&#8221; as in VBS. Now, if you care about the input that the user gives, it&#8217;s really easy to get. All you need to do is grab the output.<\/p>\n<div class=\"codecolorer-container powershell solarized-dark\" style=\"overflow:auto;white-space:nowrap;width:680px;\"><div class=\"powershell codecolorer\"><span class=\"re0\">$Testing<\/span> <span class=\"sy0\">=<\/span> <span class=\"br0\">&#91;<\/span>System.Windows.Forms.MessageBox<span class=\"br0\">&#93;<\/span>::Show<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Testing Output&quot;<\/span><span class=\"sy0\">,<\/span><span class=\"st0\">&quot;&quot;<\/span><span class=\"sy0\">,<\/span><span class=\"st0\">&quot;AbortRetryIgnore&quot;<\/span><span class=\"sy0\">,<\/span><span class=\"st0\">&quot;Warning&quot;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"co1\">#Imaginary click of the &quot;Retry button&quot;<\/span><br \/>\n<span class=\"re0\">$Testing<\/span><br \/>\n<span class=\"sy0\">&gt;&gt;<\/span> Retry<\/div><\/div>\n<p>So you can see that we can work with the output now in other ways, but what if we want something really detailed, like we could get in VBS with the Inputbox command. Well, that&#8217;s a giant pain. I played with this for a while, but since there is no equivalent .NET object, it&#8217;s not very easy. Basically, you have to create a blank form then add all your objects to it. Set what all the buttons do and then set what happens when the form closes. I found a good tutorial written by Microsoft, so I won&#8217;t re-invent the egg:<br \/>\n<a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/ff730941.aspx\">Creating a Custom Input Box<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A co-worker of mine posed the question of how to get the same type of pop-up that you get when you are using a simple wscript.echo in VBS. Since I am always telling him that PowerShell is typically easier than VBS to do something I was a bit annoyed, becuase he found one case where VB is easier.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[],"_links":{"self":[{"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=\/wp\/v2\/posts\/106"}],"collection":[{"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=106"}],"version-history":[{"count":0,"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=\/wp\/v2\/posts\/106\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=106"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}