newb question - creating functions

Welcome Forums General PowerShell Q&A newb question - creating functions

This topic contains 3 replies, has 3 voices, and was last updated by

 
Participant
2 weeks, 5 days ago.

  • Author
    Posts
  • #132300

    Participant
    Points: 14
    Rank: Member

    Hello

    I am new to powerscript – so the answer to my question is probably something simple I am overlooking.

     

    I am attempting to create a function just for testing purposes so that I can play around with how that process works.  I can do it in the console without issue just by typing this in

    function script:testecho {echo this is a functional test of echo}

    calling it by typing in testecho produces the expected result

    PS C:\powershell\batch> testecho
    this
    is
    a
    functional
    test
    of
    echo

     

    however, putting the same thing in a script does not perform as expected

    cls

    C:\powershell\batch>Function script:testecho {echo this is a test of the echo function}
    'Function' is not recognized as an internal or external command,
    operable program or batch file.

    C:\powershell\batch>pause
    Press any key to continue . . .

     

    C:\powershell\batch>testecho
    'testecho' is not recognized as an internal or external command,
    operable program or batch file.

    C:\powershell\batch>pause
    Press any key to continue . . .

     

    I am sure I am doing something simple wrong.

    Can someone tell me what it is?

    thanks

     

    Brent

     

  • #132327

    Participant
    Points: 282
    Helping Hand
    Rank: Contributor

    Hi Brent – You need to source the script before you call the function inside the script.

    
    # Insert the function inside the script and source it.
    
    C:\> .  .\Test-Function.ps1
    
    # Now call the function.
    
    C:\> testecho
    
    
  • #132339

    Participant
    Points: 438
    Helping Hand
    Rank: Contributor

    Hey Brent,

    I'm surprised your test worked, normally you would get an error because you have random strings in a function and Powershell doesn't know what to do with it...

    PS C:\> function Test-Echo {
        This is a test
    }
    
    PS C:\> Test-Echo
    This : The term 'This' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At line:2 char:5
    +     This is a test
    +     ~~~~
        + CategoryInfo          : ObjectNotFound: (This:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException
     
    
    PS C:\> 
    

    This is a better 'echo test':

    PS C:\> function Test-Echo {
        "This is a test"
    }
    
    PS C:\> Test-Echo
    This is a test
    
    PS C:\> 
    

    however, putting the same thing in a script does not perform as expected

    Now, calling a script, you left out some details. To call a script, use dot sourcing, like so:

    PS C:\scripts> .\Test-Echo.ps1
    
    PS C:\scripts> C:\Scripts\Test-Echo.ps1
    This is a test
    

    In the above, there are 2 results. The first is running:

    function Test-Echo {
        "This is a test"
    }
    

    If you notice, there is no echo, because you are only loading the function into memory and not calling the function, so you need to call the function:

    function Test-Echo {
        "This is a test"
    }
    
    Test-Echo
    

    Last, the reason your attempts did work. Powershell is built to be simple for system administrators. That being said, it will do many conversions for you. You need to be careful though because you can see my attempt failed but yours worked. It appears since you built the function in the prompt, Powershell made your echo string into a n array, which should look more like 'this','is','a','test'; however, Powershell has automatically done the conversion.

    PS C:\scripts> function script:testecho {echo this is a functional test of echo}
    
    
    
    PS C:\scripts> $test = testecho
    
    PS C:\scripts> $test
    this
    is
    a
    functional
    test
    of
    echo
    
    PS C:\scripts> $test.GetType()
    
    IsPublic IsSerial Name                                     BaseType                                                                                                                                                                                              
    -------- -------- ----                                     --------                                                                                                                                                                                              
    True     True     Object[]                                 System.Array                                                                                                                                                                                          
    
    
    
    PS C:\scripts> 
    
  • #132530

    Participant
    Points: 14
    Rank: Member

    thank you all for the help.  It is working now.

     

    Brent

     

You must be logged in to reply to this topic.