[英]To pass argument to one powershell script from another powershell script
我有兩個PowerShell腳本a.ps1和b.ps1。 我在a.ps1腳本中調用b.ps1腳本。 我需要將兩個變量作為參數從a.ps1傳遞到b.ps1,如下所示。
//a.ps1 Script starts here
$variable1 ="This is Variable1"
$variable2 ="This is Variable2"
注意:我在這個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
但是來自b.ps1的上述echo命令什么都不打印。 上述參數傳遞有什么問題?
有兩個可能的問題:
#
(而不是//
) b.ps1
。 詳細信息是這篇MS文章 ( a.ps
,在嘗試從命令行運行a.ps
之后, a.ps
會對此文章進行暗示)。 Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process
: Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process
b.ps1調用中的額外空間是什么?
一旦我糾正了這一點,我就得到了預期的結果。 此外,如果你所做的只是發送到屏幕,你永遠不需要Write- *(echo)。 輸出到屏幕是默認值。
最后,這里不需要// for comments,#和<##>在這個網站上運行正常,如下所示。
環境
$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
內容
# a.ps1
'running a.ps1 script, calling b.ps1'
$variable1 = "This is Variable1"
$variable2 = "This is Variable2"
.\b.ps1 $variable1 $variable2
內容
# b.ps1
'running b script'
'Showing output from a.ps1 variables'
$getVariable1= $args[0]
$getVariable2= $args[1]
$getVariable1
結果
.\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.