Reputation: 341
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!
Upvotes: 1
Views: 5350
Reputation: 341
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.
Upvotes: 0
Reputation: 437638
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.
Upvotes: 2