none
how to fix my catch all script RRS feed

  • Question

  • I created a script to catch all data, but it keeps giving me a error message. the error message is below:

    At C:\autoscripts\LyncClientPolicies\AllowFederationAccess.ps1:20 char:2

    + }

    +  ~

    The Try statement is missing its Catch or Finally block.

        + CategoryInfo          : ParserError: (:) [], ParseException

        + FullyQualifiedErrorId : MissingCatchOrFinally

    Script below:

    Import-Module Lync 

    //first create your log file

     

    set

     

    //then your query

     

    $users = get-csuser -LDAPFilter "memberOf=CN=LyncAllowFederationAccess,OU=Lync,OU=Groups,DC=qvcdev,DC=qvc,DC=net"

     

    //then loop it

     

    foreach ($person in $users){

    //then try the command

    try{

    //then execute the grant command

    Grant-CsExternalAccessPolicy -Identity "LyncAllowFederationAccess" -Policyname "Allow Federation Access" -ErrorAction Continue

    //log succss

    add-content -path C:\autoscripts\lyncClientPlicies\log.txt "Applied Policy to $ "LyncAllowFederationAccess" Successfully"

    }

    //now catch any errors

    catch{

    //if failed log event

    add-content -path C:\autoscripts\lyncClientPlicies\log.txt -value "FAILED to grant policy to $($person.SipAddress)"

    //catch the PS Exception message

    add-content -path C:\autoscripts\lyncClientPlicies\log.txt -value $_.ExceptionMessage

    }

    }


    James

    Tuesday, December 1, 2015 7:53 PM

Answers

  • Your script is not readable. Please try using proper indentation.

    Also, PowerShell uses # as the comment character, not "//".

    The correct syntax for try/catch/finally is documented in the help.


    PS C:\> help about_Try_Catch_Finally
    


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by JJOHN7 Tuesday, December 8, 2015 10:05 PM
    Tuesday, December 1, 2015 7:56 PM
    Moderator
  • You have copied C# code and are tying to run it in PowerShell.  This will not work.


    \_(ツ)_/

    • Marked as answer by JJOHN7 Tuesday, December 8, 2015 10:05 PM
    Tuesday, December 1, 2015 8:03 PM
  • As you can see, it's not going to be possible to simply guess your way to a solution.

    There are lots of good and free resources available on the Internet to help you learn PowerShell.


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by JJOHN7 Tuesday, December 8, 2015 10:05 PM
    Tuesday, December 1, 2015 8:23 PM
    Moderator

  • PS C:\> help about_Redirection
    

    I think you would be best served by learning the basics of PowerShell.

    As I noted before, there are lots of good resources available.


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by JJOHN7 Tuesday, December 8, 2015 10:05 PM
    Tuesday, December 1, 2015 8:43 PM
    Moderator

All replies

  • Your script is not readable. Please try using proper indentation.

    Also, PowerShell uses # as the comment character, not "//".

    The correct syntax for try/catch/finally is documented in the help.


    PS C:\> help about_Try_Catch_Finally
    


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by JJOHN7 Tuesday, December 8, 2015 10:05 PM
    Tuesday, December 1, 2015 7:56 PM
    Moderator
  • You have copied C# code and are tying to run it in PowerShell.  This will not work.


    \_(ツ)_/

    • Marked as answer by JJOHN7 Tuesday, December 8, 2015 10:05 PM
    Tuesday, December 1, 2015 8:03 PM
  • I replace // with # and it asking  to supply values for the following paramerters: Name[0]: 

    James

    • Marked as answer by JJOHN7 Tuesday, December 8, 2015 10:05 PM
    • Unmarked as answer by Bill_StewartModerator Tuesday, December 22, 2015 4:10 PM
    Tuesday, December 1, 2015 8:20 PM
  • As you can see, it's not going to be possible to simply guess your way to a solution.

    There are lots of good and free resources available on the Internet to help you learn PowerShell.


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by JJOHN7 Tuesday, December 8, 2015 10:05 PM
    Tuesday, December 1, 2015 8:23 PM
    Moderator
  • How to catch all errors and success log for a simple script?

    James

    • Marked as answer by JJOHN7 Tuesday, December 8, 2015 10:05 PM
    • Unmarked as answer by Bill_StewartModerator Tuesday, December 22, 2015 4:10 PM
    Tuesday, December 1, 2015 8:27 PM
  • Here's my corrected script. I just want to add catch all date for this script errors and success:

    Import-Module Lync

    get-csuser -LDAPFilter "memberOf=CN=LyncAllowFederationAccess,OU=Lync,OU=Groups,DC=qvcdev,DC=qvc,DC=net" | Grant-CsExternalAccessPolicy -PolicyName "Allow Federation Access"


    James

    • Marked as answer by JJOHN7 Tuesday, December 8, 2015 10:05 PM
    • Unmarked as answer by Bill_StewartModerator Tuesday, December 22, 2015 4:10 PM
    Tuesday, December 1, 2015 8:29 PM
  • Sorry; I don't understand the question.


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by JJOHN7 Tuesday, December 8, 2015 10:05 PM
    • Unmarked as answer by Bill_StewartModerator Tuesday, December 22, 2015 4:11 PM
    Tuesday, December 1, 2015 8:35 PM
    Moderator
  • From the script below how could i add lines within this script below to create a log.txt file

    Import-Module Lync

    get-csuser -LDAPFilter "memberOf=CN=LyncAllowFederationAccess,OU=Lync,OU=Groups,DC=qvcdev,DC=qvc,DC=net" | Grant-CsExternalAccessPolicy -PolicyName "Allow Federation Access"


    James

    • Marked as answer by JJOHN7 Tuesday, December 8, 2015 10:05 PM
    • Unmarked as answer by Bill_StewartModerator Tuesday, December 22, 2015 4:10 PM
    Tuesday, December 1, 2015 8:40 PM

  • PS C:\> help about_Redirection
    

    I think you would be best served by learning the basics of PowerShell.

    As I noted before, there are lots of good resources available.


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by JJOHN7 Tuesday, December 8, 2015 10:05 PM
    Tuesday, December 1, 2015 8:43 PM
    Moderator