Why doesn't $PSScriptRoot resolve properly in PSS?

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST LICENSE NUMBERS, ACTIVATION KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

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 7 years and 3 months 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.
User avatar
david@koppy.com
Posts: 20
Last visit: Fri Jan 11, 2019 8:21 am

Why doesn't $PSScriptRoot resolve properly in PSS?

Post by david@koppy.com »

Product, version and build: PowerShell Studio 2016 v.5.3.131
(*** Please do not write "latest" as a version, specify a version number ***)
32 or 64 bit version of product:
Operating system: Windows 8.1
32 or 64 bit OS: 64-bit
PowerShell Version: 4

When I debug scripts that include references to $PSScriptRoot, PSS doesn't resolve the variable correctly when it asks for parameter input before starting the run. The parameter still appears as "$PSScriptRoot\scriptdata.txt" but only resolves when I change it to ".\scriptdata.txt" in the input dialog.
DevinL
Posts: 1098
Last visit: Tue Jun 06, 2017 9:15 am

Re: Why doesn't $PSScriptRoot resolve properly in PSS?

Post by DevinL »

Is it any script that references $PSScriptRoot that is having trouble or is it a particular set of scripts? If it's a particular set, can you upload one of those scripts so I can see what exactly you're trying?

You can upload it here or to https://sapien.com/support/upload if you wish for the code to remain private.
DevinL
SAPIEN Technologies, Inc.
User avatar
david@koppy.com
Posts: 20
Last visit: Fri Jan 11, 2019 8:21 am

Re: Why doesn't $PSScriptRoot resolve properly in PSS?

Post by david@koppy.com »

The problem is with the pop-up box that prompts for parameters when running from the program. $PSScriptRoot translates correctly in the script when it's used. The issue is entering the value as a parameter in the popup. I have a script that the default location for the file is in $PSScriptRoot. When I run the program inside PSS, the popup appears and asks me for parameters and gives the defaults listed in the code, listing the parameter default as -ComputerName "$PSScriptRoot\file.txt." Accepting this causes the file to be not found because the program doesn't translate $PSScriptRoot from the dialog. I have to change the value to ".\file.txt" in order to run the script successfully.
DevinL
Posts: 1098
Last visit: Tue Jun 06, 2017 9:15 am

Re: Why doesn't $PSScriptRoot resolve properly in PSS?

Post by DevinL »

I see what you mean now, I've spoken with the team and we're investigating the issue.

We'll get back to you as soon as we have some more information.
DevinL
SAPIEN Technologies, Inc.
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Why doesn't $PSScriptRoot resolve properly in PSS?

Post by davidc »

The Parameter Dialog is displayed before any PowerShell code is executed. This is the reason you don't see the expanded variable values.

Now the script fails because at the time PowerShell evaluates the $PSScriptRoot variable, the script file hasn't been executed yet and therefore $PSScriptRoot's value is an empty string.

To illustrate this, run the following in the console:

.\myscript.ps1 -ComputerName "$PSScriptRoot\file.txt."

You will see that you get the same error.

To get around this chicken before the egg issue, you can do either of the following:

1. Place an actual path there instead of the default value.

or

2. Remove the parameter from the parameter dialog and let PowerShell assign the default value during runtime.
David
SAPIEN Technologies, Inc.
User avatar
david@koppy.com
Posts: 20
Last visit: Fri Jan 11, 2019 8:21 am

Re: Why doesn't $PSScriptRoot resolve properly in PSS?

Post by david@koppy.com »

I know this. It seems to me it would be a minor fix for you to change the default populated in that dialog with the actual path (or relative path, as I've been doing), when it appears. This would be a better solution than telling your customers to use a workaround.

I'm sorry to sound antagonistic but my experience with your support hasn't been great over the years.
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Why doesn't $PSScriptRoot resolve properly in PSS?

Post by davidc »

I'm sorry you were not satisfied with the response. I provided a workaround so you can get pass the issue without having to wait for change on our end.

I will pass along your request to the dev team and thank you for your candid feedback.
David
SAPIEN Technologies, Inc.
User avatar
Alexander Riedel
Posts: 8479
Last visit: Thu Mar 28, 2024 9:29 am
Answers: 19
Been upvoted: 37 times

Re: Why doesn't $PSScriptRoot resolve properly in PSS?

Post by Alexander Riedel »

"I'm sorry to sound antagonistic but my experience with your support hasn't been great over the years."

I am sorry to hear you feel that way. I was looking through your posts and could not find anything that wasn't resolved within days, if not faster.
I am curious to find out where and when you experienced a problem that was not resolved within a reasonable amount of time. Would you mind pointing out what exactly your grievances are?

Thank you for your feedback.
Alexander Riedel
SAPIEN Technologies, Inc.
This topic is 7 years and 3 months 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.