【问题标题】:Powershell change location of dot sourced filesPowershell更改点源文件的位置
【发布时间】:2015-01-10 20:23:16
【问题描述】:

我有一个引用了许多 PS1 文件的 Power Shell 模块。

. $PSScriptRoot\vs-command.ps1
. $PSScriptRoot\open.ps1
. $PSScriptRoot\git.ps1
. $PSScriptRoot\build.ps1
. $PSScriptRoot\deploy.ps1
. $PSScriptRoot\count.ps1
. $PSScriptRoot\tab.ps1
. $PSScriptRoot\test.ps1
. $PSScriptRoot\nuget.ps1
. $PSScriptRoot\checkforxml.ps1

此 PSM1 文件保存在 Documents\WindowsPowerShell\Modules 的位置,目前与上面引用的 PS1 文件相关联。

上面的文件对 Visual Studio 项目\解决方案执行了许多操作,我现在要求将每个文件保存在解决方案文件夹中自己的文件夹中。

我需要找到一种在打开同一个 PowerShell 窗口时切换 $PSScriptRoot 的方法。

所以......要求是......这些文件的初始加载,所以我有我期望的基本命令......然后......当我切换到保存在 d:\slns 中的解决方案文件夹时将检测到根目录已更改,然后拾取新的 ps1 文件。 .

我对 powershell 非常陌生,因此非常感谢所有帮助和信息。

谢谢

【问题讨论】:

    标签: powershell


    【解决方案1】:

    $PSScriptRoot 是一个自动变量,其中包含启动给定 PowerShell 脚本的文件夹的路径,即您的脚本正在从其自己的父文件夹中获取其他 .ps1 文件。

    如果您需要从不同位置获取文件,则需要将该变量替换为每个文件的相应路径。

    我仍然不确定我是否理解您的问题,但也许可以这样做:

    switch -wildcard ($PSScriptRoot) {
      '*\somefolder\*' {
        . "$PSScriptRoot\A.ps1"
        . "$PSScriptRoot\..\..\C.ps1"
      }
      '*\otherfolder\*' {
        . "$PSScriptRoot\B.ps1"
        . 'C:\some\where\else\D.ps1'
      }
      default {
        . "$PSScriptRoot\A.ps1"
        . "$PSScriptRoot\B.ps1"
      }
    }
    

    【讨论】:

    • 目前的计划是尝试切换 powershell 环境...以便在任何点源信息或变量中都不会留下多余的东西...
    • @SimonPrice 对不起,我不知道你想告诉我什么。请解释(在您的问题中)您想要的结果是什么样的。示例可能会有所帮助。
    • 所以...我有许多 Visual Studio 解决方案文件,每个解决方案文件夹中都有一个包含 psm1 和各种 ps1 文件的 powershell 模块文件夹。当我启动 powershell 时,它为我提供了上面提到的默认模块和 ps1 文件。并非所有解决方案都将被平等地创建,并且并非所有 psm1 和 ps1 文件都是相同的......所以......我需要一种切换到正确 psm1 和 ps1 文件的方法,具体取决于我所在的解决方案文件夹 \ root从而删除所有当前点源信息并确保声明变量没有悬垂
    猜你喜欢
    • 2018-02-15
    • 2010-09-06
    • 2014-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-12
    • 1970-01-01
    相关资源
    最近更新 更多