Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Add-Type -AssemblyName System.Windows.Forms
- # Defining a synchronized hashtable allows us to add and retrieve objects in a 'thread-safe' manner.
- $ht = [hashtable]::Synchronized(@{})
- # Creates a runspace that is attached to a newly-created PowerShell instance.
- Function New-Runspace()
- {
- $rs = [runspacefactory]::CreateRunspace()
- $rs.ApartmentState = "STA"
- $rs.ThreadOptions = "ReuseThread"
- $rs.Open()
- $rs
- }
- # New-PowerShell creates a PowerShell instance within the specified runspace and runs the supplied ScriptBlock.
- Function New-PowerShell([scriptblock] $ScriptBlock, [runspace] $Runspace)
- {
- $ps = [powershell]::Create().AddScript($ScriptBlock)
- $ps.Runspace = $Runspace
- $ps.BeginInvoke()
- }
- # The main form that is displayed.
- $form = New-Object System.Windows.Forms.Form -Property @{
- Height = 150
- Width = 600
- }
- # This TextBox is where your 'systeminfo.exe' results will go.
- # As soon as the systeminfo text is added, this TextBox and the
- # Form will resize themselves to fit the content.
- $statusBox = New-Object System.Windows.Forms.TextBox -Property @{
- Height = 30
- Width = 580
- Location = "10, 50"
- MultiLine = $true
- ScrollBars = [System.Windows.Forms.ScrollBars]::Both
- WordWrap = $false
- Add_TextChanged = {
- $count = $statusBox.Lines.Length
- $possibleHeight = $count * $statusBox.Font.Height
- if ($possibleHeight -gt 800)
- {
- $possibleHeight = 800
- }
- $statusBox.Height = $possibleHeight
- $form.Height = $possibleHeight + 100
- }
- }
- $ht.Add("StatusBox", $statusBox) # Add $statusBox to the 'synchronized' hashtable so it can be called safely
- # from within a runspace.
- # This is the button that starts the 'systeminfo.exe' process.
- # Notice, because it will run the 'systeminfo.exe' process in a separate thread,
- # the GUI remains responsive. You can even click the "DummyButton" all day long while
- # the operation is running.
- $button = New-Object System.Windows.Forms.Button -Property @{
- Text = "Get System Info"
- Width = 150
- Location = "20, 10"
- Name = "MyButton"
- # This is the Click Event handler that calls 'systeminfo.exe' asynchronously.
- Add_Click = {
- $rs = New-Runspace
- $rs.SessionStateProxy.SetVariable("ht", $ht) # We add the synchronized hashtable to the InitialSessionState.
- # This allows us to reference $ht from within the runspace.
- # $code represents the scriptblock that calls 'systeminfo.exe'
- $code = {
- $message = systeminfo.exe # Run the systeminfo.exe like normal.
- # We do NOT pipe it to 'Out-String' as we want
- # to be able to retain and query the line count.
- $ht.StatusBox.Lines = $message # Add the entire message to the StatusBox's Lines property.
- # We are referencing it from the synchronized hashtable.
- }
- New-PowerShell -ScriptBlock $code -Runspace $rs
- }
- }
- # This is just a dummy button object that you can click to show
- # that the GUI remains responsive while 'systeminfo.exe' is running
- # in the background.
- $dummyButton = New-Object System.Windows.Forms.Button -Property @{
- Text = "Dummy Button"
- Width = 100
- Location = "180, 10"
- Name = "DummyButton"
- }
- $form.Controls.AddRange(@($button, $dummyButton, $statusBox))
- $form.ShowDialog()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement