powershell - 使用脚本中的参数调用第二个脚本

标签 powershell parameter-passing command-line-arguments invoke-command start-job

我有一个脚本,它读取一个配置文件,该文件会生成一组名称值对,我希望将其作为参数传递给第二个 PowerShell 脚本中的函数。

我不知道在设计时将在该配置文件中放置哪些参数,因此在我需要调用第二个 PowerShell 脚本时,我基本上只有一个变量具有第二个脚本的路径,第二个变量是传递给路径变量中标识的脚本的参数数组。

因此,包含第二个脚本路径的变量 ($scriptPath) 可能具有如下值:

"c:\the\path\to\the\second\script.ps1"

包含参数的变量 ($argumentList) 可能类似于:

-ConfigFilename "doohickey.txt" -RootDirectory "c:\some\kind\of\path" -Max 11

如何从这种状态转到使用 $argumentList 中的所有参数执行 script.ps1?

我希望第二个脚本中的任何写入主机命令对于调用第一个脚本的控制台可见。

我尝试过点采购、Invoke-Command、Invoke-Expression 和 Start-Job,但还没有找到一种不会产生错误的方法。

例如,我认为最简单的第一个路线是尝试调用 Start-Job,如下所示:

Start-Job -FilePath $scriptPath -ArgumentList $argumentList

...但是失败并出现以下错误:

System.Management.Automation.ValidationMetadataException:
Attribute cannot be added because it would cause the variable
ConfigFilename with value -ConfigFilename to become invalid.

...在这种情况下,“ConfigFilename”是第二个脚本定义的参数列表中的第一个参数,我的调用显然是试图将其值设置为“-ConfigFilename”,这显然是为了识别按名称参数,而不设置其值。

我错过了什么?

编辑:

好的,这是要调用的脚本的模型,位于名为 invokee.ps1 的文件中

Param(
[parameter(Mandatory=$true)]
[alias("rc")]
[string]
[ValidateScript( {Test-Path $_ -PathType Leaf} )]
$ConfigurationFilename,
[alias("e")]
[switch]
$Evaluate,
[array]
[Parameter(ValueFromRemainingArguments=$true)]
$remaining)

function sayHelloWorld()
{
    Write-Host "Hello, everybody, the config file is <$ConfigurationFilename>."
    if ($ExitOnErrors)
    {
        Write-Host "I should mention that I was told to evaluate things."
    }
    Write-Host "I currently live here: $gScriptDirectory"
    Write-Host "My remaining arguments are: $remaining"
    Set-Content .\hello.world.txt "It worked"
}

$gScriptPath = $MyInvocation.MyCommand.Path
$gScriptDirectory = (Split-Path $gScriptPath -Parent)
sayHelloWorld

...这是调用脚本的模型,位于名为 invoker.ps1 的文件中:

function pokeTheInvokee()
{
    $scriptPath = (Join-Path -Path "." -ChildPath "invokee.ps1")
    $scriptPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($scriptPath)
    
    $configPath = (Join-Path -Path "." -ChildPath "invoker.ps1")
    $configPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($configPath)
    
    $argumentList = @()
    $argumentList += ("-ConfigurationFilename", "`"$configPath`"")
    $argumentList += , "-Evaluate"

    Write-Host "Attempting to invoke-expression with: `"$scriptPath`" $argumentList"
    Invoke-Expression "`"$scriptPath`" $argumentList"
    Invoke-Expression ".\invokee.ps1 -ConfigurationFilename `".\invoker.ps1`" -Evaluate
    Write-Host "Invokee invoked."
}

pokeTheInvokee

当我运行 invoker.ps1 时,这是我当前在第一次调用 Invoke-Expression 时遇到的错误:

Invoke-Expression : You must provide a value expression on
the right-hand side of the '-' operator.

第二个调用工作得很好,但一个显着的区别是第一个版本使用的参数的路径中包含空格,而第二个版本则没有。我是否错误地处理了这些路径中存在的空格?

最佳答案

啊哈。事实证明,这是一个简单的问题,脚本路径中存在空格。

将 Invoke-Expression 行更改为:

Invoke-Expression "& `"$scriptPath`" $argumentList"

...足以让它开始。感谢 Neolisk 的帮助和反馈!

关于powershell - 使用脚本中的参数调用第二个脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12850487/

相关文章:

c - 将值传递给 C 上的 typedef 结构

java - 调用方法中的数组被调用方法数组覆盖,没有任何返回语句

r - 如何在命令提示符下获取用户输入并将其传递给 R

c# - 从 c# 调用 powershell 函数的问题

PowerShell 打开 gzip 流

windows - 如何使用硬编码密码修复 PowerShell 中 WinSCP SFTP 脚本中的安全性

azure - Connect-AzAccount Powershell 本地部署

python - 如何将接收参数的方法作为Python中另一个函数的参数传递

Scala 参数解析

java - args4j:如果给出/未给出另一个参数,如何使参数成为必需的?