簡體   English   中英

Powershell更改點源文件的位置

[英]Powershell change location of dot sourced files

我有一個Power Shell Module,它引用了許多PS1文件。

. $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的新手,非常感謝所有幫助和信息。

謝謝

$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"
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM