19

I am attempting to call another PS script B.ps1 which takes a parameter:

Invoke-Expression "C:\AzureFileShare\MEDsys\Powershell Scripts\B.ps1 -ServerName medsys-dev"

Then in B.ps1:

Param([string]$ServerName)
...
...

When I attempt to execute Invoke-Expression, I get the following error:

The term 'C:\AzureFileShare\MEDsys\Powershell' is not recognized as the name of a cmdlet, function, script file, or operable program.

I don't know why PowerShell is complaining about this since the B.ps1 script does actually exist in the referenced folder. Unless something else is wrong?

6
  • dot source the script. Commented Nov 2, 2017 at 12:11
  • 1
    @guiwhatsthat - What? Commented Nov 2, 2017 at 12:12
  • Take alook at the answer which I posted Commented Nov 2, 2017 at 12:15
  • .\B.ps1 -parameter "Value" You have to change your directory to B.ps1's location. Commented Nov 2, 2017 at 12:19
  • No, you don't have to change locations. Just specify the path and filename of the script you want to run. Commented Nov 2, 2017 at 16:44

3 Answers 3

31

EDIT: dot-sourcing the script as shown in my original answer will load the script and variables, objects, functions, etc. into the current session - this might have unintended consequences in certain circumstances.

The alternative is to use the & call operator which runs the script in its own scope:

& "C:\AzureFileShare\MEDsys\Powershell Scripts\B.ps1" -ServerName medsys-dev

The problem is you have a space in your file path so you need to wrap the entire path in single quotes so it's recognised correctly. (See about_Quoting_Rules for more info on single vs double quotes). It ends up being a messy command in the end:

Invoke-Expression "&'C:\AzureFileShare\MEDsys\Powershell Scripts\B.ps1' -ServerName medsys-dev"

dot-sourcing is much nicer as you just wrap the script path in double quotes and leave the params as-is:

."C:\AzureFileShare\MEDsys\Powershell Scripts\B.ps1" -ServerName medsys-dev
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. This is close. But now I'm getting the following error: A positional parameter cannot be found that accepts argument 'C:\AzureFileShare\MEDsys\Powershell Scripts\B.ps1'.
See edit, I'd copied the wrong line from my testing.
18

You don't need Invoke-Expression or dot-sourcing. Just run the script:

& "C:\AzureFileShare\MEDsys\Powershell Scripts\B.ps1" -param1 arg1...

If the script's path and/or filename contain spaces, enclose the script in quotes and use the & (call) operator to run the script. If the script's path and/or filename does not contain quotes, you can run it without the quotes or the & operator:

C:\AzureFileShare\MEDsys\PowershellScripts\B.ps1 -param1 arg1...

The reason I don't recommend dot-sourcing the script is because the variables, functions, etc. from the script will appear in the current session. This is probably not the intent if you just want to run the script and not pollute the current session's namespace with stuff from the script.

4 Comments

Can we use -Credential parameter with this call?
& won't work if you want to pass multiple $args
(?) I use & all the time with multiple arguments.
Simple and works with multiple args
1

Use dot sourcing

Like that:

."PathtoScript\script.ps1" "ParamValue"

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.