1

I have customer cmdlet implemented in .net. I would like to know all the parameters user passed to it.

My-Cmdlet -foo -bar -foobar

Basically i would like to know that user executed this cmdlet with parameter foo, bar, foobar programmatically.

Looks like in script we can do it using: $PSBoundParameters.ContainsKey('WhatIf')

I need equalent of that in .net (c#)

3 Answers 3

8

As far as I remember: $PSBoundParameters is just shortcut for $MyInvocation.BoundParameters: $MyInvocation.BoundParameters.Equals($PSBoundParameters) True

If you want to get the same information in cmdlet that you wrote, you can get it like that...:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management.Automation;

namespace Test
{
    [Cmdlet(VerbsCommon.Get, "WhatIf", SupportsShouldProcess = true)]
    public class GetWhatIf : PSCmdlet
    {

        // Methods
        protected override void BeginProcessing()
        {
            this.WriteObject(this.MyInvocation.BoundParameters.ContainsKey("WhatIf").ToString());
        }
    }
}

The code is quick'n'dirty, but you should get the picture. Disclaimer: I'm not a developer, so I'm probably doing it wrong. ;)

HTH Bartek

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

1 Comment

Need to also consider InvocationInfo.UnboundParameters for any not bound to a paramter (eg. when there is a parameter has ValueFromRemainingArguments true).
0

Off the top of my head, you can't without having access to the code, unless you create a proxy command around the cmdlet (wrap the command with a function) and add your custom code to it. Another idea would be to check the last executed command in the console history or a similar method.

Comments

0

Some how this.GetVariable for whatifpreference always returned false.

i worked around this by using myinvocation.buildparameters dictionary.

public bool WhatIf
{
    get
    {                                
        //if (this.GetVaribaleValue<bool>("WhatIfPreference", out whatif))
        return this.MyInvocation.BoundParameters.ContainsKey("WhatIf")
            && ((SwitchParameter)MyInvocation.BoundParameters["WhatIf"]).ToBool(); 
    }
}

Regards, Dreamer

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.