3

C# でコマンドレットを作成しました。特定の文字列パラメーター ("PackageId" はこのサンプルです) に対してすべての可能な値を提供することは可能ですか?

public sealed class InstallPackageCommand : PSCmdlet
{
    [Parameter(Position = 0, Mandatory = true)]
    public string PackageId { get; set; }

    protected override void BeginProcessing()
    {
       //...
    }
} 
4

3 に答える 3

5

ValidateSetAttributeそのパラメーターのプロパティで を使用できます。

[ValidateNotNullOrEmpty]
[ValidateSet(new string[] {"a","b","c"})]
[Parameter(Position = 0, Mandatory = true)]
public string PackageId { get; set; }
于 2013-10-31T15:41:22.847 に答える
0

カスタム列挙を作成できます。

http://blogs.msdn.com/b/powershell/archive/2007/01/23/how-to-create-enum-in-powershell.aspx

次に、変数をその列挙型にします。

これにより、可能な値がコマンド lilne からの Powershell タブ補完でも機能するようになります。

于 2013-10-31T14:33:29.537 に答える
0

カスタム列挙の例:

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

namespace ExampleNameSpace
{
    [Cmdlet(VerbsCommon.Get, "Something")]
    [OutputType("PSCustomObject")]
    public class GetSomething : PSCmdlet
    {
        public enum ExampleEnum { A, B, C };

        [Parameter(
            HelpMessage = "Enter A, B, or C.",
            Mandatory = true,
            Position = 0,
            ValueFromPipelineByPropertyName = true,
            ValueFromPipeline = true
        )]
        public ExampleEnum ExampleParameter { get; set; }

        protected override void ProcessRecord()
        {
            WriteObject(ExampleParameter);
            switch (ExampleParameter)
            {
                case ExampleEnum.A:
                    WriteObject("Case A");
                    break;
                case ExampleEnum.B:
                    WriteObject("Case B");
                    break;
                case ExampleEnum.C:
                    WriteObject("Case C");
                    break;
                default:
                    break;
            }
        }
    }
}

使用例 #1:

$ Get-Something

cmdlet Get-Something at command pipeline position 1
Supply values for the following parameters:
(Type !? for Help.)
ExampleParameter: !?
Enter A, B, or C.
ExampleParameter: a
A
Case A

使用例 #2:

$ Get-Something -ExampleParameter x
Get-Something : Cannot bind parameter 'ExampleParameter'. Cannot convert value "x" to type
"ExampleNameSpace.GetSomething+ExampleEnum". Error: "Unable to match the identifier name x to a valid enumerator name.
Specify one of the following enumerator names and try again:
A, B, C"
At line:1 char:33
+ Get-Something -ExampleParameter x
+                                 ~
    + CategoryInfo          : InvalidArgument: (:) [Get-Something], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,ExampleNameSpace.GetSomething

使用例 #3:

$ "a","b","c","d" | Get-Something
A
Case A
B
Case B
C
Case C
Get-Something : The input object cannot be bound to any parameters for the command either because the command does not
take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
At line:1 char:20
+ "a","b","c","d" | Get-Something
+                    ~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (d:String) [Get-Something], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectNotBound,ExampleNameSpace.GetSomething
于 2015-10-09T14:04:00.353 に答える