1

I need to pass a few arguments from one script in file to another one. I load path to my current script file to variable and add name, arguments of other script that I want to call.

Here is the sample of calling and passing argument that I got in Script1.ps1:

Param([string]$argument)
$thisScript = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
.($thisScript+'\anotherScript.ps1 -passedArgument '+$argument)

Here is the part of the script Script2.ps1 that I'm calling:

Param([string]$passedArgument)
$passedArgument = "do some work with it HERE"

When I start the first script like this

C:\Users\user1\Desktop\Script1.ps1 -argument datatopass

it writes the error

The term 'C:\Users\user1\Desktop\Script2.ps1 -passedArgument datatopass' 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.

When I try use the script manually like this

C:\Users\user1\Desktop\Script2.ps1 -passedArgument datatopass

it works fine and doesn't report any error with wrong path or name.

I don't know where the problem is, and I couldn't find anything about this error.

2
  • did you noticed you missing the $ sign in: Param([string]passedArgument) ? Commented Jul 2, 2015 at 10:45
  • Yeah didnt noticed that i edited it. In my code its ok just forgot to add it here. Commented Jul 2, 2015 at 10:47

1 Answer 1

1

You dont have to concat the passedArgument with its value to a string. Try:

& (Join-Path $thisScript 'anotherScript.ps1') -passedArgument $argument
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah if i use ".(Join-Path $thisScript 'anotherScript.ps1') -passedArgument $argument" it works. Thank you

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.