Brackets in PowerShell Code Question

Welcome Forums General PowerShell Q&A Brackets in PowerShell Code Question

Viewing 7 reply threads
  • Author
    Posts
    • #269534
      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.

    • #269537
      Participant
      Topics: 5
      Replies: 170
      Points: 656
      Helping Hand
      Rank: Major Contributor

      You are actually missing }s and )s, but there is a much simpler way to do what you are trying to do.  First, to fix your existing code it should look like this:

      But here is the simpler way:

       

    • #269546
      Participant
      Topics: 16
      Replies: 1855
      Points: 3,568
      Helping Hand
      Rank: Community Hero

      Out of curiosity, why would you check that client-side when that data is in the SCCM database. You would just advertise to clients that meet that criteria rather than sending to a number of clients to check locally/

    • #269618
      Participant
      Topics: 4
      Replies: 12
      Points: 43
      Rank: Member

      Hi Rob,

      To answer your question, I use client side queries because it is a more real-time view of my environment.  Where as SCCM inventory has to occur send the data on a inventory cycle to the server and it has to be processed in the inventory for it to report accurately.  I agree it works just as well and for most things is perfect but if I have a urgent update and need to target machine with a specific software and version installed this will allow me to check at the client real-time and thus not miss anything that has not yet reported inventory back to the server.  I hope that makes sense.

    • #269624
      Participant
      Topics: 4
      Replies: 12
      Points: 43
      Rank: Member

      Hi Mike,

      Thanks for the information it did fix it also thank you for the alternative way to accomplish what I am doing.

      I do have a question for you it feels like it was easy for you to look at my code and identify what was missing and where it was missing from.  In an effort for me to learn how to be that awesome is there something you could recommend that teaches how to identify where brackets need to be placed and possibly why that I can read or get access to?  I love the help and great advise but I really am trying to learn and am open to any lessons I can get my hands on to get better at writing scripts, functions, and modules in PowerShell.

      Thanks again!

    • #269663
      Participant
      Topics: 5
      Replies: 170
      Points: 656
      Helping Hand
      Rank: Major Contributor

      othompson09,

      I use VS Code to write scripts which have create syntax highlighting to identify those issues, but even with the PowerShell ISE, there are ways to help identify open/close ({[.  In the ISE when you highlight a bracket, parens, etc., it’s pair is also highlighted.  This may be hard to see with the default color scheme in ISE since the script pane is white and the highlighting is very light grey, but you can change the color scheme by going to tools then options.  If you change the color scheme I would recommend using one of the built in themes and not manually changing individual element colors.  The “dark console, dark editor” theme will help you see the highlighted brackets.  The other thing you can do in ISE is when you have a parens, bracket, etc highlighted, hold down CTRL and press].  This will bounce the cursor between the matching pairs.  Finally, and this is a best practice when coding and scripting in any language, whenever you open a parens, bracket, etc, ALWAYS type the closing pair, then type whatever you need inside.  VS Code scripting environment helps enforce this.  Hope this helps.

    • #269717
      Participant
      Topics: 4
      Replies: 12
      Points: 43
      Rank: Member

      Mike,

      This was just the type of information I was hoping to get here.

      I appreciate all the insight and information you have provided thanks a million Sir!

    • #270014
      Participant
      Topics: 25
      Replies: 206
      Points: 772
      Helping Hand
      Rank: Major Contributor

      Not sure if the forum software in text mode mangled your code, but, what I re-iterate to every person I work with in the shell. Formatting, formatting, formatting.

      pick one of the accepted norms, and stick to it. I’d say 90% of the time folks are having issues with simple code, when they are forced to go back through the script and format, usually you’ll pick up immediately when/where you’re missing brackets.

      in addition, use a good editor, VS Code/ise, just don’t use a text editor.

      • #270077
        Participant
        Topics: 4
        Replies: 12
        Points: 43
        Rank: Member

        Hi David,

        Thanks for the insight, just an FYI I write all of my code in PowerShell ISE.

        Using the Dark on Dark editor seems to force the brackets to stand out.

        Also learning that I need to look for each opening bracket and know each need a closing bracket helps me know where to start looking for a missing closing bracket.

        I appreciate all of the information and insight you guys provide it is a great help on my journey learning PowerShell as I am a beginner.  What I am doing to learn is going through Pluralsight’s various PowerShell course some of them have example scripts that we can use and learn from by adding functionality to them.

        When I basically re-write line by line these scripts I think I am missing little parts and cannot seem to find them from time to time so I come here for help and advise on how to figure them out.

        So far this community has been spot on and a great resource.

        I am sure over time I will learn both from the courses and from this forum and be able to write scripts totally on my own to solve any situation that might come up.  That is my hope!

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