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?
$ErrorActionPreference = "Stop"should make all errors terminating errors.