2

I try to start a function of an script out of another script. I want to save the return into a variable but this doesn't work.

script1.ps1:

function test
{
  return "hallo"
}

script2.ps1:

./script1.ps1; $p=test

or
$p = ./script1.ps1; test

It seems that $p is null, but I don't know what's wrong. Can anybody please help me? thx

1 Answer 1

4

Try this:

. ./script1.ps1; $p=test

Why: you have to load the function into current scope (that's the period at the beginning – the dot source operator).

If you use ';', then completely new statement begins. So from you example $p = ./script.ps1; test, you assign output from script.ps1 to $p and then run the function.

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

1 Comment

that's not a comma, <- that is; It's a period. <- like that ;-)

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.