A while ago I added a command called ConvertTo-WPFGrid in my PSScriptTools module. I wanted something similar to Out-Gridview but done in a WPF form. The initial version was ok and got the job done but it always felt lacking. The new version of the command is greatly improved I think. I wanted to take a few minutes and show you a new shiny toy.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
Auto Size
The original version of the command let you specify dimensions of the final form. The current version will auto size the form based on the size of the datagrid control. For the most part I like this approach. Although the code I'm using may briefly display a blank form before everything is resized. And I no longer can have the form auto center on the screen. I have some issues open on these items in the Github repo and will eventually get around to addressing them.
Auto Refresh
The previous version had a timeout parameter which would automatically close the form after a user specified number of seconds. That feature remains. But I've taken it a step further. If you write a one line PowerShell expression that is piped to ConvertTo-WPFGrid, the function will parse out the command to build a scriptblock of all the code leading up to ConvertTo-WPFGrid. Now, if you use -Refresh and -Timeout, the data will be updated at that interval.
Get-VM | Select Name,State,CPUUsage,@{Name="MemAssignedMB";Expression = {$_.MemoryAssigned/1MB -as [int]}},@{Name="MemDemandMB";Expression={$_.MemoryDemand/1MB}},@{Name="Runtime";Expression= { "{0:dd\.hh\:mm\:ss}" -f $_.Uptime}} | ConvertTo-WPFGrid -Title "VM Status" -Refresh -Timeout 20
In order for this to work the command needs to be one long pipelined expression. It won't work with nested prompts or if you break the command at a | in the PowerShell ISE.
This offers up some terrific possibilities.
You could create a one-line script to build a small monitoring tool.
Get-Volume -CimSession (get-content c:\work\names.txt) | Where-Object {$_.DriveLetter -AND $_.DriveType -eq 'fixed'} | Select-object @{Name="Server";Expression={$_.PSComputername.toUpper()}},DriveLetter,HealthStatus,FileSystemLabel,@{Name="SizeGB";Expression={[int]($_.Size/1GB)}},@{Name="PctFree";Expression = {($_.sizeremaining/$_.size)*100}} | ConvertTo-WPFGrid -Title "DiskStatus" -Refresh -Timeout 60
Background Runspace
This is possible because the new form runs in a separate runspace which means your PowerShell prompt is no longer blocked. However this also means that your profile scripts aren't loaded so anything that you rely to get the display data won't be loaded. That's another issue I'll have to address. Related to that is that when you close the grid, the runspace remains. I have yet to find a technique to close the parent runspace when the form closes. If you display several forms you'll be left with runspaces.
The first runspace is almost always your primary PowerShell session. They sessions will be removed when you close PowerShell. Or you can use the Remove-Runspace command also in the latest version of the PSScriptTools module.
Try for Yourself
You can try all of this for yourself. Install the PSScriptTools module from the PowerShell Gallery. Be sure to read help for the new commands. Convertto-WPFGrid converts the entire incoming object so be sure to use Select-Object to specify what you want to see in the form.
Hello,
I would like to suggest 2 enhancements.
– import of user variables because you use scriptblock parameter
$InitialSessionState = [InitialSessionState]::CreateDefault()
if ($AddVariable) {
$AddVariable | foreach-object {
$var = $_.split(‘:’,2)
$var = if (@($var).count -eq 1) {
get-variable $_ -ErrorAction 0
} else {
get-variable $var[1] -Scope $var[0] -ErrorAction 0
}
if ($var) {
$varEntry = New-Object System.Management.Automation.Runspaces.SessionStateVariableEntry(
$var.name, $var.value, $null)
$InitialSessionState.Variables.Add($varEntry)
}
}
}
$newRunspace = [RunspaceFactory]::CreateRunspace($Host, $InitialSessionState)
– grid scalability.
When I resize grid window the grid is not resized. If my table very large, window takes all the screen width and it is impossible to resized it without moving it. I believe it is not good idea to take all the screen width.
Did you test this using v2.3.0 of the PSScriptTools module? I made some changes that affect sizing and resizing that might address some of your issues. I’ll have to look at the variable issue.
I have used it.
As I resize window vertically the grid is not resized.
One more Q. How to disable in cell editing?
I’ll add these to the list.
Test out the revised function in v2.4.0 of the PSScriptTools module.
This solves a big problem, awesome module! I may try to integrate this into the AnyBox; very cool!
https://github.com/dm3ll3n/AnyBox