windows 如何将参数或变量从一个 powershell 脚本传递到另一个?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26731933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I pass arguments or variables from one powershell script to another?
提问by user3377627
In cmd I could pass arguments from one bat to another by listing them after the to-be-run bat file was stated. Then the to-be-run bat received them as %1, %2, %3, etc. Can this be done in Powershell?
在 cmd 中,我可以通过在声明要运行的 bat 文件后列出它们来将参数从一个 bat 传递到另一个 bat 。然后要运行的 bat 将它们接收为 %1、%2、%3 等。这可以在 Powershell 中完成吗?
I have one ps1 script,script1, that prompts the user for a location. That script has been taken care of. That location is stored as a variable; $loc. In the first script, there is a point that the user can choose an option that will run another ps1 script, script2, that has itself more options. I want to pass $loc to the script2 from script1.
我有一个 ps1 脚本,script1,它会提示用户输入位置。该脚本已被处理。该位置存储为变量;$loc。在第一个脚本中,有一点是用户可以选择一个选项来运行另一个 ps1 脚本 script2,该脚本本身具有更多选项。我想将 $loc 从 script1 传递给 script2。
In script1 I have tried the following:
在 script1 中,我尝试了以下操作:
param ($loc)
start-process "\script2.ps1" -ArgumentList $loc
start-process "\script2.ps1" -$loc
start-process "\script2.ps1"
Then is script 2
然后是脚本2
args[0]
$loc
I know I'm probably just not understanding passing arguments. Thing is that another options calls a bat script. That one I use -ArgumentList $loc and it passes that fine. I pick that argument up in the bat script using "Set loc = %1"
我知道我可能只是不理解传递参数。事情是另一个选项调用 bat 脚本。我使用的那个 -ArgumentList $loc 并且它通过了。我在 bat 脚本中使用“Set loc = %1”选择该参数
回答by Ansgar Wiechers
You don't need Start-Process
to run one PowerShell script from another PowerShell script. Simply call the second script with whatever parameters you want:
您不需要Start-Process
从另一个 PowerShell 脚本运行一个 PowerShell 脚本。只需使用您想要的任何参数调用第二个脚本:
# script1.ps1
$loc = Read-Host 'Enter location'
C:\path\to\script2.ps1 $loc 'other parameter'
In the second script the argument list can be accessed for instance via the $args
array:
在第二个脚本中,可以通过$args
数组访问参数列表:
# script2.ps1
Write-Host $args[0]
Write-Host $args[1]
You could also define named parameters like this:
您还可以像这样定义命名参数:
# script2.ps1
Param($Location, $Foo)
Write-Host $Location
Write-Host $Foo
or (more complete) like this:
或(更完整)像这样:
# script2.ps1
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[string]$Location,
[Parameter(Mandatory=$false)]
[string]$Foo
)
Write-Host $Location
Write-Host $Foo
Defining named parameters allows you to pass arguments without having to worry about their order:
定义命名参数允许您传递参数而不必担心它们的顺序:
C:\path\to\script2.ps1 -Foo 'other parameter' -Location $loc
or to have parameters automatically validatedwithout having to implement the checks in the function body:
或者让参数自动验证而不必在函数体中实现检查:
# script2.ps1
Param(
[ValidateSet('a', 'b', 'c')]
[string]$Location,
[ValidatePattern('^[a-z]+$')]
[string]$Foo
)
Write-Host $Location
Write-Host $Foo
If more arguments are passed than named parameters were defined those additional arguments are stored in the $args
array:
如果传递的参数多于定义的命名参数,则这些附加参数将存储在$args
数组中:
PS C:\> cat test.ps1 Param($Foo) Write-Host $Foo Write-Host $args[0] PS C:\> .\test.ps1 'foo' 'bar' foo bar
For more information see Get-Help about_Functions_Advanced_Parameters
.
有关更多信息,请参阅Get-Help about_Functions_Advanced_Parameters
。
回答by Richard
param ($loc)
param ($loc)
At the top of a .ps1
script that defines a parameter for the script, so call it as
在.ps1
定义脚本参数的脚本顶部,因此将其称为
PathToMyScript\MyScript.ps1 -loc ValueOfLoc
All the attributes you can apply, including using [CmdletBinding()]
on the param
statement in a function work on a script as well.
回答by Naha
The variables declared in Variables.ps1 are at "Script Scope". That is you can not see them outside of the scope of the script that declares them. One way to bring the variables in Variables.ps1 to the scope of main.ps1 is to "dot source" Variables.ps1. This, in effect, runs Variables.ps1 at the scope of main.ps1. To do this, just stick a period and space before your invocation of the script:
Variables.ps1 中声明的变量位于“脚本范围”。也就是说,您无法在声明它们的脚本范围之外看到它们。将 Variables.ps1 中的变量引入 main.ps1 范围的一种方法是“点源”Variables.ps1。这实际上在 main.ps1 的范围内运行 Variables.ps1。为此,只需在调用脚本之前添加句号和空格:
. .\Variables.ps1
$var1
$var2