PowerShell“;“无法解析参数集”;调用其他脚本时

PowerShell“;“无法解析参数集”;调用其他脚本时,powershell,parameters,Powershell,Parameters,目前,我正在努力使用Invoke Expression调用我的一个PowerShell脚本中的第二个脚本。它当前正在生成一个错误: “无法使用指定的命名参数解析参数集。” 令人恼火的是,它对一个交换机(being-ServerDriveReport)工作良好,但对另一个交换机不起作用 第一个脚本(称为DriveReport.ps1)类似于: [cmdletbinding()] Param( [Parameter(ParameterSetName="ServerDriveReport")]

目前,我正在努力使用Invoke Expression调用我的一个PowerShell脚本中的第二个脚本。它当前正在生成一个错误:

“无法使用指定的命名参数解析参数集。”

令人恼火的是,它对一个交换机(being-ServerDriveReport)工作良好,但对另一个交换机不起作用

第一个脚本(称为DriveReport.ps1)类似于:

[cmdletbinding()]
Param(
    [Parameter(ParameterSetName="ServerDriveReport")]
    [switch]$ServerDriveReport,

    [Parameter(ParameterSetName="VMDriveReport")]
    [switch]$VMDriveReport)

If($ServerDriveReport){
Invoke-Expression "& 'C:\Scripts\Drive Report\EmailDriveReport.ps1' -ServerDriveReport"}

If($VMDriveReport){
Invoke-Expression "& 'C:\Scripts\Drive Report\EmailDriveReport.ps1' -VMDriveReport"}
[cmdletbinding()]
Param(
    [Parameter(ParameterSetName="ServerDriveReport")]
    [switch]$ServerDriveReport,

    [Parameter(ParameterSetName="VMDriveReport")]
    [switch]$VMDriveReport)

If($ServerDriveReport){
# Send an email containing the server drive report}

If($VMDriveReport){
# Send an email contining the VM drive report}
“EmailDriveReport.ps1”脚本类似于:

[cmdletbinding()]
Param(
    [Parameter(ParameterSetName="ServerDriveReport")]
    [switch]$ServerDriveReport,

    [Parameter(ParameterSetName="VMDriveReport")]
    [switch]$VMDriveReport)

If($ServerDriveReport){
Invoke-Expression "& 'C:\Scripts\Drive Report\EmailDriveReport.ps1' -ServerDriveReport"}

If($VMDriveReport){
Invoke-Expression "& 'C:\Scripts\Drive Report\EmailDriveReport.ps1' -VMDriveReport"}
[cmdletbinding()]
Param(
    [Parameter(ParameterSetName="ServerDriveReport")]
    [switch]$ServerDriveReport,

    [Parameter(ParameterSetName="VMDriveReport")]
    [switch]$VMDriveReport)

If($ServerDriveReport){
# Send an email containing the server drive report}

If($VMDriveReport){
# Send an email contining the VM drive report}
当运行“DriveReport.ps1-ServerDriveReport”时,一切正常。但是当运行“DriveReport.ps1-VMDriveReport”时,我会收到前面提到的错误消息

以前有人见过这个吗


任何帮助都将不胜感激 $PSBONDATABORE 变量”,将参数传递给第二个脚本:

[cmdletbinding()]
Param(
    [Parameter(ParameterSetName="ServerDriveReport")]
    [switch]$ServerDriveReport,

    [Parameter(ParameterSetName="VMDriveReport")]
    [switch]$VMDriveReport)
)

& 'C:\Scripts\Drive Report\EmailDriveReport.ps1' @PSBoundParameters
一般来说,应该避免调用表达式,因为通常有更健壮的解决方案可用,而且如果在不受信任的字符串上调用,则会带来安全风险。

谢谢您的帮助! 我仔细检查了脚本,发现其中一条Else语句错误地调用了文件,从而解决了这个问题。我现在将此更改为: &建议使用“C:\Scripts\Drive Report\EmailDriveReport.ps1”