{"id":51113,"date":"2010-03-04T00:01:00","date_gmt":"2010-03-04T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/03\/04\/hey-scripting-guy-can-i-create-a-windows-form-to-select-computer-names-and-run-wmi-queries\/"},"modified":"2010-03-04T00:01:00","modified_gmt":"2010-03-04T00:01:00","slug":"hey-scripting-guy-can-i-create-a-windows-form-to-select-computer-names-and-run-wmi-queries","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-can-i-create-a-windows-form-to-select-computer-names-and-run-wmi-queries\/","title":{"rendered":"Hey, Scripting Guy! Can I Create a Windows Form to Select Computer Names and Run WMI Queries?"},"content":{"rendered":"<p class=\"MsoNormal\"><a class=\"addthis_button\" href=\"http:\/\/www.addthis.com\/bookmark.php?v=250&amp;pub=scriptingguys\"><img decoding=\"async\" alt=\"Bookmark and Share\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\" width=\"125\" height=\"16\"><\/a>&nbsp;<\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p class=\"MsoNormal\"><img decoding=\"async\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\">Hey, Scripting Guy! I had this idea. I would like to have a Windows Form that had a list of computer names in it. I would then like to be able to select one of the computers, and run a WMI query against it. My idea is that I could give the Windows Form to the Help Desk, and I would not need to tell them how to run a script, give them access to Windows PowerShell, or have them attempt to edit a script. Point and click<span>&mdash;<\/span>that is my goal. <\/p>\n<\/p>\n<p class=\"MsoNormal\">&#8212; MT<\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<p class=\"MsoNormal\"><img decoding=\"async\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\">Hello MT, <\/p>\n<p class=\"MsoNormal\">Microsoft Scripting Guy Ed Wilson here. It has been a really nice day in Charlotte, North Carolina, today. I am sitting here, checking the e-mail sent to <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\"><font face=\"Segoe\">scripter@microsoft.com<\/font><\/a>, and watching my Twitter client go crazy. Because it is later in the evening, I am drinking a natural peppermint tea that has no caffeine in it. And my ANZAC biscuits, which are never far from my cup of tea, are just wishing to be consumed. I first had peppermint tea in <a href=\"http:\/\/en.wikipedia.org\/wiki\/Rothenburg_ob_der_Tauber\"><font face=\"Segoe\">Rothenburg, Germany<\/font><\/a>, several years ago when I was in Munich teaching a VBScript class. I had a <a href=\"http:\/\/en.wikipedia.org\/wiki\/Bavarian_cuisine\"><font face=\"Segoe\">schneeballen<\/font><\/a> with my tea due to the absence of ANZAC biscuits in Rothenburg. I took the following picture in the town square as we waited for the clock tower glockenspiel. <\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Photograph of the Rothenburg, Germany, town square\" alt=\"Photograph of the Rothenburg, Germany, town square\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0304\/hsg-03-04-10-01.jpg\" width=\"600\" height=\"450\"><\/p>\n<p class=\"Fig-Graphic\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">MT, use the <b>ListBox<\/b> project from <a href=\"http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2010\/03\/03\/hey-scripting-guy-march-3-2010.aspx\"><font face=\"Segoe\">yesterday&rsquo;s Hey, Scripting Guy! post<\/font><\/a> as a starting point, and you are halfway to having your script completed. <\/p>\n<p class=\"MsoNormal\">The first thing to do is to add an additional text box. Name the new text box <b>tb_WMIOutput<\/b>. In the <b>Behavior<\/b> section, make the text box accept multiple lines of output by setting the <b>Multiline<\/b> property to true. When the form loads, we do not want the WMI Output box to be visible; therefore, change the <b>Visible<\/b> property to false, as shown in the following image. <\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of changing Visible property to False\" alt=\"Image of changing Visible property to False\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0304\/hsg-03-04-10-02.jpg\" width=\"600\" height=\"418\"><\/p>\n<p class=\"Fig-Graphic\">After you have set the <b>Multiline<\/b> property to true, you can resize the text box, and drag it out until it is as wide as the <b>tb_Path<\/b> box and nearly touches the bottom of the Windows Form. Under Appearance, make sure you change the <b>Scrollbars<\/b> property to vertical or you will not be able to scroll up and down to see all of your output. <\/p>\n<p class=\"MsoNormal\">Drag a new <b>Button<\/b> control to the Windows Form, and line it up with <b>btn_Update<\/b>, as shown in the following image.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of dragging new Button control to Windows Form\" alt=\"Image of dragging new Button control to Windows Form\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0304\/hsg-03-04-10-03.jpg\" width=\"314\" height=\"233\"><\/p>\n<p class=\"Fig-Graphic\">In the <b>Design<\/b> section, set the <b>Visible<\/b> property to false so that the button will not be present when the Windows Form loads. Name the button <b>btn_WMI<\/b>, as shown in the following image. Change the Text property of the button to <b>Get WMI<\/b>. <\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of naming the button\" alt=\"Image of naming the button\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0304\/hsg-03-04-10-04.jpg\" width=\"218\" height=\"307\"><\/p>\n<p class=\"Fig-Graphic\">After you save your PrimalForms project and generate your Windows PowerShell code, open the Windows PowerShell code in a script editor. You will need to modify the <b>OnClick<\/b> event handler for the <b>btn_FillLb<\/b> button. After the list box has been filled with the names of the computers from the text file, make the <b>WMI<\/b> button visible. To do this, set the <b>Visible<\/b> property of <b>$btn_WMI<\/b> to <b>$true<\/b> as seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$btn_WMI.visible = $true<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">The complete ev\nent handler for the <b>$btn_FillLB<\/b> button uses code from <a href=\"http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2010\/03\/03\/hey-scripting-guy-march-3-2010.aspx\"><font face=\"Segoe\">yesterday&rsquo;s Hey, Scripting Guy! article<\/font><\/a>, and is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$btn_FillLb_OnClick= <br \/>{<br \/>#TODO: Place custom script here<br \/><span>&nbsp;<\/span>$computerNames = Get-Content -Path $tb_Path.Text<br \/><span>&nbsp;<\/span>$lb_Computers.BeginUpdate()<br \/><span>&nbsp;<\/span><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>foreach($computer in $computerNames)<br \/><span>&nbsp;<\/span><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp; <\/span>{<br \/><span>&nbsp;<\/span><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp;<\/span><span>&nbsp; <\/span>$lb_Computers.Items.add($computer)<br \/><span>&nbsp;<\/span><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp; <\/span>}<br \/><span>&nbsp;<\/span>$lb_Computers.EndUpdate()<br \/><span>&nbsp;<\/span>$btn_WMI.visible = $true<br \/>}<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">You also need to add code to the <b>OnClick<\/b> event for the <b>btn_WMI<\/b> button. The first thing you need to do is to add the results of a WMI query to the <b>Text<\/b> property of the <b>tb_WMIOutput<\/b> text box. The WMI query uses the computer name from the selected computer from the <b>lb_Computers<\/b> list box. Because Windows PowerShell returns a Management Object, pipe the results to the <b>Out-String<\/b> cmdlet before assigning it to the <b>Text<\/b> property of the <b>lb_Computers<\/b> list box. This is shown here:<span>&nbsp;&nbsp; <\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$tb_WMIOutput.Text = Get-WmiObject -Class win32_computersystem `<br \/><span>&nbsp;&nbsp; <\/span>-ComputerName $lb_computers.SelectedItem | Out-String<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">The last thing that we need to do is to make the <b>tb_WMIOutput<\/b> text box visible. Do this by assigning <b>$true<\/b> to the <b>Visible<\/b> property as shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$tb_WMIOutput.Visible = $true<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">The complete <b>OnClick<\/b> event handler is shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$btn_WMI_OnClick= <br \/>{<br \/>#TODO: Place custom script here<br \/><span>&nbsp;<\/span>$tb_WMIOutput.Text = Get-WmiObject -Class win32_computersystem `<br \/><span>&nbsp;&nbsp; <\/span>-ComputerName $lb_computers.SelectedItem | Out-String<br \/><span>&nbsp;<\/span>$tb_WMIOutput.Visible = $true<br \/>}<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">When you run the script, the <b>Path<\/b> text box, the <b>Computer Name<\/b> list box, and the <b>Update<\/b> button are visible. Once you type the path to your text file and click <b>Update<\/b>, the <b>List Computer<\/b> list box is populated. The <b>btn_WMI<\/b> button then becomes visible, as shown here.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of Get WMI button\" alt=\"Image of Get WMI button\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0304\/hsg-03-04-10-05.jpg\" width=\"300\" height=\"198\"><\/p>\n<p class=\"Fig-Graphic\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">After you make your selection of a specific computer from the list box, clicking the <b>btn_WMI<\/b> button causes the WMI query to execute and the <b>tb_WMIOutput<\/b> box to appear with the results from running the WMI query. This is shown in the following image.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of WMI query results when Get WMI button is clicked\" alt=\"Image of WMI query results when Get WMI button is clicked\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0304\/hsg-03-04-10-06.jpg\" width=\"300\" height=\"198\"><\/p>\n<p class=\"Fig-Graphic\">The complete listbox3.ps1 script is on the <a href=\"http:\/\/gallery.technet.microsoft.com\/ScriptCenter\/en-us\/7f9e7e32-8b98-412f-a1b4-0c25512670f1\"><font face=\"Segoe\">Scripting Guys Script Repository<\/font><\/a>. <\/p>\n<p class=\"Fig-Graphic\">\n<p>&nbsp;<\/p>\n<p class=\"MsoNormal\">MT, that is all there is to using a Windows Form with Windows PowerShell to select computer names and to run WMI queries. This concludes Graphical Windows PowerShell Week. Join us tomorrow for Quick-Hits Friday. <\/p>\n<p class=\"MsoNormal\">If you want to know exactly what we will be looking at tomorrow, follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\"><font face=\"Segoe\">Twitter<\/font><\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\"><font face=\"Segoe\">Facebook<\/font><\/a>. If you have any questions, send e-mail to us at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\"><font face=\"Segoe\">scripter@microsoft.com<\/font><\/a> or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\"><font face=\"Segoe\">Official Scripting Guys Forum<\/font><\/a>. See you tomorrow. Until then, peace.<\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<\/p>\n<p><b><span>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/span><\/b><\/p>\n<p><b><span><\/span><\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; &nbsp; Hey, Scripting Guy! I had this idea. I would like to have a Windows Form that had a list of computer names in it. I would then like to be able to select one of the computers, and run a WMI query against it. My idea is that I could give the Windows [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[71,3,4,45],"class_list":["post-51113","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-graphical","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; &nbsp; Hey, Scripting Guy! I had this idea. I would like to have a Windows Form that had a list of computer names in it. I would then like to be able to select one of the computers, and run a WMI query against it. My idea is that I could give the Windows [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51113","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\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=51113"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51113\/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=51113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=51113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=51113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}