I have the following code, where I try to create a custom Cmdlet for PowerShell using C#.
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.
The point is that if I try to call my cmdlet with a parameter -Test for example, which is of course a wrong parameter, I don't get the value of the default statement, which says "Bad parameter name".
I guess my code just don't get till the default part of the switch.
By the way, SHA256Text, SHA256File, and SHA256Directory, are just custom helper methods that I have written, so don't worry about them.
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
}
}