简体   繁体   English

如何将参数或变量从一个 powershell 脚本传递到另一个?

[英]How do I pass arguments or variables from one powershell script to another?

In cmd I could pass arguments from one bat to another by listing them after the to-be-run bat file was stated.在 cmd 中,我可以通过在声明要运行的 bat 文件后列出它们来将参数从一个 bat 传递到另一个 bat 。 Then the to-be-run bat received them as %1, %2, %3, etc. Can this be done in Powershell?然后要运行的 bat 将它们接收为 %1、%2、%3 等。这可以在 Powershell 中完成吗?

I have one ps1 script,script1, that prompts the user for a location.我有一个 ps1 脚本,script1,它会提示用户输入位置。 That script has been taken care of.该脚本已被处理。 That location is stored as a variable;该位置存储为变量; $loc. $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.在第一个脚本中,有一点是用户可以选择一个选项来运行另一个 ps1 脚本 script2,该脚本本身具有更多选项。 I want to pass $loc to the script2 from script1.我想将 $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.事情是另一个选项调用 bat 脚本。 That one I use -ArgumentList $loc and it passes that fine.我使用的那个 -ArgumentList $loc 并且它通过了。 I pick that argument up in the bat script using "Set loc = %1"我在 bat 脚本中使用“Set loc = %1”选择该参数

You don't need Start-Process to run one PowerShell script from another PowerShell script.您不需要Start-Process从另一个 PowerShell 脚本运行一个 PowerShell 脚本。 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'

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 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

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

The variables declared in Variables.ps1 are at "Script Scope". Variables.ps1 中声明的变量位于“脚本范围”。 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.将 Variables.ps1 中的变量引入 main.ps1 范围的一种方法是“点源”Variables.ps1。 This, in effect, runs Variables.ps1 at the scope of main.ps1.这实际上在 main.ps1 的范围内运行 Variables.ps1。 To do this, just stick a period and space before your invocation of the script:为此,只需在调用脚本之前添加句号和空格:

. .\Variables.ps1
$var1
$var2
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.您可以应用的所有属性,包括在函数中的param语句上使用[CmdletBinding()]也适用于脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将带点的批处理参数传递给powershell脚本? - How can I pass batch arguments with dots into a powershell script? 如何在PowerShell中将参数传递给嵌入式脚本? - How to pass arguments to an inline script in PowerShell? 如何将 arguments 传递给远程 powershell 脚本? - How to pass arguments to a remote powershell script? 如何将.bat文件中的变量导入PowerShell脚本? - How can I source variables from a .bat file into a PowerShell script? 如何在不同的进程中运行PowerShell脚本并将参数传递给它? - How to run PowerShell script in a different process and pass arguments to it? 如何将命令行 arguments 传递给由 doskey 调用的 PowerShell 脚本 - How to pass command line arguments to a PowerShell script invoked by a doskey 如何从Powershell访问经常账户环境变量? - How do I access the current account environment variables from powershell? 如何用bat文件包装exe,以便设置环境变量并仍然传递参数? - How do I wrap an exe with a bat file so I can set environmental variables and still pass arguments? 如何以PowerShell中的参数作为管理员运行python脚本 - How run python script with arguments from powershell as admin 如何将托管对象从一个进程传递到另一个? - How can I pass managed objects from one Process to another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM