Multiple dot course functions and relative paths

Welcome Forums General PowerShell Q&A Multiple dot course functions and relative paths

Viewing 3 reply threads
  • Author
    Posts
    • #202661
      Participant
      Topics: 2
      Replies: 3
      Points: 17
      Rank: Member

      Hey all,

      I am dot sourcing a function from an original script which in itself would dot course another function. I am looking for a way to determine the file location of the first dot sourced function so I can create a relative link to the second dot sourced location (relative to the first). The first/second function file should always be in the same directory.

      When running a `Get-Location` within the first dot source file, it seems as if the original script location is referenced. It may be the the calling function isn’t always in the same location, indeed the dot sourced function might not be in the same place, but both function files should be in the same folder.

      I have worked around this by dot sourcing both file in the original script, but this might not always be known.

    • #202667
      Senior Moderator
      Topics: 9
      Replies: 1236
      Points: 4,443
      Helping Hand
      Rank: Community Hero

      Don’t know whether I understood your problem properly, but you might want to look at $PSScriptRoot automatic variable.

    • #202700
      Participant
      Topics: 2
      Replies: 3
      Points: 17
      Rank: Member

      Thanks @kvprasoon, I will looks at this. To clarify (hopefully), lets try this:

      Original Script. say located on a users desktop (C:\users\me\desktop\script.ps1)

      #This will do something, and reference a function in another file...
      . .C:\somepathtoscrits\function1.ps1
      $variable = function1

      Then we have function1.ps1

      #This is function1, which references function2.ps1. Both files should be in the same folder.
      . .\function2.ps1
      $check = function2
      do something...
      return

      Function2 will not be loaded as the location is set to the path of the original script.

    • #202730
      OK
      Participant
      Topics: 9
      Replies: 20
      Points: 122
      Rank: Participant
Viewing 3 reply threads
  • The topic ‘Multiple dot course functions and relative paths’ is closed to new replies.