Skip to content

Instantly share code, notes, and snippets.

@kevinblumenfeld
Created October 6, 2020 03:54
Show Gist options
  • Save kevinblumenfeld/8987fb8fce72532e62f583dd2e7e3234 to your computer and use it in GitHub Desktop.
Save kevinblumenfeld/8987fb8fce72532e62f583dd2e7e3234 to your computer and use it in GitHub Desktop.
#-------------------------------------------------------------#
#----Initial Declarations-------------------------------------#
#-------------------------------------------------------------#
Add-Type -AssemblyName PresentationCore, PresentationFramework
$Xaml = @"
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Width="800" Height="400">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="299*"/>
<RowDefinition Height="62*"/>
</Grid.RowDefinitions>
<DataGrid Grid.Row="0" Grid.Column="0" ItemsSource="{Binding source}" Name="kfxd4p3g8lvlz"/>
<Button Content="Get-Process" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="21.671875,24,0,0" Grid.Row="1" Grid.Column="0" Name="kfxd4p3hoiqgc"/>
</Grid></Window>
"@
#-------------------------------------------------------------#
#----Control Event Handlers-----------------------------------#
#-------------------------------------------------------------#
function getData {
Async {
$TDT = GPF
$State.source = $TDT
}
}
function GPF {
$GP = Get-Process | Select-Object -First 20
foreach ($P in $GP) {
[PSCustomObject]@{
Name = $P.name
Id = $P.id
Handle = $p.Handle
}
}
}
#endregion
#-------------------------------------------------------------#
#----Script Execution-----------------------------------------#
#-------------------------------------------------------------#
$Window = [Windows.Markup.XamlReader]::Parse($Xaml)
[xml]$xml = $Xaml
$xml.SelectNodes("//*[@Name]") | ForEach-Object { Set-Variable -Name $_.Name -Value $Window.FindName($_.Name) }
$kfxd4p3hoiqgc.Add_Click({getData $this $_})
$State = [PSCustomObject]@{}
Function Set-Binding {
Param($Target,$Property,$Index,$Name)
$Binding = New-Object System.Windows.Data.Binding
$Binding.Path = "["+$Index+"]"
$Binding.Mode = [System.Windows.Data.BindingMode]::TwoWay
[void]$Target.SetBinding($Property,$Binding)
}
function FillDataContext($props){
For ($i=0; $i -lt $props.Length; $i++) {
$prop = $props[$i]
$DataContext.Add($DataObject."$prop")
$getter = [scriptblock]::Create("return `$DataContext['$i']")
$setter = [scriptblock]::Create("param(`$val) return `$DataContext['$i']=`$val")
$State | Add-Member -Name $prop -MemberType ScriptProperty -Value $getter -SecondValue $setter
}
}
$DataObject = ConvertFrom-Json @"
{
"source" : []
}
"@
$DataContext = New-Object System.Collections.ObjectModel.ObservableCollection[Object]
FillDataContext @("source")
$Window.DataContext = $DataContext
Set-Binding -Target $kfxd4p3g8lvlz -Property $([System.Windows.Controls.DataGrid]::ItemsSourceProperty) -Index 0 -Name "source"
$Global:SyncHash = [HashTable]::Synchronized(@{})
$Jobs = [System.Collections.ArrayList]::Synchronized([System.Collections.ArrayList]::new())
$initialSessionState = [initialsessionstate]::CreateDefault()
Function Start-RunspaceTask
{
[CmdletBinding()]
Param([Parameter(Mandatory=$True,Position=0)][ScriptBlock]$ScriptBlock,
[Parameter(Mandatory=$True,Position=1)][PSObject[]]$ProxyVars)
$Runspace = [RunspaceFactory]::CreateRunspace($InitialSessionState)
$Runspace.ApartmentState = 'STA'
$Runspace.ThreadOptions = 'ReuseThread'
$Runspace.Open()
ForEach($Var in $ProxyVars){$Runspace.SessionStateProxy.SetVariable($Var.Name, $Var.Variable)}
$Thread = [PowerShell]::Create('NewRunspace')
$Thread.AddScript($ScriptBlock) | Out-Null
$Thread.Runspace = $Runspace
[Void]$Jobs.Add([PSObject]@{ PowerShell = $Thread ; Runspace = $Thread.BeginInvoke() })
}
$JobCleanupScript = {
Do
{
ForEach($Job in $Jobs)
{
If($Job.Runspace.IsCompleted)
{
[Void]$Job.Powershell.EndInvoke($Job.Runspace)
$Job.PowerShell.Runspace.Close()
$Job.PowerShell.Runspace.Dispose()
$Runspace.Powershell.Dispose()
$Jobs.Remove($Runspace)
}
}
Start-Sleep -Seconds 1
}
While ($SyncHash.CleanupJobs)
}
Get-ChildItem Function: | Where-Object {$_.name -notlike "*:*"} | select name -ExpandProperty name |
ForEach-Object {
$Definition = Get-Content "function:$_" -ErrorAction Stop
$SessionStateFunction = New-Object System.Management.Automation.Runspaces.SessionStateFunctionEntry -ArgumentList "$_", $Definition
$InitialSessionState.Commands.Add($SessionStateFunction)
}
$Window.Add_Closed({
Write-Verbose 'Halt runspace cleanup job processing'
$SyncHash.CleanupJobs = $False
})
$SyncHash.CleanupJobs = $True
function Async($scriptBlock){ Start-RunspaceTask $scriptBlock @([PSObject]@{ Name='DataContext' ; Variable=$DataContext},[PSObject]@{Name="State"; Variable=$State})}
Start-RunspaceTask $JobCleanupScript @([PSObject]@{ Name='Jobs' ; Variable=$Jobs })
$Window.ShowDialog()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment