1

Have installed PowerShell on Linux and have created a custom function like below and saved it as test.ps1, Now how do I call the function ttr by passing value to $file and $name

#!/usr/bin/env pwsh

function ttr {
    Param(
             $file,
             $name
         )
    Write-Host $file
    Write-Host $name
}
5
  • 2
    From where do you want to call the function: the same script, another script, an interactive PowerShell prompt? How have you tried calling the function? Have you taken a look at the about_Functions documentation? Commented Sep 12, 2018 at 22:42
  • Was trying to call it from another script, calling the function from the same script works as expected Commented Sep 12, 2018 at 23:34
  • 1
    Possible duplicate of How to call a function in another PowerShell script when executing PowerShell script using 'Run With PowerShell' Commented Sep 13, 2018 at 0:29
  • The above link refers to Windows Environment, but I have installed powershell core in linux and trying to call the function through another script. Commented Sep 13, 2018 at 4:13
  • The key point of the linked question is attempting to call a function defined in one script from another script; despite the title specifically mentioning using the "Run with PowerShell" context menu item to invoke the script I don't believe that is significant to the issue. As for this question, I do not believe function invocation differs between Windows and Linux - PowerShell scripts would not be portable between platforms otherwise - so the answer to the question would be the same for both environments. Commented Sep 13, 2018 at 5:24

3 Answers 3

1

Am sure there might be some other way, but this one worked for me

Saved the below file in /tmp/testscript.ps1

#!/usr/bin/env pwsh
function New-ttr{

    Param(
             $file,
             $name
         )
    write-host $file
    write-host $name
}

created another script like below and saved in /tmp/callscript.ps1

#!/usr/bin/env pwsh                                                                                                                                                                                                
import-module /tmp/testscript.ps1     
New-ttr -file "Test call in PSCore" -name "Testing"

then

chmod 777 callscript.ps1

./tmp/callscript.ps1
Sign up to request clarification or add additional context in comments.

Comments

0

To make a powershell function accessible outside of a powershell script, you will have to run the script so that any variables or functions get loaded in.

To do this you will need to write function as "global:ttr", then run the script. Variables require you to write them as such "$global:varname".

Afterwards, for the duration of the current shell, you will have access to said function and variable.

3 Comments

Dot-sourcing is an alternative way to do this, and I believe is the more typical approach, too.
@BACON That should probably be a separate answer and not a comment on this question
@Andy That's a fair point. To be honest, I've never seen functions added to the global scope to accomplish this, so my comment was really a gentle way of saying "Yes, that is a way to do that, but I don't think is/should be recommended." It's also not yet clear to me that the question author is trying to call a function from outside of the script.
0

I just ran your post code sample on OSX, in VSCode editor, both terminals, and via the normal default PSCore terminal and it ran as expected.

Also, remember the proper naming construct in PowerShell if verb-noun. So your function should be something line New-ttr.

#!/usr/bin/env pwsh

function New-ttr{
    Param(
             $file,
             $name
         )
    write-host $file
    write-host $name
} 

So, I saved the code as test.ps1 in my downloads folder and executed with …

~/downloads/test.ps1

... from each of the terminals. Running in the VSCode editor is just a matter of selecting the text...

    function New-ttr{
        Param(
                 $file,
                 $name
             )
        $file
        $name
    } 
    New-ttr -$file Somefilename -name somename

select terminal and run selected text or run Active File. Lastly, don't use Write-Host unless you are coloring screen text output, or other needed formatting conditions. Output to the screen is the default.

So, this...

write-host $file
write-host $name

.. and this...

$file
$name

… will do the same thing.

Write-Host is not pipeline friendly, and clears the buffer, so it's use is strongly discouraged.

If you want to use a write for screen output, consider using Write-Output or one of the other Write-* cmdlets.

Though I am unsure why you are passing both params $file and $name, even in this test. My assumption is one is a path and the other is the name of the file to look for. You can just pass the fullpath of the file in $file and extract the name using $file.Name.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.