[英]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: 有两个可能的问题:
#
(instead of //
) #
(而不是//
) 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 ). a.ps
,在尝试从命令行运行a.ps
之后, a.ps
会对此文章进行暗示)。 Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process
: Set-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.