relative path in Import-Module

前端 未结 3 953
野趣味
野趣味 2020-12-29 18:46

I have directory structure that looks like this:

C:\\TFS\\MasterScript\\Script1.ps1
C:\\TFS\\ChildScript\\Script2.ps1

What i want to do is

相关标签:
3条回答
  • 2020-12-29 19:14

    This worked for me:

    $selfPath = (Get-Item -Path "." -Verbose).FullName
    $dllRelativePath = "........"
    $dllAbsolutePath = Join-Path $selfPath $dllRelativePath
    Import-Module $dllAbsolutePath
    
    0 讨论(0)
  • 2020-12-29 19:31

    When you use a relative path, it is based off the currently location (obtained via Get-Location) and not the location of the script. Try this instead:

    $ScriptDir = Split-Path -parent $MyInvocation.MyCommand.Path
    Import-Module $ScriptDir\..\MasterScript\Script.ps1
    

    In PowerShell v3, you can use the automatic variable $PSScriptRoot in scripts to simplify this to:

    # PowerShell v3 or higher
    
    #requires -Version 3.0
    Import-Module $PSScriptRoot\..\MasterScript\Script.ps1
    
    0 讨论(0)
  • 2020-12-29 19:31

    The new Method for this is $PSScriptRoot

    Import-Module $PSScriptRoot\Script1.ps1
    

    Nice little one liner.

    0 讨论(0)
提交回复
热议问题