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.
This topic is 3 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
dcarlson
Posts: 3
Last visit: Mon Mar 04, 2024 8:03 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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 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: Mon Mar 04, 2024 8:03 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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Script Cannot Find Called Script

Post by jvierra »

OR

$result = & $pathToScript\CALLEDSCRIPT.ps1 -Variable1 $var1 -Variable2 $var2
User avatar
Alexander Riedel
Posts: 8473
Last visit: Tue Mar 19, 2024 1:15 am
Answers: 19
Been upvoted: 37 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: Mon Mar 04, 2024 8:03 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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 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.
This topic is 3 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked