如何在PowerShell脚本中使用参数来处理相对路径。

huangapple go评论66阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年6月29日 02:20:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76575773.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定