使用脚本中的参数调用第二个脚本 [英] Invoke a second script with arguments from a script

查看:72
本文介绍了使用脚本中的参数调用第二个脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a script that reads a configuration file that results in a set of name value pairs that I would like to pass as arguments to a function in a second PowerShell script.

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

I do not know what parameters will be placed in this configuration file at design time, so right at the point where I need to invoke this second PowerShell script, I basically just have one variable that has the path to this second script, and a second variable that is an array of arguments to pass to the script identified in the path variable.

因此,该变量包含第二个脚本的路径($ scriptPath) ,其值可能像这样:

So the variable containing the path to the second script ($scriptPath), might have a value like:

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

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

The variable containing the arguments ($argumentList) might look something like:

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

我如何获得从这种状态到执行script.ps1以及$ argumentList中的所有参数?

How do I get from this state of affairs to the execution of script.ps1 with all of the arguments from $argumentList?

我想要第二个脚本中的任何写主机命令来在调用第一个脚本的控制台上可见。

I'd like any write-host commands from this second script to be visible to the console from which this first script is invoked.

我已经尝试过点源,Invoke-Command,Invoke-Expression和Start-Job。

I have tried dot-sourcing, Invoke-Command, Invoke-Expression, and Start-Job, but I haven't found an approach that doesn't produce errors.

例如,我认为最简单的第一种方法是尝试如下所示的Start-Job:

For example, I thought the easiest first route was to try Start-Job called as follows:

Start-Job -FilePath $scriptPath -ArgumentList $argumentList

...但是此操作因以下错误而失败:

...but this fails with this error:

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

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

...in this case, "ConfigFilename" is the first parameter in the param list defined by the second script, and my invocation is apparently trying to set its value to "-ConfigFilename", which is obviously intended to identify the parameter by name, not set its value.

我想念什么?

编辑:

好,这是一个模拟被称为脚本的特写镜头,位于名为invokee.ps1的文件中。

Ok, here is a mock-up of the to-be-called script, in a file named 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

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

...and here is a mock-up of the calling script, in a file named invokee.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:

When I run invoker.ps1, this is the error I'm currently getting on the first call to Invoke-Expression:

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

第二个调用工作正常,但一个重大区别是第一个调用使用的是其路径的参数其中有空格,而第二个则没有。我在这些路径中是否存在空格错误?

The second call works just fine, but one significant difference is that the first version is using arguments whose paths have spaces in them, and the second does not. Am I mishandling the presence of spaces in these paths?

推荐答案

啊哈。原来这是一个简单的问题,脚本的路径中有空格。

Aha. This turned out to be a simple problem of there being spaces in the path to the script.

将Invoke-Expression行更改为:

Changing the Invoke-Expression line to:

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

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

...was enough to get it to kick off. Thanks to Neolisk for your help and feedback!

这篇关于使用脚本中的参数调用第二个脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆