Forum Discussion
Pat Richard
Oct 01, 2018MVP
Catching errors
Trying to wrap my head around this. Let's say I execute the following, WITHOUT defining the variables:
try {
New-SmbShare -Description "$SmbShareDescription" -FullAccess 'everyone' -Name "$SmbS...
- Oct 02, 2018
Yup, you need to throw a terminating error, which in this case can be done by adding the -ErrorAction Stop parameter.
[10:05:49]# try { New-SmbShare test -Path \ } catch {"aaa"} New-SmbShare : The filename, directory name, or volume label syntax is incorrect. At line:1 char:7 + try { New-SmbShare test -Path \ } catch {"aaa"} + ~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (MSFT_SMBShare:ROOT/Microsoft/Windows/SMB/MSFT_SMBShare) [New-SmbShare], CimException + FullyQualifiedErrorId : Windows System Error 123,New-SmbShare [10:06:00]# try { New-SmbShare test -Path \ -ErrorAction Stop} catch {"aaa"} aaa
BlackV
Oct 01, 2018Brass Contributor
It only runs on a terminating error.
Is share already exists a terminating error, possibly not
Pat Richard
Oct 02, 2018MVP
Thanks. I've looked around to see how to tell if an error will be terminating, and not finding a reference. Anyone have info?