Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.6k views
in Technique[技术] by (71.8m points)

powershell - ErrorActionPreference=stop stops all scripts stack

Consider the following:

// Calling-Script.ps1
Write-Output "Calling script"
./Failing-Script.ps1
Write-Output "Calling script again"

// Failing-Script.ps1
Write-Output "Failing script"
$ErrorActionPreference="stop"
Copy-Item invalid_file invalid_directory
Write-Output "Failing script again"

// output
Calling script
Failing script

Notice that Calling script again didn't show up. First I thought that somehow the change of ErrorActionPreference in Failing-Script.ps1 affected the one in Calling-Script.ps1, but I checked it and it doesn't:

// Calling-Script.ps1
Write-Output "Calling script"
$ErrorActionPreference
./Failing-Script.ps1
Write-Output "Calling script again"
$ErrorActionPreference

// Failing-Script.ps1
Write-Output "Failing script"
$ErrorActionPreference
$ErrorActionPreference="stop"
#Copy-Item invalid_file invalid_directory

Write-Output "Failing script again"
$ErrorActionPreference

// output
Calling script
Continue
Failing script
Continue
Failing script again
stop
Calling script again
Continue

The question is: Why is the execution of Calling-Script.ps1 stops? Does this make sense? is it documented?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...