locked
Powershell... bug?? RRS feed

  • Question

  • When I run this simple code in editor...it errors the first time.. (can't find the function name - testScope).
    I press F5 again...and it works....wtf?  i'm trying to test function outputs and this happens with code below.
    I'm new to powershell....

    $testVar = "test"
    testScope ( $testVar )

    Function testScope( [STRING] $tVar)
    {
       
        $output = $tVar
        [System.Windows.Forms.MessageBox]::Show($output)
    }
    Tuesday, September 19, 2017 5:42 PM

All replies

  • Before trying to use PowerShell you need to learn the basics. 

    Start here to learn basic PowerShell: https://mva.microsoft.com/en-us/training-courses/getting-started-with-microsoft-powershell-8276

    You cannot call a function before it is defined.


    \_(ツ)_/


    • Edited by jrv Tuesday, September 19, 2017 5:50 PM
    • Proposed as answer by Richard MuellerMVP Tuesday, September 19, 2017 6:39 PM
    Tuesday, September 19, 2017 5:50 PM
  • Hi,

    I agree with jrv.

    Based on my research, I'd like to explain that PowerShell executes code sequentially. I recommend you could place your function at the front so that PowerShell could load them firstly.

    If you need further help, please feel free to let us know.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Wednesday, September 20, 2017 3:07 AM
  • Futhermore, functions are not called using brackets. 
    #wrong:
    testScope ( $testVar )
    
    #right
    testScope $testVar

    Wednesday, September 20, 2017 5:47 AM
  • thank you.

    The videos..the guys sure talk alot about nothing until they get to the meat of the subject.

    Thursday, September 21, 2017 2:01 PM
  • I'll try it.
    But since this post, I've learned how to create modules and have it work.

    thank you
    Thursday, September 21, 2017 2:02 PM
  • Ok.. I learned something. Thank you.
    Thursday, September 21, 2017 2:03 PM
  • Start here: https://mva.microsoft.com/en-us/training-courses/getting-started-with-microsoft-powershell-8276

    By skipping fundamentals you only make it harder to understand things later.  If you choose the wrong video or fail to spend the time necessary to understand the lesson you will not move forward correctly.

    The linked video is very basic and accurate but requires a good attention span.  It is not a variety show or a rock video.  It takes time to learn how to learn.


    \_(ツ)_/


    • Edited by jrv Thursday, September 21, 2017 2:10 PM
    Thursday, September 21, 2017 2:09 PM
  • Hi,

    I'm checking how the issue is going, was your issue resolved?

    And if the replies as above are helpful, we would appreciate you to mark them as answers, and if you resolve it using your own solution, please share your experience and solution here. It will be greatly helpful to others who have the same question.

    Appreciate for your feedback.

    Best Regards,
    Albert Ling


    Please remember to mark the replies as an answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Monday, September 25, 2017 5:20 AM