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!
Script Cannot Find Called Script
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 forum is a space to discuss coding in PowerShell, and technical issues related to development.
- Question about a licensed SAPIEN product? Post here: Product Support for Registered Users
- Question about a trial SAPIEN product? Post here: Former and Future Customers - Questions
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 forum is a space to discuss coding in PowerShell, and technical issues related to development.
- Question about a licensed SAPIEN product? Post here: Product Support for Registered Users
- Question about a trial SAPIEN product? Post here: Former and Future Customers - Questions
Re: Script Cannot Find Called Script
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
$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
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.
$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.
- Alexander Riedel
- Posts: 8594
- Last visit: Thu Nov 28, 2024 6:44 pm
- Been upvoted: 42 times
Re: Script Cannot Find Called Script
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.
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.
SAPIEN Technologies, Inc.
Re: Script Cannot Find Called Script
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!
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
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.
Good luck and may the force be with you -- or at least, not annoy you too much.