Skip to content

Instantly share code, notes, and snippets.

@proxb
proxb / Struct.ps1
Created April 27, 2017 22:54
Using PowerShell and Reflection to dynamically build a Struct with Constructors and Methods
#region Module Builder
$Domain = [AppDomain]::CurrentDomain
$DynAssembly = New-Object System.Reflection.AssemblyName(([guid]::NewGuid().ToString()))
$AssemblyBuilder = $Domain.DefineDynamicAssembly($DynAssembly, [System.Reflection.Emit.AssemblyBuilderAccess]::Run) # Only run in memory
$ModuleBuilder = $AssemblyBuilder.DefineDynamicModule(([guid]::NewGuid().ToString()), $False)
#endregion Module Builder
#region STRUCTs
#Order of creating these Structs is important
#region MyStruct
$Attributes = 'AutoLayout, AnsiClass, Class, Public, SequentialLayout, Sealed, BeforeFieldInit'
@proxb
proxb / OutFromPSJobToConsole.ps1
Last active December 16, 2022 01:54
Sending data from PSJob to main PowerShell Console while job is still running
#Register the event subscription for the forwarded event
Register-EngineEvent -SourceIdentifier This.Nothing -Action {
Write-Host ("Date: {0}" -f $event.messagedata) -BackgroundColor Black -ForegroundColor Yellow
}
Start-Job -Name TestJob -ScriptBlock {
While ($True) {
Register-EngineEvent -SourceIdentifier This.Nothing -Forward
Start-Sleep -seconds 5
New-Event -SourceIdentifier This.Nothing -Message ("[{0}] From PSJob" -f (Get-Date))
Measure-Command {
$sessionstate = [system.management.automation.runspaces.initialsessionstate]::CreateDefault()
$runspacepool = [runspacefactory]::CreateRunspacePool(1, 10, $sessionstate, $Host)
$runspacepool.Open()
$ScriptBlock = {
Param ($File)
$tempHash = @{}
ForEach ($Item in $File) {
$RawFile = [activator]::CreateInstance([System.IO.StreamReader],@($Item, [System.Text.Encoding]::UTF8,$True,524288)).ReadToEnd()
@proxb
proxb / Get-WindowPosition.ps1
Last active November 20, 2015 15:24
Gets the X and Y position of a Windows given a process name as well as its Width and Height
<#
This will return the rectangle position of a process window.
Values of the Rectangle in relation to pixel location on window:
Left; // x position of upper-left corner
Top; // y position of upper-left corner
Right; // x position of lower-right corner
Bottom; // y position of lower-right corner
@proxb
proxb / SpeakingTopics.ps1
Created August 27, 2015 01:21
Some possible speaker topics for my session with Omaha PowerShell User Group in November
<#
10 tips for better PowerShell
-Various tips and tricks with using PowerShell
The Art of Runspaces in PowerShell
-Using runspaces for multithreading
Event Everything! (PowerShell events)
-Covering everything from WMI events to .Net events using PowerShell
@proxb
proxb / Example2Runspace.ps1
Created August 10, 2015 00:58
Example 2 showing synchronized collections
# Create an array of computers to do work against
$Computers = “computer01”,”computer02”,”computer03”,”computer04”,”computer05”
# Create an empty array that we'll use later
$RunspaceCollection = @()
# This is the array we want to ultimately add our information to
$qwinstaResults = [System.Collections.ArrayList]::Synchronized((New-Object System.Collections.ArrayList))
# Create a Runspace Pool with a minimum and maximum number of run spaces. (http://msdn.microsoft.com/en-us/library/windows/desktop/dd324626(v=vs.85).aspx)
@proxb
proxb / Example1Runspace.ps1
Created August 10, 2015 00:50
Example 1 of returning data back from a runspace
# Create an array of computers to do work against
$Computers = “computer01”,”computer02”,”computer03”,”computer04”,”computer05”
# Create an empty array that we'll use later
$RunspaceCollection = @()
# This is the array we want to ultimately add our information to
[Collections.Arraylist]$qwinstaResults = @()
# Create a Runspace Pool with a minimum and maximum number of run spaces. (http://msdn.microsoft.com/en-us/library/windows/desktop/dd324626(v=vs.85).aspx)
@proxb
proxb / synchash_winform
Created February 12, 2015 02:58
Example of not using synchronized hashtable vs. Script/Global scope for variables in forms
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$hash = [hashtable]::Synchronized(@{})
$hash.text = ""
$window = New-Object System.Windows.Forms.Form
$window.Width = 1000
$window.Height = 1000
$windowTextBox = New-Object System.Windows.Forms.TextBox
$windowTextBox.Location = New-Object System.Drawing.Size(10,10)
@proxb
proxb / ChainedAsync.ps1
Created May 29, 2014 14:55
Chained AddScripts()
$rs = [runspacefactory]::CreateRunspacePool(1,5)
$rs.Open()
$ps = [powershell]::Create()
$Ps.RunspacePool = $rs
[void]$ps.AddScript({
Write-Output 'Beginning Statement'
}).AddStatement().AddScript({
Param ($Name)
Get-Process -Name $Name
}).AddParameter('Name','PowerShell').AddStatement().AddScript({