Hoo*_*oob 57 powershell parameter-passing command-line-arguments invoke-command start-job
我对PowerShell比较陌生,并且有一个脚本可以读取一个配置文件,该文件会产生一组名称值对,我希望将它们作为参数传递给第二个PowerShell脚本中的函数.
我不知道在设计时将在此配置文件中放置哪些参数,所以就在我需要调用第二个PowerShell脚本时,我基本上只有一个变量具有第二个脚本的路径,第二个脚本变量,它是要传递给路径变量中标识的脚本的参数数组.
因此,包含第二个脚本($ scriptPath)路径的变量可能具有如下值:
"c:\the\path\to\the\second\script.ps1"
Run Code Online (Sandbox Code Playgroud)
包含参数($ argumentList)的变量可能类似于:
-ConfigFilename "doohickey.txt" -RootDirectory "c:\some\kind\of\path" -Max 11
Run Code Online (Sandbox Code Playgroud)
如何从这种状态到使用$ argumentList中的所有参数执行script.ps1?
我希望来自第二个脚本的任何write-host命令对于调用此第一个脚本的控制台是可见的.
我尝试过dot-sourcing,Invoke-Command,Invoke-Expression和Start-Job,但我还没有找到一种不会产生错误的方法.
例如,我认为最简单的第一条路线是尝试按如下方式调用Start-Job:
Start-Job -FilePath $scriptPath -ArgumentList $argumentList
Run Code Online (Sandbox Code Playgroud)
...但是由于此错误而失败:
System.Management.Automation.ValidationMetadataException:
Attribute cannot be added because it would cause the variable
ConfigFilename with value -ConfigFilename to become invalid.
Run Code Online (Sandbox Code Playgroud)
...在这种情况下,"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
Run Code Online (Sandbox Code Playgroud)
...这是一个名为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
Run Code Online (Sandbox Code Playgroud)
当我运行invoker.ps1时,这是我第一次调用Invoke-Expression时遇到的错误:
Invoke-Expression : You must provide a value expression on
the right-hand side of the '-' operator.
Run Code Online (Sandbox Code Playgroud)
第二个调用工作正常,但一个显着的区别是第一个版本使用的参数的路径中有空格,第二个版本没有.我错误地处理了这些路径中空间的存在吗?
Hoo*_*oob 52
啊哈.事实证明,这是一个简单的问题,即脚本路径中有空格.
将Invoke-Expression行更改为:
Invoke-Expression "& `"$scriptPath`" $argumentList"
Run Code Online (Sandbox Code Playgroud)
......足以让它开始.感谢Neolisk的帮助和反馈!
Neo*_*isk 43
Invoke-Expression应该工作得很好,只要确保你正确使用它.对于你的情况,它应该是这样的:
Invoke-Expression "$scriptPath $argumentList"
Run Code Online (Sandbox Code Playgroud)
我使用Get-Service测试了这种方法,似乎按预期工作.
Mrc*_*ief 29
很多简单的实际上是:
方法1:
Invoke-Expression $scriptPath $argumentList
Run Code Online (Sandbox Code Playgroud)
方法2:
& $scriptPath $argumentList
Run Code Online (Sandbox Code Playgroud)
方法3:
$scriptPath $argumentList
Run Code Online (Sandbox Code Playgroud)
如果你有空位scriptPath,不要忘记逃避它们`"$scriptPath`"
这里有一个答案,涵盖了从PS脚本调用另一个PS脚本的更一般的问题,就像你编写许多小的,窄目的脚本的脚本一样.
我发现这只是一个使用点源的案例.也就是说,你只需:
# This is Script-A.ps1
. ./Script-B.ps1 -SomeObject $variableFromScriptA -SomeOtherParam 1234;
Run Code Online (Sandbox Code Playgroud)
我发现所有的Q/A非常令人困惑和复杂,并最终落在上面的简单方法上,这就像调用另一个脚本就好像它是原始脚本中的一个函数,我似乎更直观.
Dot-sourcing可以完全"导入"其他脚本,使用:
. ./Script-B.ps1
Run Code Online (Sandbox Code Playgroud)
现在好像这两个文件已合并.
最终,我真正缺少的是我应该构建一个可重用功能模块的概念.
我们可以使用splatting:
& $command @args
Run Code Online (Sandbox Code Playgroud)
其中@args(自动变量$ args)被喷射到参数数组中.
根据PS,5.1