英文:
how to dot the relative path with arguments in powershell script
问题
I have a header.ps1 file that needs to be called from the Main.ps1 script. They are both under the same directory.
In the header.ps1 file, it starts off with the following...
[CmdletBinding()]
param(
[String]$PROJECTNAME = 'PSToolBox',
[string]$SCRIPTNAME = 'accountSelector.ps1'
)
In the Main.ps1 file, the following does work, except I cannot use the header.ps1 parameters.
This works:
. ($PSScriptRoot + '\header.ps1')
This does NOT work:
. (($PSScriptRoot + '\header.ps1') -PROJECTNAME 'LunchBox' -SCRIPTNAME 'MyLunch.ps1')
So, how to dot-source the relative path with passing the parameters in PowerShell script?
英文:
I have a header.ps1 file that need to be called from the Main.ps1 script. They are both under the same directory.
In the header.ps1 file, it started off with following...
[CmdletBinding()]
param(
[String]$PROJECTNAME = 'PSToolBox',
[string]$SCRIPTNAME = 'accountSelector.ps1'
)
In the Main.ps1 file, the following does work except I cannot use header.ps1 parameters.
This works:
. ($PSScriptRoot + '\header.ps1')
This does NOT work:
. (($PSScriptRoot + '\header.ps1') -PROJECTNAME 'LunchBox' -SCRIPTNAME 'MyLunch.ps1')
So, how to dot the relative path with passing the parameters in powershell script?
答案1
得分: 4
你需要将命令名称/路径与参数参数分开传递(这不仅适用于“.”,对于使用“&”进行调用也是如此):
. ($PSScriptRoot + '\header.ps1') -PROJECTNAME 'LunchBox' -SCRIPTNAME 'MyLunch.ps1'
# \_____________________________/ \_______________________________________________/
# 首先是命令名称 ... 然后是参数
英文:
You need to pass the command name/path separately from the parameter arguments (this applies not only to .
, the same goes for invocations with &
):
. ($PSScriptRoot + '\header.ps1') -PROJECTNAME 'LunchBox' -SCRIPTNAME 'MyLunch.ps1'
# \_____________________________/ \_______________________________________________/
# first the command ... and then the arguments
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论