“ForEach” looping, help with syntax

Welcome Forums General PowerShell Q&A “ForEach” looping, help with syntax

Viewing 8 reply threads
  • Author
    Posts
    • #244559
      Participant
      Topics: 1
      Replies: 3
      Points: -1
      Rank: Member

      I am only 2 days into PowerShell after watching Jeff Hicks’s 3-part PowerShell Essentials videos, so I apologize for the newbie obvious mistakes (20+ years programming, none in PowerShell !!).  I think my issue is with syntax placement.

      I’m trying to write a simple script to turn on WinRM on remote domain computers using the script below.  In the output window, the computers UT0291, PD3697, and PDCM06 do not ping, but the logic drops each object into the “True” section and I don’t get the “CANNOT ping” message I expect when $tc evaluates to “false”.  I’ve run the “Test-Connection” command on all 3 individually in the command window and each one returns “False”.  Definitely won’t be my last post for help !!  Thanks everyone.

      Script Run

    • #244571
      Participant
      Topics: 0
      Replies: 1
      Points: 7
      Rank: Member

      This should do it

      $tc -eq $true
    • #244574
      Participant
      Topics: 15
      Replies: 1776
      Points: 3,218
      Helping Hand
      Rank: Community Hero

      Simple mistake. The Powershell Comparison Operator is -eq, not =.

      should be:

      For loops, typically prefer to do loops like below versus $_ as it gets confusing what that variable context is if you are doing pipeline operations:

    • #244589
      Participant
      Topics: 1
      Replies: 3
      Points: -1
      Rank: Member

      Thank you both, that did it.  I’m guessing I should using the “=” type operators for arithmetic evaluations and “-eq” for boolean and string comparisons.

      Rob, in your suggestions, the line “foreach ($cname in $dcomps) { “, I have seen this type of statement in many examples.  However, where is the “$cname” variable declared ?  Is that just some variable that is created when the statement is run ? Could I have used “foreach ($dogpile in $dcomps){ ” and then reference the “$dogpile” variable ?  And I assume these are variables and not objects, and therefore do not have properties, like “$cname.value” or “$dogpile.value,  correct ?

      Ah, to be such a newbie again, I forgot what it was like to to be “programmer dumb” !!  Thanks again.

    • #244592
      Participant
      Topics: 15
      Replies: 1776
      Points: 3,218
      Helping Hand
      Rank: Community Hero

      Is that just some variable that is created when the statement is run ? Could I have used “foreach ($dogpile in $dcomps){ ” and then reference the “$dogpile” variable ?

      Yes, that is just a placeholder variable that is defined by you.

      And I assume these are variables and not objects, and therefore do not have properties, like “$cname.value” or “$dogpile.value,  correct ?

      This is a ‘it depends’. Almost everything is an ‘object’. Even if in the exammple above you are working with strings that have properties (e.g. Length) and methods.

      You can use GetType() to see what the type is and Get-Member to see what properties\methods exist for an object:

    • #244595
      Participant
      Topics: 1
      Replies: 3
      Points: -1
      Rank: Member

      The “flexibility” of PowerShell and it’s ability to perform the same task in a multitude of ways is a little
      “mind-boggling” !!  Other “rigid” languages are little easier (for me at least) to figure out and troubleshoot. Once you know
      how to structure the syntax, that was pretty much it.  I like the flexibility of PowerShell, it is really powerful, I just need more
      practice and training…..”baby steps”….although “baby steps” are at time very frustrating when you can write code in another
      language with your eyes closed !!

      Thanks again !!

    • #244754
      Participant
      Topics: 0
      Replies: 1
      Points: 5
      Rank: Member

      Hi Richard,

      Since you started coding in PowerShell. I can suggest you to use debug option in Powershell ISE (or other IDE which support Powershell Script Development).

      It really helps you to figure out the issue.

      In the above case if you have marked a breakpoint on code if ($tc = $true) {} and run the script on debug mode. When the logic reaches the line ,execute it and just hover the mouse above the $tc variable, so you can see “True” value getting assigned to the $tc variable.

      So this makes you understand it is not doing the comparison.

      You can learn more while debugging.!!!

       

       

    • #244985
      Participant
      Topics: 15
      Replies: 1776
      Points: 3,218
      Helping Hand
      Rank: Community Hero

      Powerhell is not really a programming language, it’s a scripting platform that you can use for programming tasks, but it was built for administrators that needed to perform tasks. Programming languages are more strict because you want consistent code for the compiler or for other developers. While the rules are loose, there are style recommendations to make code easier to read, like this https://github.com/PoshCode/PowerShellPracticeAndStyle

    • #245991
      Participant
      Topics: 1
      Replies: 3
      Points: -1
      Rank: Member

      Thanks, Rob, that makes sense.  I find a lot of similarities between “script” code and “language” code.  I think my biggest hurdle is going to be syntax.  For example, I just watched a video which references the “Switch” function, which seems to act a lot like a “Case” statement in programming.

      And thank you Sunish, I have discovered the “debug” options.  In programming, the “step” function is most helpful, so I think I will find it useful as I learn.

      I’m seeing that most of my work will be utilizing “WMI” and “CIM” commands to manage domain computers.  We also have a Govt Office 365 tenant, so I’m delving into the world of using powershell to manage MS exchange accounts in Office 365, such as group memberships, account management etc.

      Thanks again and stay tuned !!

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