Skip to content

Instantly share code, notes, and snippets.

@ALIENQuake
Created September 9, 2018 16:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ALIENQuake/669ad419f985c52e2fa157efb7f40f8d to your computer and use it in GitHub Desktop.
Save ALIENQuake/669ad419f985c52e2fa157efb7f40f8d to your computer and use it in GitHub Desktop.
$script:SyncHashTable = [Hashtable]::Synchronized(@{ Exited = $false })
function DisplayLogSendInput {
$RunSpace = [RunspaceFactory]::CreateRunspace()
$RunSpace.ApartmentState = "STA"
$RunSpace.ThreadOptions = "ReuseThread"
$RunSpace.Open()
$RunSpace.SessionStateProxy.SetVariable("SyncHashTable", $script:SyncHashTable)
$PowerShellCmd = [PowerShell]::Create().AddScript({
[reflection.assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
[reflection.assembly]::LoadWithPartialName("System.Drawing") | Out-Null
$script:SyncHashTable.Button2_Click = {
# code below will hang application
'ping', 'ping' | % {
$script:SyncHashTable.Exited = $false
$initialSessionState = [InitialSessionState]::CreateDefault()
$RunSpace2 = [runspacefactory]::CreateRunspace($initialSessionState)
$RunSpace2.ApartmentState = "STA"
$RunSpace2.ThreadOptions = "ReuseThread"
$RunSpace2.Open()
$RunSpace2.SessionStateProxy.SetVariable("SyncHashTable", $script:SyncHashTable)
$PowerShellCmd2 = [Management.Automation.PowerShell]::Create().AddScript({
param ($ProcessName)
$process_OutputReceived = {
$script:SyncHashTable.TextBox1.Lines += $_.Data
$script:SyncHashTable.TextBox1.Select($script:SyncHashTable.TextBox1.Text.Length, 0)
$script:SyncHashTable.TextBox1.ScrollToCaret()
}
$process_Exited = {
Write-Host "Process has ended"
$script:SyncHashTable.Exited = $true
$script:SyncHashTable.TextBox1.Lines += "Process has ended"
$process.CancelOutputRead()
$process.Dispose()
}
$process = New-Object System.Diagnostics.Process
$script:SyncHashTable.process = $process
$process.EnableRaisingEvents = $true
$process.StartInfo.RedirectStandardError = $true
$process.StartInfo.RedirectStandardOutput = $true
$process.StartInfo.RedirectStandardInput = $true
$process.StartInfo.FileName = $processName
$process.StartInfo.UseShellExecute = $false
$process.StartInfo.CreateNoWindow = $true
$process.StartInfo.Arguments = '-n 3 google.com'
$process.add_OutputDataReceived($process_OutputReceived)
$process.add_Exited($process_Exited)
$process.SynchronizingObject = $script:SyncHashTable.Form1
$process.Start() | Out-Null
$process.BeginOutputReadLine()
}, $True)
$PowerShellCmd2.AddArgument($_)
$PowerShellCmd2.Runspace = $RunSpace2
$PowerShellCmd2.BeginInvoke()
Write-Host "Exited: $($script:SyncHashTable.Exited)"
# removing this loop unfreeze application but both process are executed at once
do {
Write-Host $script:SyncHashTable.Exited
sleep -Milliseconds 1000
} until ($script:SyncHashTable.Exited -eq $true)
SendToTextBox 'End loop'
}
}
#region Form Controls
$script:SyncHashTable.Button1_Click = {
#TODO: Place custom script here
$script:SyncHashTable.TextBox1.Text = $script:SyncHashTable.TextBox1.Text + 'Input was:' + $script:SyncHashTable.TextBox2.Text + "`n"
$script:SyncHashTable.process.StandardInput.WriteLine($script:SyncHashTable.TextBox2.Text)
$script:SyncHashTable.process.StandardInput.WriteLine("`n") # Simulate 'Enter' keypress
$script:SyncHashTable.TextBox1.Select($script:SyncHashTable.TextBox1.Text.Length, 0)
$script:SyncHashTable.TextBox1.ScrollToCaret()
}
$script:SyncHashTable.Form1 = New-Object System.Windows.Forms.Form
$script:SyncHashTable.TextBox1 = New-Object System.Windows.Forms.RichTextBox
$script:SyncHashTable.InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
$script:SyncHashTable.System_Drawing_Size = New-Object System.Drawing.Size
$script:SyncHashTable.System_Drawing_Size.Height = 320
$script:SyncHashTable.System_Drawing_Size.Width = 360
$script:SyncHashTable.Form1.ClientSize = $script:SyncHashTable.System_Drawing_Size
$script:SyncHashTable.Form1.DataBindings.DefaultDataSourceUpdateMode = 0
$script:SyncHashTable.Form1.Name = "Form1"
$script:SyncHashTable.Form1.Text = "Log Box"
$script:SyncHashTable.TextBox1.Anchor = 15
$script:SyncHashTable.TextBox1.DataBindings.DefaultDataSourceUpdateMode = 0
$script:SyncHashTable.System_Drawing_Point = New-Object System.Drawing.Point
$script:SyncHashTable.System_Drawing_Point.X = 12
$script:SyncHashTable.System_Drawing_Point.Y = 48
$script:SyncHashTable.TextBox1.Location = $script:SyncHashTable.System_Drawing_Point
$script:SyncHashTable.TextBox1.Name = "TextBox1"
$script:SyncHashTable.System_Drawing_Size = New-Object System.Drawing.Size
$script:SyncHashTable.System_Drawing_Size.Height = 200
$script:SyncHashTable.System_Drawing_Size.Width = 336
$script:SyncHashTable.TextBox1.Size = $script:SyncHashTable.System_Drawing_Size
$script:SyncHashTable.TextBox1.TabIndex = 0
$script:SyncHashTable.TextBox1.Text = ""
$script:SyncHashTable.TextBox2 = New-Object System.Windows.Forms.RichTextBox
$script:SyncHashTable.TextBox2.Anchor = 15
$script:SyncHashTable.TextBox2.DataBindings.DefaultDataSourceUpdateMode = 0
$script:SyncHashTable.System_Drawing_Point = New-Object System.Drawing.Point
$script:SyncHashTable.System_Drawing_Point.X = 12
$script:SyncHashTable.System_Drawing_Point.Y = 250
$script:SyncHashTable.TextBox2.Location = $script:SyncHashTable.System_Drawing_Point
$script:SyncHashTable.TextBox2.Name = "TextBox1"
$script:SyncHashTable.System_Drawing_Size = New-Object System.Drawing.Size
$script:SyncHashTable.System_Drawing_Size.Height = 24
$script:SyncHashTable.System_Drawing_Size.Width = 220
$script:SyncHashTable.TextBox2.Size = $script:SyncHashTable.System_Drawing_Size
$script:SyncHashTable.TextBox2.TabIndex = 0
$script:SyncHashTable.TextBox2.Text = ""
$script:SyncHashTable.Button1 = New-Object System.Windows.Forms.Button
$script:SyncHashTable.System_Drawing_Point = New-Object System.Drawing.Point
$script:SyncHashTable.System_Drawing_Point.X = 260
$script:SyncHashTable.System_Drawing_Point.Y = 250
$script:SyncHashTable.Button1.Location = $script:SyncHashTable.System_Drawing_Point
$script:SyncHashTable.Button1.Name = "Button1"
$script:SyncHashTable.Button1.Text = "SendInput"
$script:SyncHashTable.Button2 = New-Object System.Windows.Forms.Button
$script:SyncHashTable.System_Drawing_Point = New-Object System.Drawing.Point
$script:SyncHashTable.System_Drawing_Point.X = 12
$script:SyncHashTable.System_Drawing_Point.Y = 12
$script:SyncHashTable.Button2.Location = $script:SyncHashTable.System_Drawing_Point
$script:SyncHashTable.Button2.Name = "Button2"
$script:SyncHashTable.Button2.Text = "Start-OneByOneFromForm"
$script:SyncHashTable.Form1.Controls.Add($script:SyncHashTable.Button1)
$script:SyncHashTable.Form1.Controls.Add($script:SyncHashTable.Button2)
$script:SyncHashTable.Form1.Controls.Add($script:SyncHashTable.TextBox1)
$script:SyncHashTable.Form1.Controls.Add($script:SyncHashTable.TextBox2)
$script:SyncHashTable.Button1.Add_Click($script:SyncHashTable.Button1_Click)
$script:SyncHashTable.Button2.Add_Click($script:SyncHashTable.Button2_Click)
$script:SyncHashTable.InitialFormWindowState = $script:SyncHashTable.Form1.WindowState
$script:SyncHashTable.Form1.ShowDialog() | Out-Null
#endregion
})
$PowerShellCmd.Runspace = $RunSpace
$PowerShellCmd.BeginInvoke()
}
function SendToTextBox ($Message) {
$script:SyncHashTable.TextBox1.Text = $script:SyncHashTable.TextBox1.Text + $Message + "`n"
$script:SyncHashTable.TextBox1.Select($script:SyncHashTable.TextBox1.Text.Length, 0)
$script:SyncHashTable.TextBox1.ScrollToCaret()
}
DisplayLogSendInput | Out-Null
Start-Sleep 1
SendToTextBox 'Start loop'
# This code execues fine, one proces at the time
function Start-OneByOne ($ProcessName) {
$script:SyncHashTable.Exited = $false
$initialSessionState = [InitialSessionState]::CreateDefault()
$RunSpace2 = [runspacefactory]::CreateRunspace($initialSessionState)
$RunSpace2.ApartmentState = "STA"
$RunSpace2.ThreadOptions = "ReuseThread"
$RunSpace2.Open()
$RunSpace2.SessionStateProxy.SetVariable("SyncHashTable", $script:SyncHashTable)
$PowerShellCmd2 = [Management.Automation.PowerShell]::Create().AddScript({
param ($processName)
$process_OutputReceived = {
$script:SyncHashTable.TextBox1.Lines += $_.Data
$script:SyncHashTable.TextBox1.Select($script:SyncHashTable.TextBox1.Text.Length, 0)
$script:SyncHashTable.TextBox1.ScrollToCaret()
}
$process_Exited = {
Write-Host "Process has ended"
$script:SyncHashTable.Exited = $true
$script:SyncHashTable.TextBox1.Lines += "Process has ended"
$process.CancelOutputRead()
$process.Dispose()
}
$process = New-Object System.Diagnostics.Process
$script:SyncHashTable.process = $process
$process.EnableRaisingEvents = $true
$process.StartInfo.RedirectStandardError = $true
$process.StartInfo.RedirectStandardOutput = $true
$process.StartInfo.RedirectStandardInput = $true
$process.StartInfo.FileName = $processName
$process.StartInfo.UseShellExecute = $false
$process.StartInfo.CreateNoWindow = $false
$process.StartInfo.Arguments = '-n 3 google.com'
$process.add_OutputDataReceived($process_OutputReceived)
$process.add_Exited($process_Exited)
$process.SynchronizingObject = $script:SyncHashTable.Form1
$process.Start() | Out-Null
$process.BeginOutputReadLine()
})
$PowerShellCmd2.AddArgument($_)
$PowerShellCmd2.Runspace = $RunSpace2
$PowerShellCmd2.BeginInvoke()
Write-Host "Exited: $($script:SyncHashTable.Exited)"
do {
Write-Host $script:SyncHashTable.Exited
Start-Sleep -Milliseconds 1000
} until ($script:SyncHashTable.Exited -eq $true)
}
'ping', 'ping' | % {
Start-OneByOne -ProcessName $_
}
SendToTextBox 'End loop'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment