如何将参数或变量从一个Powershell脚本传递给另一个?
在cmd中,我可以通过在声明要运行的bat文件之后列出它们来将参数从一个蝙蝠传递到另一个蝙蝠.然后,即将运行的蝙蝠以%1,%2,%3等接收它们.可以在Powershell中完成吗?
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?
我有一个ps1脚本script1,它提示用户输入位置.该脚本已处理完毕.该位置存储为变量; $ loc.在第一个脚本中,有一点是用户可以选择一个选项,该选项将运行另一个具有更多选项的ps1脚本script2.我想将$ loc从script1传递到script2.
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.
在script1中,我尝试了以下操作:
In script1 I have tried the following:
param ($loc)
start-process "\script2.ps1" -ArgumentList $loc
start-process "\script2.ps1" -$loc
start-process "\script2.ps1"
然后是脚本2
args[0]
$loc
我知道我可能只是不了解传递的参数.问题是另一个选择称为蝙蝠脚本.我使用的是-ArgumentList $ loc,它可以通过.我在蝙蝠脚本中使用"Set loc =%1"来获取该参数
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"
您无需Start-Process
即可从另一个PowerShell脚本运行一个PowerShell脚本.只需使用所需的任何参数调用第二个脚本即可:
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:
# script1.ps1
$loc = Read-Host 'Enter location'
C:\path\to\script2.ps1 $loc 'other parameter'
在第二个脚本中,例如可以通过$args
数组访问参数列表:
In the second script the argument list can be accessed for instance via the $args
array:
# 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 validated without 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
如果传递的参数比定义的命名参数多,则这些附加参数将存储在$args
数组中:
If more arguments are passed than named parameters were defined those additional arguments are stored in the $args
array:
PS C:\> cat test.ps1
Param($Foo)
Write-Host $Foo
Write-Host $args[0]
PS C:\> .\test.ps1 'foo' 'bar'
foo
bar
有关更多信息,请参见 Get-Help about_Functions_Advanced_Parameters
.
For more information see Get-Help about_Functions_Advanced_Parameters
.