![](/img/trans.png)
[英]How to run a Powershell script from the command line and pass a directory as a parameter
[英]Pass multiple parameter to Powershell function from command line?
我正在編輯腳本函數以添加一個名為CloneGitPackage
的函數。 這是我寫的函數:
function CloneGitPackage {
$PACKAGE_URL = $args[1]
$PACKAGE_NAME = $args[2]
write-verbose "Downloading package: $PACKAGE_URL $PACKAGE_NAME"
git clone --depth 1 $PACKAGE_URL $PACKAGE_NAME 2>$null
write-verbose ""
}
但是原始腳本還有其他一些功能:
[CmdletBinding()]
param(
[Parameter(Mandatory = $false, Position = 0)]
[string]$command,
[Parameter(Mandatory = $false)]
[switch] $coverage
)
...
try{
switch ($command){
"bootstrap" { Bootstrap }
"install" { Install }
"run_tests" { RunTests }
"clone_git_package" { CloneGitPackage }
}
}catch {
throw $_
}
我這樣調用腳本:
.\script.ps1 "clone_git_package" "https://github.com" "Folder" -verbose
但它在抱怨:
script.ps1 : A positional parameter cannot be found that accepts argument 'https://github.com'.
At line:1 char:19
+ ... .\script.ps1 "clone_git_package" "https://github.com/ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [script.ps1], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,script.ps1
Command executed with exception: A positional parameter cannot be found that accepts argument 'https://github.com'.
我以為問題是原始腳本已經有陌生人頭了:
[CmdletBinding()]
param(
[Parameter(Mandatory = $false, Position = 0)]
[string]$command,
[Parameter(Mandatory = $false)]
[switch] $coverage
)
這些是相應於其標頭的對腳本的一些有效調用:
.\\script.ps1 "bootstrap" -verbose
.\\script.ps1 "install" -verbose
.\\script.ps1 "run_tests" -coverage -verbose
我需要能夠通過新函數名稱調用腳本,在這種情況下為clone_git_package
並提供其參數,字符串git URL
和另一個字符串directory name
。
我該如何解決此問題,而又不破壞與已經使用此腳本的腳本上其他內容的向后兼容性?
為了能夠將參數傳遞給函數,您將需要更新腳本的參數以適應您的其他參數。 因此,讓我們從函數上的參數開始。 現在您參考:
$PACKAGE_URL = $args[1]
$PACKAGE_NAME = $args[2]
這不是特別好的腳本。 更好的選擇是為函數創建實際參數:
function CloneGitPackage {
Param(
$PACKAGE_URL,
$PACKAGE_NAME
)
基本上,這與您之前所做的相同,因為參數的位置基於其列出的順序。 我們可以做得更好,但實際上沒有必要。 因此,如果我們將相同的邏輯應用於腳本的參數,它就會像這樣(將開關保持在底部,使其成為最后一個期望的參數):
param(
[Parameter(Mandatory = $false, Position = 0)]
[string]$command,
$PACKAGE_URL,
$PACKAGE_NAME,
[Parameter(Mandatory = $false)]
[switch] $coverage
)
現在,當您向腳本提供其他信息時,該腳本便知道如何處理您的參數! 之后,您只需要更新腳本調用函數的方式,以在調用函數時包括那些參數,就可以了。
switch ($command){
"bootstrap" { Bootstrap }
"install" { Install }
"run_tests" { RunTests }
"clone_git_package" { CloneGitPackage $PACKAGE_URL $PACKAGE_NAME }
}
我做到了,它似乎運行良好:
[CmdletBinding()]
param(
[Parameter(Mandatory = $false, Position = 0)]
[string]$command,
[Parameter(Mandatory = $false, Position = 1)]
[string]$package_url,
[Parameter(Mandatory = $false, Position = 2)]
[string]$package_name,
[Parameter(Mandatory = $false)]
[switch] $coverage
)
...
function CloneGitPackage {
$PACKAGE_PATH = "$TARGET_FOLDER\$package_name"
write-verbose "Downloading package: $package_url $PACKAGE_PATH"
git clone --depth 1 $package_url $PACKAGE_PATH 2>$null
write-verbose ""
}
try{
switch ($command){
"bootstrap" { Bootstrap }
"install" { Install }
"run_tests" { RunTests }
"clone_git_package" { CloneGitPackage }
}
}catch {
throw $_
}
以以下方式調用時:
.\\script.ps1 "bootstrap" -verbose
.\\script.ps1 "install" -verbose
.\\script.ps1 "run_tests" -coverage -verbose
.\\script.ps1 "clone_git_package" "https://github.com" "Folder" -verbose
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.