【发布时间】:2018-10-04 01:00:46
【问题描述】:
我有一个调用文件的 .ps1,如果它不能调用文件,它将在本地查找这些文件。我想提供一个选项,作为参数,在本地工作或从 Internet 获取,并指定要使用或调用 5 个文件中的哪一个。我使脚本与“本地”和“外部”函数一起工作,但我如何也向这些函数添加参数?
例如:
./script.ps1 -local file1,file2,file3
或
./script.ps1 -external file4,file5
这是我目前的代码:
Param(
[Parameter(Position=1)][string]$option
)
function RunLocal {
Write-Host "local"
}
function RunExternal {
Write-Host "ext"
}
function RunDefault {
Write-Host "default"
}
switch ($option) {
local { RunLocal }
external { RunExternal }
default { RunDefault }
}
【问题讨论】:
-
你可以试试
./script.ps1 -option "local" file1,file2,file3,$option会取值"local"。
标签: powershell parameter-passing powershell-2.0 powershell-3.0