简体   繁体   English

使用SwitchParameter的PowerShell自定义Cmdlet

[英]PowerShell custom Cmdlet using SwitchParameter

I have the following code, where I try to create a custom Cmdlet for PowerShell using C#. 我有以下代码,我尝试使用C#为PowerShell创建自定义Cmdlet。 What I want to do with my custom cmdlet is that, user should call it with two parameters, from which first one should be -Text or -File or -Dir, and the next one should be a value, a string which specifies the value for text, or file, or directory. 我想用自定义cmdlet做的是,用户应该使用两个参数调用它,第一个应该是-Text或-File或-Dir,下一个应该是一个值,一个指定值的字符串用于文本,文件或目录。 It works fine as long as I can see. 只要我能看到它就可以正常工作。 But I'm just curious whether there is another simple method or more elegant method that I can use to achieve what I want. 但我只是好奇是否有另一种简单的方法或更优雅的方法可以用来实现我想要的东西。 Or is my solution the simplest that it can get? 或者我的解决方案最简单吗? By the way, SHA256Text, SHA256File, and SHA256Directory, are just custom functions that I have written, so don't worry about them. 顺便说一下,SHA256Text,SHA256File和SHA256Directory只是我编写的自定义函数,所以不要担心它们。

using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
using System.Management.Automation;

namespace PSSL
{
    [Cmdlet(VerbsCommon.Get, "SHA256")]
    public class GetSHA256 : PSCmdlet
    {
        #region Members

        private bool text;
        private bool file;
        private bool directory;
        private string argument;

        #endregion

        #region Parameters

        [Parameter(Mandatory = true, Position = 0, ParameterSetName = "Text")]
        public SwitchParameter Text
        {
            get { return text; }
            set { text = value; }
        }

        [Parameter(Mandatory = true, Position = 0, ParameterSetName = "File")]
        public SwitchParameter File
        {
            get { return file; }
            set { file = value; }
        }

        [Parameter(Mandatory = true, Position = 0, ParameterSetName = "Directory")]
        public SwitchParameter Dir
        {
            get { return directory; }
            set { directory = value; }
        }

        [Parameter(Mandatory = true, Position = 1)]
        [ValidateNotNullOrEmpty]
        public string Argument
        {
            get { return argument; }
            set { argument = value; }
        }

        #endregion

        #region Override Methods

        protected override void ProcessRecord()
        {
            switch(ParameterSetName)
            {
                case "Text":
                    SHA256Text(argument);
                    break;

                case "File":
                    SHA256File(argument);
                    break;

                case "Directory":
                    SHA256Directory(argument);
                    break;

                default:
                    throw new ArgumentException("Error: Bad parameter name.");
            }
        }

        #endregion
    }
}

You could use parameter sets to ensure that the user is only able to specify one of -Text , -File , or -Dir in conjunction with -Argument . 你可以使用参数集 ,以确保用户只能指定一个-Text-File ,或-Dir会同-Argument You should then make your switches (and argument) mandatory rather than optional. 然后,您应该使您的开关(和参数)成为强制性而非可选性。 This would mean your ProcessRecord method would know that a switch has been specified and that an argument has been provided. 这意味着您的ProcessRecord方法将知道已指定了一个开关并且已经提供了一个参数。 Therefore, you could remove your usage output, which will be available via Powershell's built-in Get-Help cmdlet. 因此,您可以删除使用输出,这些输出将通过Powershell的内置Get-Help cmdlet提供。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM