learning the "param" - i dont get it

This topic contains 6 replies, has 2 voices, and was last updated by  Lev Leiderman 2 months, 2 weeks ago.

  • Author
    Posts
  • #95751

    Lev Leiderman
    Participant

    So,
    i've tried to learn it already two days and unfotinatly, i'm not getting something write

    i watched lots of videos and read lots of articales and still don't get it

    so i need your help

    i've tried two difreint scripts through the "function" parameter

    first is a very very basic and the second one is abit less basic and i dont' understand what is the problem

    1) script which is worked:

    function my-test {
       param($test)
        write-host "i did a $test"
    
    }

    2) second script which is not working:

    function net-test {
        
        $levspc = "computername"
    
        Read-Host "what is the computer name?"
    
        param (Test-Connection -computername $levspc -Count 1)
    
        write-host "the $levspc is alive "
    }
  • #95754

    Don Jones
    Keymaster

    It might be worth reading something like "Learn PowerShell Scripting in a Month of Lunches," which goes over this in pretty gory detail.

    Param() blocks must be the first thing defined inside the function. In the second example, you didn't do that, and so the block is ignored.

    Additionally:

    param (Test-Connection -computername $levspc -Count 1)
    

    That is not a parameter definition. That is executable code. A parameter is a "placeholder" (a variable, really) for a piece of information that you want input into your script or function. That's not what you've done here. Aside from a ValidationScript(), which is a bit of a more advanced topic, parameter blocks aren't executable code. They're definitions, or declarations, of inputs that the script or function can accept.

    • #95760

      Lev Leiderman
      Participant

      Don,
      i got it !!

      function net-test {
      
          param ($computername1,
          
          $computername2 )
      
      
      
          Test-Connection $computername1, $computername2 |Format-Table -AutoSize
      }

      Thanks alot !
      PS: is there any way to send you a PM?

  • #95762

    Don Jones
    Keymaster

    We don't support private messaging on the site; it's been a problem in the past what with spammers and all. And, because of my job, I'm not allowed to provide private consulting or assistance, so it's easier to have that turned off for me.

    Glad you got it working!

    • #95765

      Lev Leiderman
      Participant

      it's not for assistance or support
      if you will read this post and delete it, i will gladly write it here

  • #95768

    Don Jones
    Keymaster

    Sure, but Google hits our site almost _constantly_, so I can't guarantee that a post will be deleted before they cache it. You can also use the "HMU" menu item on DonJones.com, if you need to send something that you don't want to be public, provided it isn't a technical assistance issue.

    • #95771

      Lev Leiderman
      Participant

      ok, you'll get it there in a few minutes

You must be logged in to reply to this topic.