1

Setting $ErrorActionPreference to Stop seems to be ignored by New-SmbShare in specific use cases:

  • Run within Windows ISE: No problem
  • Invoked using powershell.exe -File argument: No problem
  • Called with explicit -ErrorAction Stop: No problem
  • Run within PowerShell Prompt (dot-sourced): $ErrorActionPreference is ignored
  • Invoked using powershell.exe -Command argument: $ErrorActionPreference is ignored

This can be tested with the following script (test.ps1):

$ErrorActionPreference = "Stop" New-SmbShare -Name "Test" -Path "C:\NonExistingPath" Write-Host "Should not be reached" 

The following call shows the error "The system cannot find the file specified", but also shows "Should never be reached", which is incorrect:

powershell.exe -Command .\test.ps1 

The following call just throws the "The system cannot find the file specified" exception, which is correct:

powershell.exe -File .\test.ps1 

Tested with:

  • PowerShell 4 and 5
  • Windows Server 2012 R2

Am I missing something here, or is this a PowerShell bug? I've already raised a uservoice, but maybe you have an explanation?

2
  • different behaviors in different host's is a bit confusing but I have a feeling this may be a terminating/non-terminating error problem - blogs.technet.microsoft.com/heyscriptingguy/2015/09/16/… Commented Mar 13, 2017 at 16:11
  • @MikeGaruccio: Yes, but New-SmbShare (or the CIM-wrapper) seems to handle it wrong. $ErrorActionPreference = "Stop" should make all errors terminating errors. Commented Mar 13, 2017 at 19:31

1 Answer 1

1

Odd you're the only person who seems to have noticed this issue.

I ran into this while calling one script from another in Powershell ISE. The script I was calling had $ErrorActionPreference = 'Stop' defined inside but was ignored.

I tried to find source code online and failing to do so I went to look at the module files in C:\Windows\System32\WindowsPowerShell\v1.0\Modules\SmbShare and discovered that New-SmbShare and family are CIM-based Cmdlet Definition XML (CDXML) cmdlets (that I'd not heard of before).

After looking into it, it seems to be an issue of variable scoping with "script" modules. The functions of the module can't see $ErrorActionPreference. Setting $Global:ErrorActionPreference = 'Stop' gives the correct behavior as does setting -ErrorAction Stop directly on the cmdlet.

I believe this thread represents the ongoing discussion of the issue.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.