Skip to content

Instantly share code, notes, and snippets.

@ConnerWill
Last active July 18, 2022 05:27
Show Gist options
  • Save ConnerWill/4c926542aef886be86b0a3bdcd17b15e to your computer and use it in GitHub Desktop.
Save ConnerWill/4c926542aef886be86b0a3bdcd17b15e to your computer and use it in GitHub Desktop.
powershell resources

PowerShell-templates-snipets.md

Create a new PSCustomObject

# Create a new PSCustomObject
$Result = [pscustomobject] @{
    Result1 = $Result1
    Result2 = $Result2
}

Return $Result

###################################################################################################

# Array with PSCustomObject
[System.Collections.ArrayList]$Results = @()

foreach($Item in $Items)
{
    $Result = [pscustomobject] @{
        Result1 = $Item.Result1
        Result2 = $Item.Result2
    }

    [void]$Results.Add($Result)
}

Return $Results

PowerShell Version Reference

Module Availability Reference

Reference material is organized into release versions. The content for each version is organized by module name, and a distinct folder for the about topics.

Over time, namespaces appeared as:

Module Name / PS Version 5.1 7.0 7.2 7.3
CimCmdlets X X X X
ISE (introduced in 2.0) X
Microsoft.PowerShell.Archive X X X X
Microsoft.PowerShell.Core X X X X
Microsoft.PowerShell.Diagnostics X X X X
Microsoft.PowerShell.Host X X X X
Microsoft.PowerShell.LocalAccounts X
Microsoft.PowerShell.Management X X X X
Microsoft.PowerShell.ODataUtils X
Microsoft.PowerShell.Operation.Validation X
Microsoft.PowerShell.Security X X X X
Microsoft.PowerShell.Utility X X X X
Microsoft.WsMan.Management X X X X
PackageManagement X X X X
PowershellGet X X X X
PSDesiredStateConfiguration X X X
PSDiagnostics X X X X
PSReadLine X X X X
PSScheduledJob X
PSWorkflow X
PSWorkflowUtility X
ThreadJob X X X

══════════════════════════════════════════════

powershellIcon

Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
###############################################################################################################
# Language : PowerShell 4.0
# Filename : RunspacePool.ps1
# Autor : BornToBeRoot (https://github.com/BornToBeRoot)
# Description : Template for RunspacePool to run code async
# Repository : https://github.com/BornToBeRoot/PowerShell
###############################################################################################################
<#
.SYNOPSIS
Template for RunspacePool to run code async
.DESCRIPTION
.EXAMPLE
.EXAMPLE
.LINK
https://github.com/BornToBeRoot/PowerShell
#>
[CmdletBinding()]
param(
# Number of concurrent threads --> depens on what code you are running / which hardware you are using
[Parameter(
Position=0,
HelpMessage='Maximum number of threads at the same time (Default=100)')]
[Int32]$Threads=100
)
Begin{
}
Process{
### Scriptblock (this code will run asynchron in the RunspacePool)
[System.Management.Automation.ScriptBlock]$ScriptBlock = {
Param(
### ScriptBlock Parameter
$Parameter1,
$Parameter2
)
#######################################
## Enter
## code
## here,
## which
## should
## run
## asynchron
#######################################
### Built custom PSObject and return it
[pscustomobject] @{
Parameter1 = Result1
Parameter2 = Result2
}
}
# Create RunspacePool and Jobs
Write-Verbose "Setting up RunspacePool..."
Write-Verbose "Running with max $Threads threads"
$RunspacePool = [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspacePool(1, $Threads, $Host)
$RunspacePool.Open()
[System.Collections.ArrayList]$Jobs = @()
Write-Verbose "Setting up Jobs..."
# Setting up jobs
for($i = $StartRange; $i -le $EndRange; $i++)
{
# Hashtable to pass parameters
$ScriptParams = @{
Parameter1 = $Parameter1
Parameter2 = $Parameter2
}
# Catch when trying to divide through zero
try {
$Progress_Percent = ($i / ($EndRange - $StartRange)) * 100 # Calulate some percent
}
catch {
$Progress_Percent = 100
}
Write-Progress -Activity "Setting up jobs..." -Id 1 -Status "Current Job: $i" -PercentComplete ($Progress_Percent)
# Create mew job
$Job = [System.Management.Automation.PowerShell]::Create().AddScript($ScriptBlock).AddParameters($ScriptParams)
$Job.RunspacePool = $RunspacePool
$JobObj = [pscustomobject] @{
RunNum = $i - $StartRange
Pipe = $Job
Result = $Job.BeginInvoke()
}
# Add job to collection
[void]$Jobs.Add($JobObj)
}
Write-Verbose "Waiting for jobs to complete & starting to process results..."
# Total jobs to calculate percent complete, because jobs are removed after they are processed
$Jobs_Total = $Jobs.Count
# Process results, while waiting for other jobs
Do {
# Get all jobs, which are completed
$Jobs_ToProcess = $Jobs | Where-Object {$_.Result.IsCompleted}
# If no jobs finished yet, wait 500 ms and try again
if($Jobs_ToProcess -eq $null)
{
Write-Verbose "No jobs completed, wait 500ms..."
Start-Sleep -Milliseconds 500
continue
}
# Get jobs, which are not complete yet
$Jobs_Remaining = ($Jobs | Where-Object {$_.Result.IsCompleted -eq $false}).Count
# Catch when trying to divide through zero
try {
$Progress_Percent = 100 - (($Jobs_Remaining / $Jobs_Total) * 100)
}
catch {
$Progress_Percent = 100
}
Write-Progress -Activity "Waiting for jobs to complete... ($($Threads - $($RunspacePool.GetAvailableRunspaces())) of $Threads threads running)" -Id 1 -PercentComplete $Progress_Percent -Status "$Jobs_Remaining remaining..."
Write-Verbose "Processing $(if($Jobs_ToProcess.Count -eq $null){"1"}else{$Jobs_ToProcess.Count}) job(s)..."
# Processing completed jobs
foreach($Job in $Jobs_ToProcess)
{
# Get the result...
$Job_Result = $Job.Pipe.EndInvoke($Job.Result)
$Job.Pipe.Dispose()
# Remove job from collection
$Jobs.Remove($Job)
# Check if result is null --> if not, return it
if($Job_Result -ne $null)
{
$Job_Result
}
}
} While ($Jobs.Count -gt 0)
Write-Verbose "Closing RunspacePool and free resources..."
# Close the RunspacePool and free resources
$RunspacePool.Close()
$RunspacePool.Dispose()
}
End{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment