I have a two powershell scripts a.ps1 & b.ps1. Im calling b.ps1 script inside the a.ps1 script. I need to pass two variables as an arguments from a.ps1 to b.ps1 like below.
//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
. 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. What is wrong with above arguments passing?
There are 2 possible issues:
#
(instead of //
) b.ps1
to be called. 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 ). Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process
What's with the extra space in the b.ps1 call?
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. 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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.