Forum Discussion

Pat Richard's avatar
Oct 01, 2018

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...
  • VasilMichev's avatar
    VasilMichev
    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

Resources