简体   繁体   English

使用多个参数从PowerShell脚本调用PowerShell脚本

[英]Call PowerShell Script from PowerShell Script with multiple arguments

I know, this has been asked a few times but I cannot seem to get it working. 我知道,这个问题已经被问过几次了,但是我似乎无法使它生效。 Basically, I am using multiple ps1 files so I don't have to have a very large script, in this case, its two files (so far): 基本上,我使用了多个ps1文件,因此我不必有一个非常大的脚本,在这种情况下,它的两个文件(到目前为止):

Migration-Main.ps1 迁移Main.ps1

Migration-Companies.ps1 迁移Companies.ps1

I want to call Migration-Companies.ps1 from Migration-Main.ps1 using invoke expression but this seems to only work if there is only one parameter in the target script, in my case there are two: 我想使用调用表达式从Migration-Main.ps1调用Migration-Companies.ps1 ,但这似乎仅在目标脚本中只有一个参数的情况下有效,在我的情况下,有两个:

param (
    [parameter(mandatory=$false )]
    [datetime]$CompanyDateCull,  
    [parameter(mandatory=$false )]
    [string]$CompanyManagerCull
)

Write-Output $CompanyDateCull

So if I try to call the script like so: 因此,如果我尝试这样调用脚本:

Invoke-Expression "$Script_Companies -CompanyDateCull $CompanyDateCull -CompanyManagerCull "Fred""

I will get the following result: 我将得到以下结果:

Invoke-Expression : A positional parameter cannot be found that accepts argument 'Fred'.
At C:\Users\user\OneDrive\Scripts\Migrations\Imports\FastTrack-Migration-Main.ps1:282 char:1
+ Invoke-Expression "$Script_Companies -CompanyDateCull $CompanyDateCul ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-Expression], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeExpressionCommand

But if i remove the second parameter 但是如果我删除第二个参数

Invoke-Expression "$Script_Companies -CompanyDateCull $CompanyDateCull"

it works: 有用:

Saturday, 21 September 2013 12:00:00 AM

I have tried running the file with other commands as well, such as simply calling PowerShell.exe: 我也尝试使用其他命令来运行文件,例如仅调用PowerShell.exe:

PowerShell.exe -file $Script_Companies -CompanyDateCull $CompanyDateCull -CompanyManagerCull $CompanyManagerCull

But this stops any of my dot sourced functions from being accessible to the script being called, which is a requirement. 但这阻止了我的任何点源函数都无法被调用的脚本访问,这是必需的。

Any help would be much appreciated. 任何帮助将非常感激。

对于第一个查询,您需要使用backtick(`)转义双引号:

Invoke-Expression "$Script_Companies -CompanyDateCull $CompanyDateCull -CompanyManagerCull `"Fred`""

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM