Powershell Core
2 TopicsInstantiating a class inherited from Forms
I created a user input GUI PS script based on the example in the MS doc https://docs.microsoft.com/en-us/powershell/scripting/samples/creating-a-custom-input-box?view=powershell-7.2, which worked fine. I then created a class that inherits from Forms, based on the same setup / config in a module. My issue is that when I instantiate my custom user input class what is displayed appears to be the base Forms class without any of the components I configured. If I include the $forms.showDialog inside the class constructor, my class displays correctly. My goals is to end up with a set of modularized PS scripts where a primary PS script instantiates the user input class, and runs the required logic from the primary, calling other classes / scripts as needed. I greatly appreciate any guidance that points me in the right direction. User input class, created in moduel (psm1) using namespace System.Windows.Forms using namespace System.Drawing [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") Class InputWindow:Form{ #InputWindow() { $form = [Form]::new() $form.Size = [Size]::new(400, 415) $form.MaximizeBox = $false $form.StartPosition = "CenterScreen" $form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle # prevents screen sizing $form.Topmost = $true $form.text = "Profile Removal" #Label $toolStrpLabel = [System.Windows.Forms.ToolStripStatusLabel]::new() $toolStrpLabel.Size = [Size]::new(10,12) $toolStrpLabel.text ="Current Status" #StatusStrip $statusStrip = [System.Windows.Forms.StatusStrip]::new() $statusStrip.Size = [System.Drawing.size]::new(10, 12) $statusStrip.Dock = [System.Windows.Forms.DockStyle]::Bottom $statusStrip.SizingGrip = $false $statusStrip.Text = "Test" $statusStrip.Items.AddRange($toolStrpLabel) $form.Controls.Add($statusStrip) #Buttons $cancelButton = [System.Windows.Forms.Button]::new() $cancelButton.Margin = 5 $cancelButton.Anchor = 'right,bottom' $cancelButton.Location = [System.Drawing.Point]::new(190, 300 ) $cancelButton.Size = [System.Drawing.Size]::new(75,23) $cancelButton.Text = '&Cancel' $cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel $form.CancelButton = $cancelButton $form.AcceptButton = $cancelButton #Sets the default active button $form.Controls.Add($cancelButton) $okButton = [System.Windows.Forms.Button]::new() $okButton.Margin = 5 $okButton.Anchor = 'right, bottom' $okButton.Location = [System.Drawing.Point]::new(275,300) $okButton.Size = [System.Drawing.Size]::new(75,23) $okButton.Text = '&OK' $okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK $form.Controls.Add($okButton) #Message Banner $msgBanner = [System.Windows.Forms.Label]::new() $msgBanner.Location = [System.Drawing.Point]::new(10,20) $msgBanner.Size = [System.Drawing.Size]::new(300,20) $msgBanner.Text = 'Enter your MID information below:' $form.Controls.Add($msgBanner) #MID Label $midLabel = [System.Windows.Forms.Label]::new() $midLabel.Location = [System.Drawing.Point]::new(50,50) $midLabel.Size = [System.Drawing.Size]::new(35,15) $midLabel.Text = 'MID:' $form.Controls.Add($midLabel) #MID input box $midBox = [System.Windows.Forms.TextBox]::new() $midBox.Location = [System.Drawing.Point]::new(85,46) $midBox.Size = [System.Drawing.Size]::new(70,20) $midBox.Enabled = $true $midBox.TextAlign = "Left" $form.Controls.Add($midBox) #Password label $pswdLabel = [System.Windows.Forms.Label]::new() $pswdLabel.Location = [System.Drawing.Point]::new(190,50) $pswdLabel.Size = [System.Drawing.Size]::new(72,15) $pswdLabel.Text = 'Password:' $form.Controls.Add($pswdLabel) #Password input box $pswdBox = [System.Windows.Forms.TextBox]::new() $pswdBox.Location = [System.Drawing.Point]::new(260,46) $pswdBox.Size = [System.Drawing.Size]::new(70,20) $pswdBox.Enabled = $true $pswdBox.TextAlign = "Left" $form.Controls.Add($pswdBox) $form.Add_Shown({$midBox.Select()}) } # END of Constructor # } #END of Class InputWindow Function Get-InputWindow () { [InputWindow]::new() } Export-ModuleMember -Function Get-InputWindow1.4KViews0likes1CommentHow do I load System.Threading.Thread Assembly from GAC using partial name?
I tried everything, but I'm not able to get `[System.Threading.Thread]::CurrentThread`. I tried `Add-Type` and `[System.Reflection.Assembly]::Load(...)`, but to no avail. I do not want to use the workaround of writing a C# string and have that be compiled by `Add-Type`, I just want to add the assembly to my app domain to work with it. As I want it to run everywhere, I don't want to use the fully qualified assembly name. The partial name must suffice, so my script will be able to run everywhere (.NET Standard 1.0). Can someone please point me to the right solution? How can I load `System.Threading.Thread` into my PowerShell app domain, so I can finally use `CurrentThread`? Examples for both, Windows PowerShell and PowerShell Core, would be very appreciated.1.3KViews0likes0Comments