简体   繁体   English

如何使用相对路径调用另一个 PowerShell 脚本?

[英]How do I call another PowerShell script with a relative path?

I have the following directory tree:我有以下目录树:

e:\powershell\services\This-Script-Here-Should-Call-Press any key to continue.ps1
e:\powershell\utils\Press any key to continue.ps1

and now I'd like to call a script named "Press any key to continue.ps1" that sits in the "utils"-folder from a script that I have in the "services"-folder.现在我想从“services”文件夹中的脚本中调用位于“utils”文件夹中的名为“Press any key to continue.ps1”的脚本。 How do I do that?我怎么做? I cannot figure out the relative path.我无法弄清楚相对路径。

I tried to do it this way:我试图这样做:

"$ '.\..\utils\Press any key to continue.ps1'"

but it did not work.但它没有用。

Based on what you were doing, the following should work:根据您的操作,以下应该有效:

& "..\utils\Press any key to continue.ps1"

or要么

. "..\utils\Press any key to continue.ps1"

(lookup the difference between using & and . and decide which one to use) (查找使用&.之间的区别并决定使用哪个)

This is how I handle such situations ( and slight variation to what @Shay mentioned):这就是我处理这种情况的方式(与@Shay 提到的略有不同):

$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$utilsDir  = Join-Path -Path $scriptDir -ChildPath ..\utils

& "$utilsDir\Press any key to continue.ps1"

Put the following function in the calling script to get its directory path and join the utils path along with the script name:将以下函数放在调用脚本中以获取其目录路径并将 utils 路径与脚本名称一起加入:

# create this function in the calling script
function Get-ScriptDirectory { Split-Path $MyInvocation.ScriptName }

# generate the path to the script in the utils directory:
$script = Join-Path (Get-ScriptDirectory) 'utils\Press any key to continue.ps1'

# execute the script
& $script 

To get the current script path $PSScriptRoot variable can be used.要获取当前脚本路径,可以使用 $PSScriptRoot 变量。 for example Following is the structure: solution\\mainscript.ps1 solution\\secondscriptfolder\\secondscript.ps1例如以下是结构:solution\\mainscript.ps1 solution\\secondscriptfolder\\secondscript.ps1

#mainscript.ps1

$Second = Join-Path $PSScriptRoot '\secondscriptfolder\secondscript.ps1'

$Second

Thank you very informative.谢谢你提供了很多信息。 I had similar issue, I could get it work like this, to call other scripts from parentfolder.我有类似的问题,我可以让它像这样工作,从父文件夹调用其他脚本。

$CommandPath = (Get-Location).Path | Split-Path -Parent; $script = "$CommandPath\Logins\Login_SSI_SST_exch2010_DKSUND_Exchange365.ps1"; & $script

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用相对和绝对路径在 powershell 脚本中调用文件 - Call file in powershell script using relative and absolute path 如何解析powershell脚本中相对或绝对文件名输入参数的绝对路径? - How I resolve the absolute path of a relative or absolute file name input parameter in a powershell script? 如何在Powershell脚本中使用UNC路径? - How do I use a UNC path in a powershell script? 如何从相对路径在C#中运行PowerShell脚本? - How to run PowerShell script in C# from relative path? 我想制作一个Powershell脚本,该脚本调用另一个Powershell脚本 - I want to make a powershell script that call another powershell script 如何根据 PowerShell 中的相对路径或绝对路径获取绝对路径? - How do I get the absolute path based on either relative or absolute paths in PowerShell? 如何使用参数将Powershell脚本调用到另一个Powershell脚本? - How to call a powershell script to another powershell script with arguments? 从命令提示符相对路径调用Powershell - Call Powershell from Command Prompt Relative Path 具有相对路径的PowerShell脚本的Windows快捷方式 - Windows shortcut to a PowerShell script with relative path 如何给出打算在 Jenkins Powershell 插件中执行的 Powershell 脚本的相对路径 - How to give relative path of Powershell script that is intended to be executed in Jenkins Powershell plugin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM