简体   繁体   English

将参数从另一个PowerShell脚本传递给一个PowerShell脚本

[英]To pass argument to one powershell script from another powershell script

I have a two powershell scripts a.ps1 & b.ps1. 我有两个PowerShell脚本a.ps1和b.ps1。 Im calling b.ps1 script inside the a.ps1 script. 我在a.ps1脚本中调用b.ps1脚本。 I need to pass two variables as an arguments from a.ps1 to b.ps1 like below. 我需要将两个变量作为参数从a.ps1传递到b.ps1,如下所示。

//a.ps1 Script starts here
 $variable1 ="This is Variable1"
 $variable2 ="This is Variable2"

Note: Im calling b.ps1 script inside this a.ps1 script like below and passing the above variables as an arguments to b.ps1 注意:我在这个a.ps1脚本中调用b.ps1脚本,如下所示,并将上述变量作为参数传递给b.ps1

. C:\filepath\b.ps1 $variable1 $variable2 //Calling b.ps1 script

//b.ps1 contains below code
$getVariable1= $args[0]
$getVariable2= $args[1]
echo $getVariable1

But the above echo command from b.ps1 prints nothing. 但是来自b.ps1的上述echo命令什么都不打印。 What is wrong with above arguments passing? 上述参数传递有什么问题?

There are 2 possible issues: 有两个可能的问题:

  • The line comment prefix is # (instead of // ) 行注释前缀是# (而不是//
  • The execution policies ( especially their defaults ) might prevent your script b.ps1 to be called. 执行策略(尤其是其默认值)可能会阻止调用脚本b.ps1 Details is this MS article ( you would be hinted at this article by powershell after attempting to run a.ps from the command line, though ). 详细信息是这篇MS文章a.ps ,在尝试从命令行运行a.ps之后, a.ps会对此文章进行暗示)。
    The linked article also details how to set the policies properly ( in a nutshell for execution from the command line: Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process 链接的文章还详细说明了如何正确设置策略(简而言之,从命令行Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope ProcessSet-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process

What's with the extra space in the b.ps1 call? b.ps1调用中的额外空间是什么?

Once I corrected that, I get the expect results. 一旦我纠正了这一点,我就得到了预期的结果。 Also, you never need Write-* (echo) if all you are doing is sending to the screen. 此外,如果你所做的只是发送到屏幕,你永远不需要Write- *(echo)。 Output to the screen is the default. 输出到屏幕是默认值。

Lastly, no need for the // for comments here, the # and <##> works just fine on this site, as shown below. 最后,这里不需要// for comments,#和<##>在这个网站上运行正常,如下所示。

Environment 环境

$PSVersionTable

<#
Name                           Value
----                           -----
PSVersion                      5.1.17763.503
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0, 5.0, 5.1.17763.503}
BuildVersion                   10.0.17763.503
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
#>

Get-ExecutionPolicy
RemoteSigned

Content 内容

# a.ps1
'running a.ps1 script, calling b.ps1'
$variable1 = "This is Variable1"
$variable2 = "This is Variable2"

.\b.ps1 $variable1 $variable2

Content 内容

# b.ps1
'running b script'
'Showing output from a.ps1 variables'
$getVariable1= $args[0]
$getVariable2= $args[1]

$getVariable1

Results 结果

 .\a.ps1
running a.ps1 script, calling b.ps1
running b script
Showing output from a.ps1 variables
This is Variable1

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

相关问题 将参数从一个Powershell脚本传递到另一个 - Pass arguments from one powershell script to another 使用来自另一个 Powershell 脚本的参数调用 PowerShell 脚本并绕过 - Call PowerShell script with argument from another Powershell script with bypass 将参数从 Powershell 传递到批处理脚本 - Pass argument from Powershell to Batch 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脚本 - Invoke one PowerShell script from Another PowerShell script with passing arguments 将变量值从 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脚本 - Calling another powershell script from first one 从另一个运行 PowerShell 脚本 - Run a PowerShell script from another one 在PowerShell中将标志从一个脚本移动到另一个脚本 - Move flag from one script to another in PowerShell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM