0

I need to apply conditional switch parameter only if other parameter is provided. Can any body let me know how can I achieve this in C#

I want following way command let accessible on powershell terminal.

Edit

 Get-Parameter # this will process other path of the code and won't throw an error. (e.g. Not providing name of parameter, it would return all parameters in the container )

Get-Parameter -Name "Epics" -Writeable # writeable is switch parameter
Get-Parameter -Writeable # should throw an error ( Writeable switch only allows when Name parameter is provided.

Following is C# commandlet code.

[Cmdlet(VerbsCommon.Get, "Parameter")]
public class GetParameter : PSCmdlet
{
    [Parameter(
        Position = 1,
        Mandatory = false,
        ValueFromPipeline = true,
        ValueFromPipelineByPropertyName = true, ParameterSetName = "Name")
    ]
    public string Name { get; set; }

    [Parameter(Mandatory = true, ParameterSetName = "Name" ,ValueFromPipeline = true,
    ValueFromPipelineByPropertyName = true)]
    public SwitchParameter Writeable { get; set; } = false;
}
Usman
  • 2,742
  • 4
  • 44
  • 82
  • 2
    I think you are talking about parameter sets but I can't tell for sure. – EBGreen Jul 10 '18 at 14:45
  • Might be , but my problem is very clear. If user types "Writeable" as switch and doesn't specify name of parameter, it would throw an error otherwise, it would work normally. – Usman Jul 10 '18 at 14:47
  • What do you mean `specify name of parameter`? @EBGreen is correct in that it sounds like you want parameter sets. – Maximilian Burszley Jul 10 '18 at 14:47
  • My powershell is a bit rusty, but don't you also need to specify a Position for `Writeable`? – Tudor Jul 10 '18 at 14:52
  • Get-Parameter -Name "Epics" -Writeable this means I want "Epics" writeable parameter. But what If if user types Get-Parameter -Writeable, this should throw error. Because user has not specified which parameter he wants. so mentioning -Writeable switch should throw an error – Usman Jul 10 '18 at 14:53
  • Yeah , I want -Writeable to be positioned at last. – Usman Jul 10 '18 at 14:54
  • At a powershell prompt type `get-help about_functions_advanced_parameters` – EBGreen Jul 10 '18 at 14:55
  • So `Name` should be mandatory, but `Writeable` optional? – Tudor Jul 10 '18 at 15:00
  • Yeah Writeable switch must be optional. Because if it is present then i would provide writable copy of parameter otherwise, it would be by default readable parameter that would be returned – Usman Jul 10 '18 at 15:02

1 Answers1

0

Based on the comments I think you need something like this:

[Cmdlet(VerbsCommon.Get, "Parameter")]
public class GetParameter : PSCmdlet
{
    [Parameter(
        Position = 0,
        Mandatory = true,
        ValueFromPipeline = true,
        ValueFromPipelineByPropertyName = true,
        ParameterSetName = "Name")
    ]
    public string Name { get; set; }

    [Parameter(
        Mandatory = false,
        ParameterSetName = "Name",
        ValueFromPipeline = true,
        Position = 1,
        ValueFromPipelineByPropertyName = true)]
    public SwitchParameter Writeable { get; set; } = false;
}
Tudor
  • 61,523
  • 12
  • 102
  • 142
  • I am sorry I didnt inform in the post. This Commandlet should also work when nothing is being provided. e.g Get-Parameter would process some other logic without asking additional info. So this commandlet has to work in dual mode. If parameter name is provided it should behave accordingly, otherwise if nothing provided that is also a valid use case that should return output without throwing an error. – Usman Jul 10 '18 at 15:12
  • @Usman: Can you set all parameters to optional and then do manual validations in the cmdlet `BeginProcessing` method? – Tudor Jul 10 '18 at 15:20
  • Yeah that can be the workaround. But i was thinking to have out of the box solution provided by attributes rather than relying on conditions – Usman Jul 10 '18 at 15:22