P
Patrick Dreyer
Hi,
I'm struggling getting a cmdlet with parameter sets working.
I'd like to support three different "flavors" for calling my cmdlet Run-SSHCmd:
1) run-sshcmd Command (Parameter set "SharedConnection")
2) run-sshcmd Command SuPassword (Parameter set "SharedConnectionSu")
3) run-sshcmd Command SuUser SuPassword (Parameter set "SharedConnectionSuUser")
1) "run-sshcmd ls" -> ok, ParameterSetName = "SharedConnection"
2) "run-sshcmd ls test" -> ERROR, requests SuPassword
3) "run-sshcmd ls root test" -> ok, ParameterSetName = "SharedConnectionSuUser"
According to the output (see below) of my PowerShell script parameters.ps1 (see below), the implementation (see below) is correct.
Any suggestions?
Patrick
PowerShell script parameters.ps1
================================
(get-command run-sshcmd).ParameterSets | foreach {
$ps = $_.name
$_.Parameters | Select @{n="ParameterName";e={$_.name}},@{n="Mandatory";e={$_.IsMandatory}},@{n="Position";e={$_.Position}},@{n="HelpMessage";e={$_.HelpMessage}},@{n="ParameterSet";e={$ps}} | Format-Table
}
Output of parameters.ps1
========================
ParameterName Mandatory Position HelpMessage ParameterSet
------------- --------- -------- ----------- ------------
Command True 0 Command to execute SharedConnection
Verbose False -2147483648 SharedConnection
Debug False -2147483648 SharedConnection
ErrorAction False -2147483648 SharedConnection
ErrorVariable False -2147483648 SharedConnection
OutVariable False -2147483648 SharedConnection
OutBuffer False -2147483648 SharedConnection
ParameterName Mandatory Position HelpMessage ParameterSet
------------- --------- -------- ----------- ------------
Command True 0 Command to execute SharedConnectionSu
SuPassword True 21 Password used for user swi... SharedConnectionSu
Verbose False -2147483648 SharedConnectionSu
Debug False -2147483648 SharedConnectionSu
ErrorAction False -2147483648 SharedConnectionSu
ErrorVariable False -2147483648 SharedConnectionSu
OutVariable False -2147483648 SharedConnectionSu
OutBuffer False -2147483648 SharedConnectionSu
ParameterName Mandatory Position HelpMessage ParameterSet
------------- --------- -------- ----------- ------------
Command True 0 Command to execute SharedConnectionSuUser
SuPassword True 21 Password used for user swi... SharedConnectionSuUser
SuUser True 20 Username to switch to with... SharedConnectionSuUser
Verbose False -2147483648 SharedConnectionSuUser
Debug False -2147483648 SharedConnectionSuUser
ErrorAction False -2147483648 SharedConnectionSuUser
ErrorVariable False -2147483648 SharedConnectionSuUser
OutVariable False -2147483648 SharedConnectionSuUser
OutBuffer False -2147483648 SharedConnectionSuUser
Cmdlet implementation
=====================
[Cmdlet("Run", "SSHCmd", DefaultParameterSetName = PARAMETER_SET_SHARED_CONNECTION)]
public class RunSSHCmd : PSCmdlet
{
public const string PARAMETER_SET_SHARED_CONNECTION = "SharedConnection";
public const string PARAMETER_SET_SHARED_CONNECTION_SU = "SharedConnectionSu";
public const string PARAMETER_SET_SHARED_CONNECTION_SU_USER = "SharedConnectionSuUser";
private string command;
[Parameter(Position = 0,
Mandatory = true,
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true,
HelpMessage = "Command to execute")]
[ValidateNotNullOrEmpty]
public string Command {
set { command = value; }
get { return command; }
}
private string suPassword;
[Parameter(Position = 21,
ParameterSetName = PARAMETER_SET_SHARED_CONNECTION_SU,
Mandatory = true,
ValueFromPipelineByPropertyName = true,
HelpMessage = "Password used for user switching with su")]
[Parameter(Position = 21,
ParameterSetName = PARAMETER_SET_SHARED_CONNECTION_SU_USER,
Mandatory = true,
ValueFromPipelineByPropertyName = true,
HelpMessage = "Password used for user switching with su")]
public string SuPassword {
set { suPassword = value; }
get { return suPassword; }
}
private string suUser;
[Parameter(Position = 20,
ParameterSetName = PARAMETER_SET_SHARED_CONNECTION_SU_USER,
Mandatory = true,
ValueFromPipelineByPropertyName = true,
HelpMessage = "Username to switch to with su; default = \"root\"")]
public string SuUser {
set { suUser = value; }
get { return suUser; }
}
...
}
Probably your curious about
I'm struggling getting a cmdlet with parameter sets working.
I'd like to support three different "flavors" for calling my cmdlet Run-SSHCmd:
1) run-sshcmd Command (Parameter set "SharedConnection")
2) run-sshcmd Command SuPassword (Parameter set "SharedConnectionSu")
3) run-sshcmd Command SuUser SuPassword (Parameter set "SharedConnectionSuUser")
1) "run-sshcmd ls" -> ok, ParameterSetName = "SharedConnection"
2) "run-sshcmd ls test" -> ERROR, requests SuPassword
3) "run-sshcmd ls root test" -> ok, ParameterSetName = "SharedConnectionSuUser"
According to the output (see below) of my PowerShell script parameters.ps1 (see below), the implementation (see below) is correct.
Any suggestions?
Patrick
PowerShell script parameters.ps1
================================
(get-command run-sshcmd).ParameterSets | foreach {
$ps = $_.name
$_.Parameters | Select @{n="ParameterName";e={$_.name}},@{n="Mandatory";e={$_.IsMandatory}},@{n="Position";e={$_.Position}},@{n="HelpMessage";e={$_.HelpMessage}},@{n="ParameterSet";e={$ps}} | Format-Table
}
Output of parameters.ps1
========================
ParameterName Mandatory Position HelpMessage ParameterSet
------------- --------- -------- ----------- ------------
Command True 0 Command to execute SharedConnection
Verbose False -2147483648 SharedConnection
Debug False -2147483648 SharedConnection
ErrorAction False -2147483648 SharedConnection
ErrorVariable False -2147483648 SharedConnection
OutVariable False -2147483648 SharedConnection
OutBuffer False -2147483648 SharedConnection
ParameterName Mandatory Position HelpMessage ParameterSet
------------- --------- -------- ----------- ------------
Command True 0 Command to execute SharedConnectionSu
SuPassword True 21 Password used for user swi... SharedConnectionSu
Verbose False -2147483648 SharedConnectionSu
Debug False -2147483648 SharedConnectionSu
ErrorAction False -2147483648 SharedConnectionSu
ErrorVariable False -2147483648 SharedConnectionSu
OutVariable False -2147483648 SharedConnectionSu
OutBuffer False -2147483648 SharedConnectionSu
ParameterName Mandatory Position HelpMessage ParameterSet
------------- --------- -------- ----------- ------------
Command True 0 Command to execute SharedConnectionSuUser
SuPassword True 21 Password used for user swi... SharedConnectionSuUser
SuUser True 20 Username to switch to with... SharedConnectionSuUser
Verbose False -2147483648 SharedConnectionSuUser
Debug False -2147483648 SharedConnectionSuUser
ErrorAction False -2147483648 SharedConnectionSuUser
ErrorVariable False -2147483648 SharedConnectionSuUser
OutVariable False -2147483648 SharedConnectionSuUser
OutBuffer False -2147483648 SharedConnectionSuUser
Cmdlet implementation
=====================
[Cmdlet("Run", "SSHCmd", DefaultParameterSetName = PARAMETER_SET_SHARED_CONNECTION)]
public class RunSSHCmd : PSCmdlet
{
public const string PARAMETER_SET_SHARED_CONNECTION = "SharedConnection";
public const string PARAMETER_SET_SHARED_CONNECTION_SU = "SharedConnectionSu";
public const string PARAMETER_SET_SHARED_CONNECTION_SU_USER = "SharedConnectionSuUser";
private string command;
[Parameter(Position = 0,
Mandatory = true,
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true,
HelpMessage = "Command to execute")]
[ValidateNotNullOrEmpty]
public string Command {
set { command = value; }
get { return command; }
}
private string suPassword;
[Parameter(Position = 21,
ParameterSetName = PARAMETER_SET_SHARED_CONNECTION_SU,
Mandatory = true,
ValueFromPipelineByPropertyName = true,
HelpMessage = "Password used for user switching with su")]
[Parameter(Position = 21,
ParameterSetName = PARAMETER_SET_SHARED_CONNECTION_SU_USER,
Mandatory = true,
ValueFromPipelineByPropertyName = true,
HelpMessage = "Password used for user switching with su")]
public string SuPassword {
set { suPassword = value; }
get { return suPassword; }
}
private string suUser;
[Parameter(Position = 20,
ParameterSetName = PARAMETER_SET_SHARED_CONNECTION_SU_USER,
Mandatory = true,
ValueFromPipelineByPropertyName = true,
HelpMessage = "Username to switch to with su; default = \"root\"")]
public string SuUser {
set { suUser = value; }
get { return suUser; }
}
...
}
Probably your curious about