Last active
July 18, 2022 05:27
-
-
Save ConnerWill/4c926542aef886be86b0a3bdcd17b15e to your computer and use it in GitHub Desktop.
powershell resources
PowerShell-templates-snipets.md
# 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
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 |
══════════════════════════════════════════════
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<!-- Generator: Adobe Illustrator 21.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) --> | |
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" | |
width="128px" height="128px" viewBox="0 0 128 128" enable-background="new 0 0 128 128" xml:space="preserve"> | |
<linearGradient id="SVGID_1_" gradientUnits="userSpaceOnUse" x1="95.1235" y1="91.8049" x2="26.855" y2="30.8249"> | |
<stop offset="0" style="stop-color:#5391FE"/> | |
<stop offset="1" style="stop-color:#3E6DBF"/> | |
</linearGradient> | |
<path fill-rule="evenodd" clip-rule="evenodd" fill="url(#SVGID_1_)" d="M9.491,109c-1.62,0-3.021-0.638-3.944-1.798 | |
c-0.944-1.185-1.268-2.814-0.889-4.47l17.834-77.911C23.239,21.557,26.376,19,29.634,19H118.5c1.62,0,3.021,0.638,3.945,1.798 | |
c0.944,1.184,1.268,2.814,0.889,4.47l-17.834,77.911c-0.747,3.264-3.884,5.822-7.143,5.822H9.491z"/> | |
<linearGradient id="SVGID_2_" gradientUnits="userSpaceOnUse" x1="26.1775" y1="30.0146" x2="93.7485" y2="90.7381"> | |
<stop offset="0" style="stop-color:#5391FE"/> | |
<stop offset="1" style="stop-color:#3E6DBF"/> | |
</linearGradient> | |
<path fill-rule="evenodd" clip-rule="evenodd" fill="url(#SVGID_2_)" d="M29.634,20H118.5c2.769,0,4.496,2.259,3.858,5.045 | |
l-17.834,77.911c-0.638,2.786-3.399,5.045-6.168,5.045H9.491c-2.769,0-4.496-2.258-3.858-5.045l17.834-77.911 | |
C24.104,22.259,26.866,20,29.634,20z"/> | |
<path fill-rule="evenodd" clip-rule="evenodd" fill="#2C5591" d="M64.165,87.558h21.613c2.513,0,4.55,2.125,4.55,4.746 | |
c0,2.621-2.037,4.747-4.55,4.747H64.165c-2.513,0-4.55-2.125-4.55-4.747C59.615,89.683,61.652,87.558,64.165,87.558z"/> | |
<path fill-rule="evenodd" clip-rule="evenodd" fill="#2C5591" d="M78.184,66.455c-0.372,0.749-1.144,1.575-2.509,2.534 | |
L35.562,97.798c-2.19,1.591-5.334,1.001-7.021-1.319c-1.687-2.32-1.28-5.49,0.91-7.082l36.173-26.194v-0.538L42.896,38.487 | |
c-1.854-1.972-1.661-5.161,0.431-7.124c2.092-1.962,5.29-1.954,7.144,0.018l27.271,29.012C79.29,62.04,79.405,64.534,78.184,66.455z | |
"/> | |
<path fill-rule="evenodd" clip-rule="evenodd" fill="#FFFFFF" d="M77.184,65.455c-0.372,0.749-1.144,1.575-2.509,2.534 | |
L34.562,96.798c-2.19,1.591-5.334,1.001-7.021-1.319c-1.687-2.32-1.28-5.49,0.91-7.082l36.173-26.194v-0.538L41.896,37.487 | |
c-1.854-1.972-1.661-5.161,0.431-7.124c2.092-1.962,5.29-1.954,7.144,0.018l27.271,29.012C78.29,61.04,78.405,63.534,77.184,65.455z | |
"/> | |
<path fill-rule="evenodd" clip-rule="evenodd" fill="#FFFFFF" d="M63.55,87h21.613c2.513,0,4.55,2.015,4.55,4.5 | |
c0,2.485-2.037,4.5-4.55,4.5H63.55C61.037,96,59,93.985,59,91.5C59,89.015,61.037,87,63.55,87z"/> | |
</svg> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################################################################### | |
# 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