如何从我所在的同一文件夹中调用文件(在脚本中)

Sun*_*une 20 powershell

我想从脚本A调用脚本B而不指定完整路径.我试过了

.\scriptb.ps1
Run Code Online (Sandbox Code Playgroud)

但这不起作用.我真的必须指定整个路径吗?

(对不起这个非常基本的问题,但谷歌无法帮助我!)

tos*_*shi 20

我使用powershell变量,它有一个简单的名称来记住它的用途.

$PSScriptRoot\scriptb.ps1
Run Code Online (Sandbox Code Playgroud)


Tom*_*nik 11

可以使用$MyInvocation如下:

$executingScriptDirectory = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
$scriptPath = Join-Path $executingScriptDirectory "scriptb.ps1"
Invoke-Expression ".\$scriptPath" 
Run Code Online (Sandbox Code Playgroud)

  • 为什么微软要把一切弄得这么复杂? (2认同)

mjo*_*nor 5

这个怎么样:

& $pwd\scriptb.ps1
Run Code Online (Sandbox Code Playgroud)