Answered Error Catching.

  • Thursday, June 14, 2012 4:06 PM
     
     

    I'm trying to pull in the local security policy on my servers and I'm running my script against all my servers.  For the most part all is fine but on some servers I get the following error message.  Can someone tell me what it means and is possible how to fix it?  All my information still pulls in so if I could just silentlycontinue on this error it would be great

    New-Object : Exception calling ".ctor" with "1" argument(s): "Value was invalid.
    Parameter name: sddlForm"
    At C:\myscript.ps1:784 char:15
    +                 $sid = new-object System.Security.Principal.SecurityIdentifier($str)
    +                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvocationException
        + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand


    SMaximus7

All Replies

  • Thursday, June 14, 2012 4:24 PM
     
     

    You can add the erroraction preference with your New-Object call.

    New-Object  ... -ea silentlycontinue


    Grant Ward, a.k.a. Bigteddy

  • Thursday, June 14, 2012 5:46 PM
     
     
    I tried that but still get the error

    SMaximus7

  • Thursday, June 14, 2012 6:36 PM
     
     Answered
    use a try/catch
     
    you wouldn’t want to just pass over the failure of creating an object
    (normally) because you'd try to use it after and it would crap out.. so do a
    try catch and do something with it..
     
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked As Answer by SMaximus7 Thursday, June 14, 2012 7:43 PM
    •  
  • Thursday, June 14, 2012 7:43 PM
     
     
    The try/catch method worked flawlessly.  Thanks

    SMaximus7

  • Thursday, June 14, 2012 7:51 PM
     
      Has Code

    Hi,

    In your case you get terminating error then need use try/catch like Justin said or set trap:

    trap [Exception] 
    { 
     write-host "Catched error: $_.Exception.Message"
        continue
    }
    $sid = new-object System.Security.Principal.SecurityIdentifier($str)
     
    try
    {
     $sid = new-object System.Security.Principal.SecurityIdentifier($str)
    } 
    catch
    {
     write-host "Catched error: $_.Exception.Message"
    }