Parenthesis error

Welcome Forums General PowerShell Q&A Parenthesis error

Viewing 2 reply threads
  • Author
    Posts
    • #269498
      Participant
      Topics: 4
      Replies: 12
      Points: 43
      Rank: Member

      Building another script and still not fully understanding how to determine where brackets should go.  I am receiving an error

      after running this piece of code.

      PS C:\WINDOWS\system32> function Validate-IsJavaInstalled {
      if (Get-WmiObject -Namespace ‘root\cimv2\sms’ -Class SMS_InstalledSoftware | where { ($_.ARPDisplayName -eq ‘Java 7 Update 55’)
      $true
      } else {
      $false
      }
      }
      Missing closing ‘)’ after expression in ‘if’ statement.
      + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
      + FullyQualifiedErrorId : MissingEndParenthesisAfterStatement

      PS C:\WINDOWS\system32> $Error
      Missing closing ‘)’ after expression in ‘if’ statement.

      I am not sure what the expression is in the if statement which might help me find the missing ‘)’

      Can anyone help me understand how to identify where to find missing ‘)’ in a PowerShell script?

      Thanks for all your help and support.

    • #272269
      Participant
      Topics: 1
      Replies: 55
      Points: 525
      Helping Hand
      Rank: Major Contributor

      <p style=”text-align: left;”>Before $true you need to close where  with } , close if condition with ) and open another { :</p>

       

      Hope that helps.

       

    • #272689
      Participant
      Topics: 4
      Replies: 98
      Points: 442
      Helping Hand
      Rank: Contributor

      If you are not familiar with Snippets I recommend learning about them and using them! I love the built in ISE snippets, and have made many of my own for tasks I administrate daily. From the ISE console for example, if you click in the scripting pane and press “Ctrl + J” you will get a menu of all the snippets loaded in the ISE. Its very helpful for recalling syntax, such as an If statement or for advanced function (cmdlet). If statements always follow “If(Evaluation code here){Condition met}Else{Condition not met}”. Another thing that makes things easier is not getting too complex in your evaluation code within the If statement. For example if there is complex logic involved create a variable with the condition statement and then check the variable. Another thing I will suggest that has worked beautifully for me is formatting the code for readability. The way I format my code is not often seen, but to me it makes it SO much easier to see where the brackets end, etc. Making it easier to troubleshoot and see where things get mixed up. Also, don’t forget we get free line breaks after a pipeline, which lends further to readability. Some examples below
      Built in “IF” Snippet from ISE

      Example of formatting for readability

Viewing 2 reply threads
  • You must be logged in to reply to this topic.