This file contains hidden or 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
# add and import the Protect-Cleanup command | |
Add-Type ` | |
-Path .\protectCleanupCommand.cs ` | |
-PassThru | | |
% Assembly | | |
Import-Module | |
function DivideByZero { [CmdletBinding()]param() 1/0 } | |
Protect-Cleanup -ErrorStreamProtection ActionPreference { |
This file contains hidden or 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
using System.Management.Automation; | |
using System.Collections.Generic; | |
public static class workaround { | |
public static void Clean (ScriptBlock clean) { | |
var errorActionPreference = new PSVariable("ErrorActionPreference","SilentlyContinue"); | |
ScriptBlock.Create(@" | |
param($clean) | |
try { | |
. $clean 1 |
This file contains hidden or 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
# define a type with the following features: | |
# - can wait for a signal to complete contruction | |
# - records constructed and disposed flags such that it is beyond object lifetime | |
Add-Type @' | |
public class WaitingDisposableMock : System.IDisposable { | |
// global statics for tracking object lifetime | |
public static bool Disposed = false; | |
public static bool Constructed = false; | |
public WaitingDisposableMock(System.Threading.ManualResetEventSlim waitSignal) { | |
Disposed = false; |
This file contains hidden or 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
function getSessionState { | |
param($ScriptBlock) | |
[scriptblock]. | |
GetProperty('SessionStateInternal', | |
[System.Reflection.BindingFlags] 'NonPublic, Instance'). | |
GetValue($ScriptBlock) | |
} | |
foreach ($method in 'call' , | |
'steppable' ) { |
This file contains hidden or 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
using System; | |
using System.Management.Automation; | |
using System.Collections.Concurrent; | |
[Cmdlet(VerbsDiagnostic.Trace,"CmdletInternals")] | |
public class TraceCmdletInternalsCommand : Cmdlet, IDisposable { | |
public static ConcurrentQueue<string> Log { get; set; } | |
static TraceCmdletInternalsCommand() { | |
Log = new ConcurrentQueue<string>(); | |
} |
This file contains hidden or 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
using System.Management.Automation; | |
using System.Management.Automation.Host; | |
using Automation = System.Management.Automation; | |
using System.Management.Automation.Runspaces; | |
using System; | |
namespace Assuage.PowerShell { | |
/* This command combines New-Object and Set-Variable to achieve construction and assignment in a manner that cannot be interrupted by the runspace stopping. This is required to ensure reliable cleanup using the assigned-to variable in clean{} because otherwise it is possible for construction, but not, assignment can succeed. | |
See also: |
This file contains hidden or 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
using System; | |
using System.Management.Automation; | |
using System.Threading; | |
// Builder type for the arguments to AtomicAssignmentSourceMock. | |
// The builder pattern is necessary because the values for Job and PipelineStopping only become available in different threads at different time, and only after a reference to an arguments object is already required. | |
public class AtomicAssignmentSourceMockArgs { | |
// flag indicating whether the mock should behave as though this is a dry run | |
public bool DryRun = false; |
This file contains hidden or 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
# https://stackoverflow.com/a/79258729/1404637 | |
param( | |
[ValidateSet('user','admin')] | |
$privileges = 'user' | |
) | |
function Invoke-Test { | |
param( | |
[int] |
This file contains hidden or 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
# https://stackoverflow.com/a/79248980/1404637 | |
# https://gist.github.com/alx9r/d82232d53356dc5d10eaab1d83458d0e | |
$before = Get-Process pwsh | % Id | |
$job = Start-Job { | |
begin { 'begin' | Set-Content .\log.txt } | |
end { | |
Start-Sleep -Seconds 1000 | |
'end' | Add-Content .\log.txt | |
} |
This file contains hidden or 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
function Where-Object2 { | |
[CmdletBinding(DefaultParameterSetName='None')] | |
param( | |
[Parameter(ValueFromPipeline=$true)] | |
[psobject] | |
${InputObject}, | |
[Parameter(ParameterSetName='ScriptBlockSet', Mandatory=$true, Position=0)] | |
[scriptblock] | |
${FilterScript}, |
NewerOlder