Page 1 of 1

Script Cannot Find Called Script

Posted: Fri Feb 12, 2021 3:41 pm
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!

Re: Script Cannot Find Called Script

Posted: Fri Feb 12, 2021 3:56 pm
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

Re: Script Cannot Find Called Script

Posted: Mon Feb 15, 2021 12:58 pm
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.

Re: Script Cannot Find Called Script

Posted: Mon Feb 15, 2021 1:49 pm
by jvierra
You still do not want the ampersand.

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

Re: Script Cannot Find Called Script

Posted: Mon Feb 15, 2021 1:51 pm
by jvierra
OR

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

Re: Script Cannot Find Called Script

Posted: Mon Feb 15, 2021 1:58 pm
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.

Re: Script Cannot Find Called Script

Posted: Mon Feb 15, 2021 3:29 pm
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!

Re: Script Cannot Find Called Script

Posted: Mon Feb 15, 2021 6:25 pm
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.