Script Cannot Find Called Script

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
dcarlson
Posts: 3
Last visit: Wed Feb 17, 2021 11:59 am

Script Cannot Find Called Script

Post by dcarlson »

Hello Everyone,

I've got a script that I've been working with that is having a strange issue, and it appears to be specific to Powershell Studio. When I try to run the script in Studio, it fails on the following line:

$result = &"\\SERVER\PATH TO SCRIPT\CALLEDSCRIPT.ps1" -Variable1 $var1 -Variable2 $var2

I've cleaned up the actual names but it really doesn't matter. The error I get is as follows:
--------------
& : The term '\\SERVER\PATH TO SCRIPT\CALLEDSCRIPT.ps1' 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.
At line:1 char:12
+ $result = &"\\SERVER\PATH TO SCRIPT\CALLEDSCRIPT.ps1" -Variable1 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (\\SERVER\P...ALLEDSCRIPT.ps1:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
--------------

This reads like the script being called isn't accessible or missing, but I have triple-checked the actual path and file about a million times. If I open the called script in Studio directly and run it independently, it runs fine. If I run the calling script outside of Powershell Studio, I get no errors, the script executes fine, calls the desired scripted and everything. This is all on the same workstation under the same profile. Changing between admin and non-admin calls doesn't seem to make a difference. Is there something in Studio I am missing? I'm running Powershell Studio version 5.8.185.0

I AM able to run & call other scripts from Powershell Studio, including those in this exact same directory, using this method of calling other scripts. I've been doing a lot of this lately so I'm very perplexed as to why all of the sudden it's having an issue with just one of many scripts.

Sorry if this is the wrong place for this. It seems like a studio issue but it's also kind of a scripting issue? Happy to repost elsewhere. Thanks!

jvierra
Posts: 14842
Last visit: Thu Jun 24, 2021 12:40 pm
Answers: 11
Has voted: 3 times
Been upvoted: 10 times

Re: Script Cannot Find Called Script

Post by jvierra »

You don't need quotes to call a PS1 script. YOu don't need to use&.

$result = \\SERVER\PATH TO SCRIPT\CALLEDSCRIPT.ps1 -Variable1 $var1 -Variable2 $var2

If there are spaces in teh path then do this:

$result = \\SERVER\'PATH TO SCRIPT'\CALLEDSCRIPT.ps1 -Variable1 $var1 -Variable2 $var2

Note the single quotes.

The following free book will help you understand how to use strings in various ways and why.

https://www.sapien.com/books_training/W ... werShell-4

dcarlson
Posts: 3
Last visit: Wed Feb 17, 2021 11:59 am

Re: Script Cannot Find Called Script

Post by dcarlson »

I'm sorry I wasn't very clear on why I'm running my commands with ampersand (&). I'm using this currently because most of the time, the path to the script being called is typically stored in a variable. So in reality it'd be something more like:

$result = &"$pathToScript\CALLEDSCRIPT.ps1" -Variable1 $var1 -Variable2 $var2

When I remove this variable and explicitly state the entire path, adjusting the script based on what you said (removed &, used single-quotes for any directory names that have spaced), the script call works fine. Unfortunately that won't be a working solution since I need these paths to be somewhat flexible.

Is there a better way to call PS1 scripts from other PS1 scripts if the entire path to the script is contained in a string variable?

Also I've downloaded the book and will try to parse through it. It is 700+ pages though...I'll try my best to find some solution in there, thank you for sharing it.

jvierra
Posts: 14842
Last visit: Thu Jun 24, 2021 12:40 pm
Answers: 11
Has voted: 3 times
Been upvoted: 10 times

Re: Script Cannot Find Called Script

Post by jvierra »

You still do not want the ampersand.

Invoke-Expression "$pathToScript\CALLEDSCRIPT.ps1 -Variable1 $var1 -Variable2 $var2"

jvierra
Posts: 14842
Last visit: Thu Jun 24, 2021 12:40 pm
Answers: 11
Has voted: 3 times
Been upvoted: 10 times

Re: Script Cannot Find Called Script

Post by jvierra »

OR

$result = & $pathToScript\CALLEDSCRIPT.ps1 -Variable1 $var1 -Variable2 $var2

User avatar
Alexander Riedel
Posts: 7702
Last visit: Thu Jun 24, 2021 11:58 am
Answers: 4
Been upvoted: 10 times

Re: Script Cannot Find Called Script

Post by Alexander Riedel »

It's a good practice to always check if the path is correct.
if (!(Test-Path $PathToFile)) {
Write-Warning "$PathToFile not found"
}
else {
$result = & $PathToFile -Variable1 $var1 -Variable2 $var2
}

If you are running a script from an admin console, elevated, some personal folders may not exist or actually be a different location.
So always make sure that you check.
Alexander Riedel
SAPIEN Technologies, Inc.

dcarlson
Posts: 3
Last visit: Wed Feb 17, 2021 11:59 am

Re: Script Cannot Find Called Script

Post by dcarlson »

I am the literal dumbest person alive.

I forgot a hyphen in one of my parameters that is used in my variable to the path to the script. I am so sorry for wasting everyone's time!

But, I'll definitely take the advice given here and modify how I'm calling these scripts going forward. Thanks so much!

jvierra
Posts: 14842
Last visit: Thu Jun 24, 2021 12:40 pm
Answers: 11
Has voted: 3 times
Been upvoted: 10 times

Re: Script Cannot Find Called Script

Post by jvierra »

The ultimate outcome of all misadventures with computers is that, as dumb as they are, they always show us that humans are maters of dumb.

Good luck and may the force be with you -- or at least, not annoy you too much.

Locked