Bookmark and Share 

 

Hey, Scripting Guy! QuestionHey, 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 clickthat is my goal.

-- MT

 

Hey, Scripting Guy! AnswerHello MT,

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 scripter@microsoft.com, 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 Rothenburg, Germany, several years ago when I was in Munich teaching a VBScript class. I had a schneeballen 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.

Photograph of the Rothenburg, Germany, town square

 

MT, use the ListBox project from yesterday’s Hey, Scripting Guy! post as a starting point, and you are halfway to having your script completed.

The first thing to do is to add an additional text box. Name the new text box tb_WMIOutput. In the Behavior section, make the text box accept multiple lines of output by setting the Multiline property to true. When the form loads, we do not want the WMI Output box to be visible; therefore, change the Visible property to false, as shown in the following image.

Image of changing Visible property to False


After you have set the Multiline property to true, you can resize the text box, and drag it out until it is as wide as the tb_Path box and nearly touches the bottom of the Windows Form. Under Appearance, make sure you change the Scrollbars property to vertical or you will not be able to scroll up and down to see all of your output.

Drag a new Button control to the Windows Form, and line it up with btn_Update, as shown in the following image.

Image of dragging new Button control to Windows Form

In the Design section, set the Visible property to false so that the button will not be present when the Windows Form loads. Name the button btn_WMI, as shown in the following image. Change the Text property of the button to Get WMI.

Image of naming the button

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 OnClick event handler for the btn_FillLb button. After the list box has been filled with the names of the computers from the text file, make the WMI button visible. To do this, set the Visible property of $btn_WMI to $true as seen here:

$btn_WMI.visible = $true

The complete event handler for the $btn_FillLB button uses code from yesterday’s Hey, Scripting Guy! article, and is seen here:

$btn_FillLb_OnClick=
{
#TODO: Place custom script here
 $computerNames = Get-Content -Path $tb_Path.Text
 $lb_Computers.BeginUpdate()
      foreach($computer in $computerNames)
        {
         $lb_Computers.Items.add($computer)
        }
 $lb_Computers.EndUpdate()
 $btn_WMI.visible = $true
}

You also need to add code to the OnClick event for the btn_WMI button. The first thing you need to do is to add the results of a WMI query to the Text property of the tb_WMIOutput text box. The WMI query uses the computer name from the selected computer from the lb_Computers list box. Because Windows PowerShell returns a Management Object, pipe the results to the Out-String cmdlet before assigning it to the Text property of the lb_Computers list box. This is shown here:  

$tb_WMIOutput.Text = Get-WmiObject -Class win32_computersystem `
   -ComputerName $lb_computers.SelectedItem | Out-String

The last thing that we need to do is to make the tb_WMIOutput text box visible. Do this by assigning $true to the Visible property as shown here:

$tb_WMIOutput.Visible = $true

The complete OnClick event handler is shown here:

$btn_WMI_OnClick=
{
#TODO: Place custom script here
 $tb_WMIOutput.Text = Get-WmiObject -Class win32_computersystem `
   -ComputerName $lb_computers.SelectedItem | Out-String
 $tb_WMIOutput.Visible = $true
}

When you run the script, the Path text box, the Computer Name list box, and the Update button are visible. Once you type the path to your text file and click Update, the List Computer list box is populated. The btn_WMI button then becomes visible, as shown here.

Image of Get WMI button

 

After you make your selection of a specific computer from the list box, clicking the btn_WMI button causes the WMI query to execute and the tb_WMIOutput box to appear with the results from running the WMI query. This is shown in the following image.

Image of WMI query results when Get WMI button is clicked


The complete listbox3.ps1 script is on the Scripting Guys Script Repository.

 

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.

If you want to know exactly what we will be looking at tomorrow, follow us on Twitter or Facebook. If you have any questions, send e-mail to us at scripter@microsoft.com or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

 

Ed Wilson and Craig Liebendorfer, Scripting Guys