0

Suppose I have a function in test.ps1, its name is show the code is like below:

. "some_path\some_other_script.ps1"

function show {
    write-host "Hello World"
    #some other function call from some_other_script.ps1
    ...
}

How can I call show in follow format (in a & - call operator)

powershell.exe "& '%test_script_path%\test.ps1'\show"

I think I need to dot source test.ps1 first in order to get dependencies in show function from some_other_script.ps1

I know I can create a new script and put the code in show in the new script instead of in a function. In that way, when do &... the script will be invoked. But I don't want to create a new script just for a very simple function

Thanks for any suggestion

2 Answers 2

2

You could dot source it and then call it like this.

powershell -Command ". '%test_script_path%\toolsql.ps1'; show"

The ; is used as a separator between commands.

Sign up to request clarification or add additional context in comments.

6 Comments

Hi, I tried your method, but it says "show : The term 'show' 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." I am sure that show is in the script buecause when I dot source the script in powershell instead of in cmd, it runs successfully
Is show not in toolsql.ps1? Reading your post again, I'm assuming show is not in toolsql but in another file called test.ps1? If that's the case you would need to dot source both of them, i.e. powershell -Command ". '%test_script_path%\toolsql.ps1'; . 'path\to\test.ps1'; show"
Sorry @Patrick, I made a typo. show IS IN test.ps1. I've updated the post. So basically, I want to dot source test.ps1 first and run show in test.ps1. I was hoping your first method would work, but somehow, it didn't...
Can you try it from the powershell console directly? Start up powershell, then run . path\to\test.ps1 and then show. I suspect something in the dependency script is causing the script to exit before it gets to your function as I made a test script that ran with my example without issue. If that does end up being the issue, remove . "some_path\some_other_script.ps1" from test.ps1 and maybe try dot sourcing that separately.
Thanks @Patrick Meinecke I tried to run in Powershell console. Everything is good. In fact, I updated test.ps1 a little bit. Now it is like below: write-host "let me see" show function show{write-host "Hello World"} And I ran it like below: C:\Users\xyz\Desktop>powershell -command ".\test.ps1" Here is the console: let me see show : The term 'show' 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.
|
0

I am really appreciated for the help and time from @Patrick Meinecke, the following command works.

powershell -command ". .\test.ps1;show"

Like he mentioned in the chat, "So to explain that the .\ just indicates it's in the current directory it still needed the other . to tell powershell to load the script into the current context"

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.