А АWednesday 24 August 2022

Output data by Poweshell Studio in runtime.

Hi all.

If you newbee coder in Poweshell Studio you need simple method for output debug information in runtime. So I can present some variants for you.


1. You can easy test your form by sending output to Out-String and then Write-Host. For

example, in your form you could end up with a line like this:

<code to get data> | Out-String | Write-Host


2. So the next it's very popular method for output data in WinForms. It's Out-GridView, I wrote about it super commandlet early:

$entries = Get-EventLog -Computer $ComputerName.Text -Log $EventLogName.SelectedItem

$entries | Out-GridView


3. If you don’t want to create additional forms, there are several controls you can use within the form itself to display your results. You can use a Label control for a simple one-line result. Just set the Text property:

$labelDeviceID.Text=$data.deviceID

$labelFreespace.Text=$data.FreeGB

$labelSize.Text=$data.SizeGB

$labelVolume.Text=$data.Volumename

Sure, don't forget to set property $label.Visible = False for finishing, or remove all debug's labels from your Form before Build.


4. You could use a RichTextBox control. This control has some interesting visual properties you can experiment with. If you use this control, set the font to a fixed-width font like Consolas, especially if you want to display PowerShell output. Both of these controls expect strings, so you might need to reformat any PowerShell output by piping it to Out-String:

#clear any existing text box values

$RichTextBoxResults.Clear()

$data=<my command>

$RichTextBoxResults.Text=$data | Out-String


5. And the last option, although the most complicated of the bunch, is a DataGridView control. This is like what you get when you pipe results to Out-Gridview, except the table is in your form. You put data in the control via its DataSource property, which is an array of binding values. Fortunately, PowerShell Studio has a helper function called Update-DataGridView that makes it easier to populate it. Here’s a code snippet of what that might look like:

#clear the grid before

$datagridview1.ClearSelection()

#get data

$data=Get-WmiObject Win32_LogicalDisk -Filter "drivetype=3" -ComputerName $Computername.Text | Select "DeviceID",@{Name="SizeGB";Expression={"{0:N2}" -f ($_.Size/

1GB)}}, @{Name="FreeGB";Expression={"{0:N2}" -f ($_.Freespace/1GB)}},"VolumeName"

#add the data to the control

Update-DataGridView -DataGridView $datagridview1 -Item $data


See you later.

No comments:

Post a Comment

А что вы думаете по этому поводу?

Версия на печать

Популярное