Skip to content

Instantly share code, notes, and snippets.

@alx9r
alx9r / test.ps1
Last active May 12, 2020 15:40
Test "no mandatory" parameter binding complex case (PowerShell/PowerShell#11143)
function Where-Object2 {
[CmdletBinding(DefaultParameterSetName='None')]
param(
[Parameter(ValueFromPipeline=$true)]
[psobject]
${InputObject},
[Parameter(ParameterSetName='ScriptBlockSet', Mandatory=$true, Position=0)]
[scriptblock]
${FilterScript},
@alx9r
alx9r / test.ps1
Created May 7, 2020 16:46
ODBC DSN bitness, platform, process test (PowerShell/PowerShell#12578)
param
(
[Parameter(Mandatory,Position=1)]
[ValidateSet('CimMethod','Wdac','CimCmdletAdapter')]
$Method,
[Parameter(Mandatory,Position=2)]
[ValidateSet('64-bit','32-bit')]
$Platform
)
# Get
Invoke-CimMethod -ClassName MSFT_OdbcDsnTask `
-Namespace ROOT\Microsoft\Windows\Wdac `
-MethodName 'Get' `
-Arguments @{
Name = $Name
DsnType = [string]$DsnType
Platform = $Platform
DriverName = $DriverName
}
# Get
Invoke-CimMethod -ClassName MSFT_OdbcDsnTask `
-Namespace ROOT\Microsoft\Windows\Wdac `
-MethodName 'Get' `
-Arguments @{
Name = $Name
DsnType = [string]$DsnType
Platform = $Platform
DriverName = $DriverName
}
@alx9r
alx9r / host.cs
Last active May 4, 2020 22:09
PowerShell Progress Bar Host Shim (PowerShell/PowerShell#12541)
using System;
using System.Globalization;
using System.Management.Automation.Host;
namespace ProgressBarShim
{
public class Host : PSHost
{
private readonly PSHost PSHost;
private readonly HostUI HostUI;
@alx9r
alx9r / playback.ps1
Last active May 1, 2020 15:17
PowerShell progress stream capture
Import-CliXml .\progress.xml |
% {
Write-Progress -Activity $_.Activity `
-Status $_.StatusDescription `
-Id $_.ActivityId `
-CurrentOperation $_.CurrentOperation `
-ParentId $_.ParentActivityId `
-PercentComplete $_.PercentComplete `
-SecondsRemaining $_.SecondsRemaining `
-Completed:(@{
@alx9r
alx9r / test.ps1
Created April 26, 2020 20:20
ODBC DSN and wildcards, stackoverflow.com/questions/61398748
Get-OdbcDriver |
% {
$id = [guid]::NewGuid().Guid.Split('-')[0]
$name = $_.Name
try {
Invoke-CimMethod -ClassName MSFT_OdbcDsnTask `
-Namespace ROOT\Microsoft\Windows\Wdac `
-MethodName 'Add' `
-Arguments @{
Name = "test_$id"
@alx9r
alx9r / beforehand-afterward.ps1
Last active June 26, 2019 17:19
Demonstration of -Begin {} and -End {} vs. Pipeline Flow of Control of Beforehand and Afterward Utility Functions
function Beforehand {
param(
[Parameter(Mandatory,ValueFromPipeline)]
$InputObject,
[Parameter(Mandatory,Position=1)]
[scriptblock]
$ScriptBlock
)
begin { . $ScriptBlock }
@alx9r
alx9r / authorizationManagerContention.ps1
Created June 23, 2018 14:19
Demonstration of performance impact of sharing AuthorizationManager when opening runspaces in parallel.
# All this C# is to synthesize a signal that corresponds to when a runspace is
# ready to be used.
# see also PowerShell/PowerShell#7034
Add-Type '
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Threading.Tasks;
using System.Collections.Concurrent;
using System.Linq;
@alx9r
alx9r / useWhileParallelLoadingModule.ps1
Last active June 23, 2018 13:56
Demonstration of parallel loading of a module into PowerShell runspaces while performing CPU-bound execution using the module.
# All this C# is to synthesize a signal that corresponds to when a runspace is
# ready to be used.
# see also PowerShell/PowerShell#7034
Add-Type '
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Threading.Tasks;
using System.Collections.Concurrent;
using System.Linq;