Skip to content

Instantly share code, notes, and snippets.

@jberezanski
Last active December 16, 2015 12:49
Show Gist options
  • Save jberezanski/1b7f40fa20b2364b64f6 to your computer and use it in GitHub Desktop.
Save jberezanski/1b7f40fa20b2364b64f6 to your computer and use it in GitHub Desktop.
workflow repro2
{
$ErrorActionPreference = 'Stop'
$dir = 'whatever'
InlineScript
{
$d = $using:dir
if ($d -eq $null) { throw "!!!!! BUG: d is null" } else { "d is $d" }
}
InlineScript
{
Get-Date
}
InlineScript
{
$d = $using:dir
if ($d -eq $null) { throw "!!!!! BUG: d is null" } else { "d is $d" }
}
}
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true)]
$targetComputer
)
workflow repro
{
Param (
[string] $EAP = 'Stop'
)
$ErrorActionPreference = $EAP
Write-Output "${Env:ComputerName} before InlineScript #1: dir is $dir"
InlineScript {
Write-Output "${Env:ComputerName} inside InlineScript #1: dir is $using:dir"
'Get-Date' | Set-Content C:\ExistingScript.ps1
}
$dir = 'C:\Program Files (x86)\Contoso App'
Write-Output $dir
Write-Output "${Env:ComputerName} before InlineScript #2: dir is $dir"
InlineScript {
Write-Output "${Env:ComputerName} inside InlineScript #2: dir is $using:dir"
if ($using:dir -eq $null) { throw "!!!!! BUG: dir is null" } else { Write-Output "----- ok: dir is not null" }
}
Write-Output "${Env:ComputerName} before InlineScript #3: dir is $dir"
InlineScript {
# referencing $using:dir prevents the bug
#Write-Output "inside InlineScript #3: dir is $using:dir"
# creating the file inside this InlineScript prevents the bug
#'Get-Date' | Set-Content C:\ExistingScript.ps1
# writing anything to output before external script invocation prevents the bug
#Write-Output 'dummy'
# executing a trivial statement with no output before external script invocation prevents the bug
#$a = 1
# variant 1: invoking an existing external script TRIGGERS the bug
& C:\ExistingScript.ps1
# variant 2: invoking a nonexistent external script TRIGGERS the bug (observable upon next workflow invocation)
#& C:\NonExistentScript.ps1
# variant 3: throwing an exception instead of invoking a script DOES NOT trigger the bug
#throw 'Boom!'
# writing anything to output after external script invocation prevents the bug
#Write-Output 'dummy'
# writing anything to verbose after external script invocation prevents the bug
#Write-Verbose 'bla'
# executing a trivial statement with no output after external script invocation prevents the bug
#$a = 1
}
Write-Output "${Env:ComputerName} before InlineScript #4: dir is $dir"
InlineScript {
Write-Output "${Env:ComputerName} inside InlineScript #4: dir is $using:dir"
if ($using:dir -eq $null) { throw "!!!!! BUG: dir is null" } else { Write-Output "----- ok: dir is not null" }
}
}
Write-Host "===== Restarting WinRM on $targetComputer ====="
icm $targetComputer { Restart-Service WinRM } -ErrorAction SilentlyContinue
Start-Sleep -Seconds 2
Write-Host '=========== Scenario 1: BUG if ErrorActionPreference is Stop ==========='
Write-Host '===== Step 1a ====='
repro -PSComputerName $targetComputer -EAP 'Stop'
Write-Host '===== Step 1b ====='
repro -PSComputerName $targetComputer -EAP 'Stop'
Write-Host "===== Restarting WinRM on $targetComputer ====="
icm $targetComputer { Restart-Service WinRM } -ErrorAction SilentlyContinue
Start-Sleep -Seconds 2
Write-Host '===== Step 1c ====='
repro -PSComputerName $targetComputer -EAP 'Stop'
Write-Host "===== Restarting WinRM on $targetComputer ====="
icm $targetComputer { Restart-Service WinRM } -ErrorAction SilentlyContinue
Start-Sleep -Seconds 2
Write-Host '=========== Scenario 2: ok if ErrorActionPreference is Continue ==========='
Write-Host '===== Step 2a ====='
repro -PSComputerName $targetComputer -EAP 'Continue'
Write-Host '===== Step 2b ====='
repro -PSComputerName $targetComputer -EAP 'Continue'
Write-Host "===== Cleaning up ====="
icm $targetComputer { if (Test-Path C:\ExistingScript.ps1) { Remove-Item C:\ExistingScript.ps1 } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment