如何以编程方式知道传递给cmdlet的所有参数?

Dre*_*mer 1 c# powershell cmdlets cmdlet powershell-2.0

我在.net中实现了客户cmdlet.我想知道用户传递给它的所有参数.

My-Cmdlet -foo -bar -foobar
Run Code Online (Sandbox Code Playgroud)

基本上我想知道用户以编程方式使用参数foo,bar,foobar执行此cmdlet.

在脚本中看起来我们可以使用:$ PSBoundParameters.ContainsKey('WhatIf')

我需要.net(c#)中的那个.

Bar*_*ekB 5

据我所知:$ PSBoundParameters只是$ MyInvocation.BoundParameters的快捷方式:$ MyInvocation.BoundParameters.Equals($ PSBoundParameters)True

如果你想在写的cmdlet中获得相同的信息,你可以得到它...:

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());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

代码很快,但你应该得到图片.免责声明:我不是开发人员,所以我可能做错了.;)

HTH Bartek