[CmdletBinding()] param( [Parameter(Mandatory = $true)] [ValidateSet("NoExistingFunction", "ExistingFunction")] [string]$Mode, [Parameter(Mandatory = $true)][string]$SupportPath ) $ErrorActionPreference = "Stop" . $SupportPath if ($Mode -ceq "NoExistingFunction") { $before = Get-MinerOpsNativeGetItemState if ([bool]$before.FunctionExisted) { throw "Child process unexpectedly began with a global Get-Item function." } $observed = @(Invoke-MinerOpsNativeGetItemTrace -Action { Get-Item -LiteralPath $SupportPath }) if ($observed -cnotcontains $SupportPath) { throw "Trace did not observe Get-Item." } Assert-MinerOpsNativeGetItemState -Expected $before ` -Context "no-preexisting-function child test" return } $existing = { param($LiteralPath) "preexisting:$LiteralPath" } Set-Item -LiteralPath "Function:\global:Get-Item" -Value $existing -Force $before = Get-MinerOpsNativeGetItemState $observed = @(Invoke-MinerOpsNativeGetItemTrace -Action { Get-Item -LiteralPath $SupportPath }) if ($observed -cnotcontains $SupportPath) { throw "Trace did not observe Get-Item over an existing global function." } $after = Get-MinerOpsNativeGetItemState Assert-MinerOpsNativeGetItemState -Expected $before ` -Context "preexisting-function child test" if ($after.FunctionScriptBlock.ToString() -cne $existing.ToString()) { throw "Existing Get-Item ScriptBlock was not restored exactly." } if (-not [object]::ReferenceEquals( $after.FunctionScriptBlock, $before.FunctionScriptBlock )) { throw "Existing Get-Item ScriptBlock instance changed." }