2012-05-16 8 views
1

.net에 구현 된 customer cmdlet이 있습니다. 사용자가 전달한 모든 매개 변수를 알고 싶습니다.cmdlet에 프로그래밍 방식으로 전달되는 모든 매개 변수를 확인하는 방법은 무엇입니까?

My-Cmdlet -foo -bar -foobar 

기본적으로 내가 매개 변수 foo는, 바이 cmdlet을 실행하는 사용자를 알고 싶습니다 프로그래밍 FOOBAR.

은 스크립트에서 우리가 사용하여 전송할 수 있습니다 같은데 : $ PSBoundParameters.ContainsKey ('WhatIf')를

난 내 머리 위로 떨어져 .NET에서 그 equalent (C#을)

답변

0

필요를 cmdlet을 사용하여 프록시 명령을 작성 (함수로 명령 줄 바꿈)하고 사용자 정의 코드를 추가하지 않는 한 코드에 액세스하지 않고는 코드를 사용할 수 없습니다. 또 다른 아이디어는 콘솔 히스토리에서 마지막으로 실행 된 명령 또는 유사한 방법을 검사하는 것입니다.

는 는
5

기억이 지금까지 같이 $ PSBoundParameters 단지 $ MyInvocation.BoundParameters에 대한 바로 가기입니다 : $ MyInvocation.BoundParameters.Equals ($ PSBoundParameters) 진정한

당신이 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()); 
     } 
    } 
} 

코드는 quick'n'dirty,하지만 당신은 사진을 찍어해야한다. 면책 조항 : 저는 개발자가 아니므로 아마 잘못했을 것입니다. ;)

HTH Bartek

+0

필요도 고려해야 할 ['InvocationInfo.UnboundParameters'] (http://msdn.microsoft.com/en-us/library/windows/desktop/system.management. 매개 변수가 'ValueFromRemainingArguments' true 인 경우) 매개 변수에 바인딩되지 않은 모든 매개 변수에 대해 (예 : automation.invocationinfo.unboundarguments % 28v = vs.85 % 29.aspx). – Richard

0

어떤 방법 whatifpreference는 항상 false를 반환하기위한 this.GetVariable.

나는 이것을 myinvocation.buildparameters 사전을 사용하여 해결했습니다.

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

안부, 드리머