从另一个脚本运行PowerShell脚本,并传递字符串变量

从另一个脚本运行PowerShell脚本,并传递字符串变量,powershell,Powershell,我有两个Powershell脚本。 第一个,script1.ps1,得到了一些全局字符串变量 第二个脚本script2.ps1必须对字符串变量的值执行一些操作,然后在不向第一个脚本传递任何数据的情况下关闭 这里是一些代码 script1.ps1 #[ENG] Global $language contains the language name: EN-US for american english, EN-GB for english of Great Britain, for example

我有两个Powershell脚本。 第一个,script1.ps1,得到了一些全局字符串变量

第二个脚本script2.ps1必须对字符串变量的值执行一些操作,然后在不向第一个脚本传递任何数据的情况下关闭

这里是一些代码

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

#[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

script2.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

#[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

我尝试的是:


$scriptPath = $scriptPath + "\" + 'cei1974.ps1 -$language $language -$workPath $workPath -$scriptPath $scriptPath -$modBiP $modBiP -$modXip $modXip -$modGit $modGit'
Invoke-Expression & $scriptPath

我添加-$[string name]:无需执行任何操作

$scriptPath = $scriptPath + "\" + 'cei1974.ps1 -$language $language -$workPath $workPath -$scriptPath $scriptPath -$modBiP $modBiP -$modXip $modXip -$modGit $modGit'
Invoke-Expression $scriptPath

不带$scriptPath(&N):无需执行任何操作

$scriptPath = $scriptPath + "\" + 'cei1974.ps1 -$language $language -$workPath $workPath -$scriptPath $scriptPath -$modBiP $modBiP -$modXip $modXip -$modGit $modGit'
Invoke-Expression $scriptPath

一、 首先写下这个问题,在这篇文章中检查并尝试回答:


如何在这两个脚本之间传递字符串?提前使用Tnx。

script1.ps1
脚本中的最后两行替换为以下内容:

. $scriptPath\script2.ps1 -language $language -workPath $workPath -scriptPath $scriptPath -modBiP $modBiP -modXip $modXip -modGit $modGit
一些注意事项:

  • 不要使用
    Invoke Expression
    后跟与(&)符的call运算符,因为这是不必要的
  • 您拥有直接调用script2.ps1所需的所有信息,而不是构建字符串并依赖表达式计算
  • 按名称指定脚本命令行参数时,只需在参数名称后使用连字符(-)。例如,
    -language$language
  • 我相信您希望使用点源(.)来调用script2.ps1。您也可以推迟使用符号(&)呼叫接线员。可以使用Invoke命令和Invoke表达式,但如果处理不正确,也可以利用它们

有关更多信息,请参见
获取有关\u运算符的帮助
获取关于\u脚本的帮助
获取关于\u参数的帮助

您是否查看了dot寻源?这样,您就可以从第一个脚本访问变量。