When I made the RoboGUI script, I will show in the next post in the robocopy series, I was experimenting with a Forms Library to make it easier to work with Forms in PowerShell, as The RoboGUI.ps1 uses it, I will post it here on my blog, It is not complete or under active development (and probably will not be anymore ) but it still might give you some ideas, hence I decided to post it outside of the series.
The goal was to make it easier, and more clear using forms code in PowerShell scripts, the library contains also a function the "read out" current controls making it possible to copy over existing settings, it is not used but I left it in for reference again.
This lib makes it possible to write code like this in a PowerShell script ;
$frmRobocopy = new-Object System.Windows.Forms.form set-controlFormat $frmRobocopy ` -Location '{X=46,Y=46}'` -Size '{Width=1223, Height=850}'` -Text 'RoboCopy PowerShell GUI'
you can see you can use parameters and hashTable format to describe control settings, what makes it look much nicer in code.
And the comple Library. (I will also post it on http://PoSHcode.org , but as you could see in last post I still have some problem embedding the code in my blog)
# FormsLib.ps1 # contains some helper functions to create and modify Form controls # in a PowerShell script used by RoboGUI.ps1 # # /\/\o\/\/ # http://thePowerShellGuy.com Function ConvertTo-HashTable ([string]$StringValue) { invoke-expression ("@$StringValue".replace(',',';')) } Function ConvertTo-Point ([string]$StringValue) { ConvertTo-HashTable $StringValue | % {New-Object System.Drawing.Point([int]($_.x),[int]($_.y))} } Function ConvertTo-Size ([string]$StringValue) { ConvertTo-HashTable $StringValue | % {New-Object System.Drawing.Size([int]($_.Width),[int]($_.Height))} } filter get-PropertyList { $o = $_ ; $_ | gm -MemberType Property | select name, @{Name='Type';Expression={$_.definition.split()[0]}}, @{Name='Value';Expression={$o."$($_.name)"}} } Function Get-ControlFormat { Param ( $Control, $properties = ('Text','Size','Location','Dock','Anchor'), $ExtraProperties ) $properties += $ExtraProperties "Set-ControlFormat `$$($Control.name) ``" $Control | get-PropertyList | Where {$Properties -contains $_.name} | Foreach { " -$($_.name) '$($_.Value)'``" } } Function Set-ControlFormat { Param ( $Control ) foreach ($arg in $args) { if ($arg.startswith('-')) { $Property = $arg.trim('-') [void] $foreach.MoveNext() Switch ($Property) { 'Location' { $Control.Location = ConvertTo-Point $foreach.current ; break} 'Size' { $Control.Size = ConvertTo-Size $foreach.current ; break} Default {$Control."$Property" = $foreach.current} } } } } Function get-FormControls ($psObject) { $form = new-object Windows.Forms.Form $form.Size = new-object Drawing.Size @(600,600) $controls = @("form") $psObject.Controls |% {$controls += $_.name} $CB = new-object Windows.Forms.Combobox $cb.Size = new-object Drawing.Size @(500,21) $cb.Items.AddRange($controls) $PG = new-object Windows.Forms.PropertyGrid $PG.Size = new-object Drawing.Size @(500,500) $PG.Location = New-Object System.Drawing.Point(50 , 50) $form.text = "$psObject" $PG.selectedobject = $psObject.PsObject.baseobject $cb.text = 'form' $cb.add_TextChanged({ if ( $this.SelectedItem -eq 'Form') { $PG.selectedobject = $psObject.PsObject.baseobject } Else { $PG.selectedobject = $psObject.Controls["$($this.SelectedItem)"].PsObject.baseobject } }) $form.Controls.Add($PG) $form.Controls.Add($CB) $Form.Add_Shown({$form.Activate()}) $form.showdialog() }
As indicated this was a test project I did about a year ago for testing this way of working, it should not be seen as best practice but as something I was test out at the time and might contain some ideas that you could use.
more examples of using this library in a script can be found in the script in the next blogpost about RoboGUI.PS1 , for the generating part (get-controlformat / get-formControls) your on your own, but note that you can retrieve extra properties also, and the Format of Set-Controlformat, is exactly the same as the Tostring() when you list Control properties from PowerShell ;-).
Enjoy,
Greetings /\/\o\/\/