PS: run ps1 without parameter (when parametersets are defined)?

janhoedt
janhoedt used Ask the Experts™
on
Hi,

I have a powershellscript with 2 parametersets.
However, if no parameter is given, the script still should execute with default values.
But it generates an error: 'Parameter set cannot be resolved using the specified named parameters.'
How can I make the script still run without giving a parameter?

[Parameter(Mandatory=$False,
        ParameterSetName = 'Set1',
        Position = 0)]
      $test,

      
      [Parameter(Mandatory=$False,
        ParameterSetName = 'Set2',
        Position = 1)]
      $test2,


J
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
You need to declare one of them as default set to use:
[CmdletBinding(DefaultParameterSetName = 'Set1')]
Param(
	[Parameter(
		Mandatory=$False,
		ParameterSetName = 'Set1',
		Position = 0
	)]
	$test,
	[Parameter(
		Mandatory=$False,
		ParameterSetName = 'Set2',
		Position = 1
	)]
	$test2
)

Open in new window

Top Expert 2014

Commented:
You may want/need to assign a default value to $test and $test2

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial