Emanuele Tinari
Emanuele Tinari

Reputation: 130

Run PowerShell script from another script, passing string variables

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

Answers (1)

leeharvey1
leeharvey1

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:

  • Do not use Invoke-Expression followed by the ampersand (&) call operator, as that is unnecessary.
  • You have all the information necessary to call script2.ps1 directly, rather than build-up a string and rely on expression evaluation.
  • When specifying script command-line parameters by name, simply use a hyphen (-) followed by the parameter name. For example, -language $language.
  • I believe you want to use dot-sourcing (.) to call your script2.ps1. You could also defer to using the ampersand (&) call operator. Invoke-Command and Invoke-Expression could be used, but can also be exploited if not handled correctly.

For more information, see Get-Help about_Operators and Get-Help about_Scripts and Get-Help about_Parameters

Upvotes: 0

Related Questions