简体   繁体   English

将参数从一个Powershell脚本传递到另一个

[英]Pass arguments from one powershell script to another

I have one powershell script, like this one: 我有一个powershell脚本,如下所示:

$ErrorActionPreference = "Stop"

$WORKDIR=$args[0]

Write-Host "Num of arguments given: " $args.Count

$AllArguments = ""
for ($i = 1; $i -lt $args.Count; $i += 2) {
    $AllArguments = '$AllArguments $args[$i] "$($args[$i+1])"'
}

Write-Host "AllArguments: $($AllArguments)"

Write-Host "Starting .\someotherscript.ps1 in directory $($WORKDIR)"

pushd "$WORKDIR"

& Powershell -File .\someotherscript.ps1 $AllArguments

popd 

Basically, this powershell script shoud start another powershell script, but without the first argument. 基本上,此powershell脚本应启动另一个powershell脚本,但没有第一个参数。 So, eg when this script is started with .\\firstscript.ps1 C:\\some\\dir -GiveMeParameter param1 , then it should call the other script with the following parameters .\\someotherscript.ps1 -GivMeParameter param1 . 因此,例如,当此脚本以.\\firstscript.ps1 C:\\some\\dir -GiveMeParameter param1 ,则它应使用以下参数.\\someotherscript.ps1 -GivMeParameter param1调用其他脚本。

How to achieve that? 如何实现呢? At the moment I do not know how to solve this problem. 目前,我不知道如何解决这个问题。

You can use multiple assignment to extract the first and remaining arguments by doing 您可以使用多个分配来提取第一个参数和其余参数

$WORKDIR, $RemainingArgs = $args

I'm not sure why you're calling PowerShell again to run the script. 我不确定为什么要再次调用PowerShell来运行脚本。 You can just run the script directly and use splatting to pass the remaining args to the child script. 您可以直接运行脚本,然后使用splatting将其余的args传递给子脚本。

.\someotherscript.ps1 @RemainingArgs 

暂无
暂无

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

相关问题 通过传递参数从另一个PowerShell脚本调用一个PowerShell脚本 - Invoke one PowerShell script from Another PowerShell script with passing arguments 如何将参数或变量从一个 powershell 脚本传递到另一个? - How do I pass arguments or variables from one powershell script to another? 将参数从另一个PowerShell脚本传递给一个PowerShell脚本 - To pass argument to one powershell script from another powershell script 从Powershell脚本传递python脚本的参数 - pass arguments of a python script from a powershell script 如何将用户凭据从一个 Powershell 脚本传递到另一个? - How To Pass User Credentials from One Powershell Script to Another? 如何将开关参数从一个 powershell 脚本传递给另一个? - How to pass switch parameter from one powershell script to another? 使用来自其他PowerShell脚本的参数调用PowerShell脚本 - Call PowerShell script with arguments from another powershell script 将变量值从 AZ Powershell 任务中的一个 Powershell 脚本传递到 Azure DevOps Release Pipeline 中下一个 AZ Powershell 任务中的另一个脚本 - Pass variable value from one Powershell script in AZ Powershell task to another script in the next AZ Powershell task in Azure DevOps Release Pipeline 将变量值从 Powershell 任务中的一个脚本传递到 Azure DevOps 发布管道中下一个 Powershell 任务中的另一个脚本 - Pass variable value from one Powershell script in Powershell task to another script in the next Powershell task in Azure DevOps Release Pipeline 如何将 arguments 传递给从批处理脚本以管理员身份执行的 PowerShell 脚本? - How to pass arguments to PowerShell script executed as administrator from batch script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM