【问题标题】:Powershell relative path to repo file回购文件的Powershell相对路径
【发布时间】:2017-01-07 01:36:23
【问题描述】:

我正在尝试引用与 powershell 脚本位于同一存储库中的文件的相对路径。

我有以下目录结构。

repo/
 ├── files
 │   └── scripts.sh
 │   
 ├── powershell_scripts
      └── my_powershell.ps1

my_powershell.ps1 我想推荐scripts.sh。我尝试在 powershell 脚本中使用$PSScriptRoot,但它没有按我想要的方式工作。

我希望my_powershell.ps1 接收带有目录结构的scripts.sh

有人知道这是怎么做的吗?

谢谢

【问题讨论】:

  • 我看到您将 PSv2 列为标签。你真的要在版本 2 上运行它吗?
  • 在 powershell-v3.0 上运行

标签: powershell automation powershell-3.0


【解决方案1】:

好的,所以在 v3 或更高版本中,这很简单。如果您想要“Repo”文件夹的路径,您可以简单地引用当前文件夹的父文件夹,例如:

$Repo = Resolve-Path "$PSScriptRoot\.."

这将为您获取 Repo 的文件夹对象。从那里你可以进入你的脚本文件夹。

或者直接引用scripts.sh文件你可以这样做:

Resolve-Path "$PSScriptRoot\..\files\scripts.sh"

【讨论】:

  • 对不起,我没有得到这个,可以指定我需要使用的完整命令将scripts.sh引用到my_powershell.ps1
  • 我尝试了$Repo = Resolve-Path "$PSScriptRoot\.." 然后Write-host $Repo 打印了`C:`
  • 如何将scripts.sh 转为my_powershell.ps1?试试Get-Content "$PSScriptRoot\..\files\scripts.sh"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-25
  • 2021-12-02
  • 1970-01-01
  • 2020-06-28
  • 1970-01-01
  • 1970-01-01
  • 2014-07-31
相关资源
最近更新 更多