Parenthesis error

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: (:slight_smile: , 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.

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

function Validate-IsJavaInstalled {
    if (Get-WmiObject -Namespace ‘root\cimv2\sms’ -Class SMS_InstalledSoftware | where { ($_.ARPDisplayName -eq ‘Java 7 Update 55’)
}#closing where-object 
)# closing if condition
    {#opening true block(when if condition is met
        $true
    } else {
        $false
    }
}

 

Hope that helps.

 

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

if ($x -gt $y)
{
    
}

Example of formatting for readability

function Validate-IsJavaInstalled {
  $test = Get-WmiObject -Namespace ‘root\cimv2\sms’ -Class SMS_InstalledSoftware | 
    Where {$_.ARPDisplayName -eq ‘Java 7 Update 55’}
  if($test)
    {
      $true
    } 
  else
    {
      $false
    }
}