[英]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.