Reputation: 130
I got two Powershell script. The first, script1.ps1, got some Global string variable.
The second, script2.ps1, must do some operation with values of the string variables, then close without passing any data to the first.
Here pieces of code.
script1.ps1
#[ENG] Global $language contains the language name: EN-US for american english, EN-GB for english of Great Britain, for example.
$global:language = $culture.Name
#[ENG] Global $workPath variable contains the path of the folder with the original files.
$global:workPath = "D:\Documents\Downloads\Emule-Incoming\osis"
#[ENG] Global $scriptPath variable contains the path of the script(s) files.
$local:temp = Split-Path $PSCommandPath
$global:scriptPath = "$temp"
$temp = ""
##########
# mods.d #
##########
#[ENG] Global $modBip variable contains the path of destination of CONF files for BPBiblePortable.
$global:modBip = "D:\Documents\Downloads\Emule-Incoming\BPBiblePortable\App\BPBible\resources\mods.d\"
#[ENG] Global $modXip contains the path of destination of CONF files for xiphos.
$global:modXip = "C:\Users\Emanuele\AppData\Roaming\Sword\mods.d\"
#[ENG] Global $modGit variable contains the path of destination of CONF files in GitHub Desktop to upload and synchronize in GitHub.
$global:modGit = "D:\Documents\GitHub\EmanueleTinari\mods.d\"
$scriptPath = $scriptPath + "\" + 'cei1974.ps1 $language $workPath $scriptPath $modBiP $modXip $modGit'
Invoke-Expression & $scriptPath
script2.ps1
#[ENG] Retrieve Global variable's values.
Param ([string]$language, [string]$workPath, [string]$scriptPath, [string]$modBiP, [string]$modXip, [string]$modGit)
Write-Host $language
Write-Host $workPath
Write-Host $scriptPath
Write-Host $modBiP
Write-Host $modXip
Write-Host $modGit
What I try:
$scriptPath = $scriptPath + "\" + 'cei1974.ps1 -$language $language -$workPath $workPath -$scriptPath $scriptPath -$modBiP $modBiP -$modXip $modXip -$modGit $modGit'
Invoke-Expression & $scriptPath
I add -$[string name] : nothing to do.
$scriptPath = $scriptPath + "\" + 'cei1974.ps1 -$language $language -$workPath $workPath -$scriptPath $scriptPath -$modBiP $modBiP -$modXip $modXip -$modGit $modGit'
Invoke-Expression $scriptPath
Without & near $scriptPath : nothing to do.
I, first write this question, examined and tryed answers in this posts:
Run a PowerShell script from another one
PowerShell Script call out from another PowerShell Script
Invoke powershell script from another
Passing varibale into powershell script that is being executed from another script
How can I achieve to pass strings between this two script? Tnx in advance.
Upvotes: 0
Views: 1187
Reputation: 1456
Swap-out the last 2 lines in your script1.ps1
script with this:
. $scriptPath\script2.ps1 -language $language -workPath $workPath -scriptPath $scriptPath -modBiP $modBiP -modXip $modXip -modGit $modGit
Some notes:
Invoke-Expression
followed by the ampersand (&) call operator, as that is unnecessary.-language $language
.For more information, see Get-Help about_Operators
and Get-Help about_Scripts
and Get-Help about_Parameters
Upvotes: 0