External variables (dot sourced) and Intellisense

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 3 years and 3 days 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
PsCustomObject
Posts: 137
Last visit: Mon Mar 18, 2024 3:11 am

External variables (dot sourced) and Intellisense

Post by PsCustomObject »

Product, version and build: PowerShell Studio 2021 v5.8.187.0, x64
Operating system: Windows 10 20H2 x64
PowerShell version(s): v7.1.2, v5.1

Not sure if this is the right place to post this as more than reporting an issue this is more of a question.

As per subject I'm working on a project requiring me to dotsource an external .ps1 containing variables for the workflow, I'm not a big fan of this for a number of reasons and was wondering if there is a way to make intellisense aware of the variables defined in the external file.

What I am doing is just a standard import at runtime:

Code: Select all

$configFile = '{0}\{1}' -f (Split-Path $script:MyInvocation.MyCommand.Path), 'Config.ps1'

try
{
    # Import configuration file
    . $configFile
}
But of course none of the variables defined in the external config.ps1 file are visible, so to speak, to intellisense.
Thanks in advance for any feedback on this!
User avatar
Alexander Riedel
Posts: 8473
Last visit: Tue Mar 19, 2024 1:15 am
Answers: 19
Been upvoted: 37 times

Re: External variables (dot sourced) and Intellisense

Post by Alexander Riedel »

Using a variable that is determined at runtime for dot sourcing cannot be resolved when editing.
$configfile has no meaning until the code is actually executed.
Generally we advise users to just use
. ./config.ps1
as you seem to require the config file to be in the same folder as the script anyway.
If you package your code as an exe afterwards, you can opt to resolve dot sourced items when packaging.
Again, this has to be an actual path, not a variable.
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
PsCustomObject
Posts: 137
Last visit: Mon Mar 18, 2024 3:11 am

Re: External variables (dot sourced) and Intellisense

Post by PsCustomObject »

Thanks Alexander, I imagined this was the case but was hoping against evidence I missed something ;-)

Script will just be scheduled so won't be packaged to an exe and could be in a different path than the script itself.

I will just keep doing what I did so far, poor man's solution of copying variables "locally" and then removing from main code body before commit.

Thanks again, appreciate you time to explain!
User avatar
Alexander Riedel
Posts: 8473
Last visit: Tue Mar 19, 2024 1:15 am
Answers: 19
Been upvoted: 37 times

Re: External variables (dot sourced) and Intellisense

Post by Alexander Riedel »

You should be able to do this:
  1. if($null -ne $SAPIENHost) {
  2. . "./config.ps1"
  3. }
  4. else {
  5. $configFile = '{0}\{1}' -f (Split-Path $script:MyInvocation.MyCommand.Path), 'Config.ps1'
  6.  
  7. try
  8. {
  9.     # Import configuration file
  10.     . $configFile
  11. }
  12. }
  13.  
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
Alexander Riedel
Posts: 8473
Last visit: Tue Mar 19, 2024 1:15 am
Answers: 19
Been upvoted: 37 times

Re: External variables (dot sourced) and Intellisense

Post by Alexander Riedel »

Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
PsCustomObject
Posts: 137
Last visit: Mon Mar 18, 2024 3:11 am

Re: External variables (dot sourced) and Intellisense

Post by PsCustomObject »

Oh wonderful yesterday I posted an answer but either it got lost or I did not hit the submit button (I think the latter...)

Thanks Alexander, appreciate your answer, I guessed that much but was hoping against hope there was something missed. Script will not be packaged but only scheduled so that's not an option.

Thanks again.
This topic is 3 years and 3 days 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.