1

I'm struggling to call a second script in one of my PowerShell scripts at the moment using Invoke-Expression. It's currently producing an error:

"Parameter set cannot be resolved using the specified named parameters."

Annoyingly, it works fine for one switch (being -ServerDriveReport), but doesn't work for the other.

The first script (called DriveReport.ps1) is like:

[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"}

The "EmailDriveReport.ps1" script is like:

[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}

When running "DriveReport.ps1 -ServerDriveReport" everything works as expected. But when running "DriveReport.ps1 -VMDriveReport", that's when I get the aforementioned error message.

Has anyone seen this before?

Any help would be greatly appreciated!

2 Answers 2

2

Without attempting to solve your immediate problem (which is not obvious to me from the code posted), consider using the automatic $PSBoundParameters variable via splatting to pass the parameters through to the 2nd script:

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

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

& 'C:\Scripts\Drive Report\EmailDriveReport.ps1' @PSBoundParameters

Generally, Invoke-Expression should be avoided, because there are usually more robust solutions available and because it presents a security risk if invoked on untrusted strings.

Sign up to request clarification or add additional context in comments.

Comments

0

Thanks for the help! I managed to resolve this by carefully going over the script and finding out one of the Else statements was calling the file incorrectly. I've now changing this to: & 'C:\Scripts\Drive Report\EmailDriveReport.ps1' as suggested.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.