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 "SharedConnecti on")
2) run-sshcmd Command SuPassword (Parameter set "SharedConnecti onSu")
3) run-sshcmd Command SuUser SuPassword (Parameter set "SharedConnecti onSuUser")
1) "run-sshcmd ls" -ok, ParameterSetNam e = "SharedConnecti on"
2) "run-sshcmd ls test" -ERROR, requests SuPassword
3) "run-sshcmd ls root test" -ok, ParameterSetNam e = "SharedConnecti onSuUser"
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).Paramet erSets | foreach {
$ps = $_.name
$_.Parameters | Select @{n="ParameterN ame";e={$_.name }},@{n="Mandato ry";e={$_.IsMan datory}},@{n="P osition";e={$_. Position}},@{n= "HelpMessage";e ={$_.HelpMessag e}},@{n="Parame terSet";e={$ps} } | Format-Table
}
Output of parameters.ps1
=============== =========
ParameterName Mandatory Position HelpMessage ParameterSet
------------- --------- -------- ----------- ------------
Command True 0 Command to execute SharedConnectio n
Verbose False -2147483648 SharedConnectio n
Debug False -2147483648 SharedConnectio n
ErrorAction False -2147483648 SharedConnectio n
ErrorVariable False -2147483648 SharedConnectio n
OutVariable False -2147483648 SharedConnectio n
OutBuffer False -2147483648 SharedConnectio n
ParameterName Mandatory Position HelpMessage ParameterSet
------------- --------- -------- ----------- ------------
Command True 0 Command to execute SharedConnectio nSu
SuPassword True 21 Password used for user swi... SharedConnectio nSu
Verbose False -2147483648 SharedConnectio nSu
Debug False -2147483648 SharedConnectio nSu
ErrorAction False -2147483648 SharedConnectio nSu
ErrorVariable False -2147483648 SharedConnectio nSu
OutVariable False -2147483648 SharedConnectio nSu
OutBuffer False -2147483648 SharedConnectio nSu
ParameterName Mandatory Position HelpMessage ParameterSet
------------- --------- -------- ----------- ------------
Command True 0 Command to execute SharedConnectio nSuUser
SuPassword True 21 Password used for user swi... SharedConnectio nSuUser
SuUser True 20 Username to switch to with... SharedConnectio nSuUser
Verbose False -2147483648 SharedConnectio nSuUser
Debug False -2147483648 SharedConnectio nSuUser
ErrorAction False -2147483648 SharedConnectio nSuUser
ErrorVariable False -2147483648 SharedConnectio nSuUser
OutVariable False -2147483648 SharedConnectio nSuUser
OutBuffer False -2147483648 SharedConnectio nSuUser
Cmdlet implementation
=============== ======
[Cmdlet("Run", "SSHCmd", DefaultParamete rSetName = PARAMETER_SET_S HARED_CONNECTIO N)]
public class RunSSHCmd : PSCmdlet
{
public const string PARAMETER_SET_S HARED_CONNECTIO N = "SharedConnecti on";
public const string PARAMETER_SET_S HARED_CONNECTIO N_SU = "SharedConnecti onSu";
public const string PARAMETER_SET_S HARED_CONNECTIO N_SU_USER = "SharedConnecti onSuUser";
private string command;
[Parameter(Posit ion = 0,
Mandatory = true,
ValueFromPipeli ne = true,
ValueFromPipeli neByPropertyNam e = true,
HelpMessage = "Command to execute")]
[ValidateNotNull OrEmpty]
public string Command {
set { command = value; }
get { return command; }
}
private string suPassword;
[Parameter(Posit ion = 21,
ParameterSetNam e = PARAMETER_SET_S HARED_CONNECTIO N_SU,
Mandatory = true,
ValueFromPipeli neByPropertyNam e = true,
HelpMessage = "Password used for user switching with su")]
[Parameter(Posit ion = 21,
ParameterSetNam e = PARAMETER_SET_S HARED_CONNECTIO N_SU_USER,
Mandatory = true,
ValueFromPipeli neByPropertyNam e = true,
HelpMessage = "Password used for user switching with su")]
public string SuPassword {
set { suPassword = value; }
get { return suPassword; }
}
private string suUser;
[Parameter(Posit ion = 20,
ParameterSetNam e = PARAMETER_SET_S HARED_CONNECTIO N_SU_USER,
Mandatory = true,
ValueFromPipeli neByPropertyNam e = 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 "SharedConnecti on")
2) run-sshcmd Command SuPassword (Parameter set "SharedConnecti onSu")
3) run-sshcmd Command SuUser SuPassword (Parameter set "SharedConnecti onSuUser")
1) "run-sshcmd ls" -ok, ParameterSetNam e = "SharedConnecti on"
2) "run-sshcmd ls test" -ERROR, requests SuPassword
3) "run-sshcmd ls root test" -ok, ParameterSetNam e = "SharedConnecti onSuUser"
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).Paramet erSets | foreach {
$ps = $_.name
$_.Parameters | Select @{n="ParameterN ame";e={$_.name }},@{n="Mandato ry";e={$_.IsMan datory}},@{n="P osition";e={$_. Position}},@{n= "HelpMessage";e ={$_.HelpMessag e}},@{n="Parame terSet";e={$ps} } | Format-Table
}
Output of parameters.ps1
=============== =========
ParameterName Mandatory Position HelpMessage ParameterSet
------------- --------- -------- ----------- ------------
Command True 0 Command to execute SharedConnectio n
Verbose False -2147483648 SharedConnectio n
Debug False -2147483648 SharedConnectio n
ErrorAction False -2147483648 SharedConnectio n
ErrorVariable False -2147483648 SharedConnectio n
OutVariable False -2147483648 SharedConnectio n
OutBuffer False -2147483648 SharedConnectio n
ParameterName Mandatory Position HelpMessage ParameterSet
------------- --------- -------- ----------- ------------
Command True 0 Command to execute SharedConnectio nSu
SuPassword True 21 Password used for user swi... SharedConnectio nSu
Verbose False -2147483648 SharedConnectio nSu
Debug False -2147483648 SharedConnectio nSu
ErrorAction False -2147483648 SharedConnectio nSu
ErrorVariable False -2147483648 SharedConnectio nSu
OutVariable False -2147483648 SharedConnectio nSu
OutBuffer False -2147483648 SharedConnectio nSu
ParameterName Mandatory Position HelpMessage ParameterSet
------------- --------- -------- ----------- ------------
Command True 0 Command to execute SharedConnectio nSuUser
SuPassword True 21 Password used for user swi... SharedConnectio nSuUser
SuUser True 20 Username to switch to with... SharedConnectio nSuUser
Verbose False -2147483648 SharedConnectio nSuUser
Debug False -2147483648 SharedConnectio nSuUser
ErrorAction False -2147483648 SharedConnectio nSuUser
ErrorVariable False -2147483648 SharedConnectio nSuUser
OutVariable False -2147483648 SharedConnectio nSuUser
OutBuffer False -2147483648 SharedConnectio nSuUser
Cmdlet implementation
=============== ======
[Cmdlet("Run", "SSHCmd", DefaultParamete rSetName = PARAMETER_SET_S HARED_CONNECTIO N)]
public class RunSSHCmd : PSCmdlet
{
public const string PARAMETER_SET_S HARED_CONNECTIO N = "SharedConnecti on";
public const string PARAMETER_SET_S HARED_CONNECTIO N_SU = "SharedConnecti onSu";
public const string PARAMETER_SET_S HARED_CONNECTIO N_SU_USER = "SharedConnecti onSuUser";
private string command;
[Parameter(Posit ion = 0,
Mandatory = true,
ValueFromPipeli ne = true,
ValueFromPipeli neByPropertyNam e = true,
HelpMessage = "Command to execute")]
[ValidateNotNull OrEmpty]
public string Command {
set { command = value; }
get { return command; }
}
private string suPassword;
[Parameter(Posit ion = 21,
ParameterSetNam e = PARAMETER_SET_S HARED_CONNECTIO N_SU,
Mandatory = true,
ValueFromPipeli neByPropertyNam e = true,
HelpMessage = "Password used for user switching with su")]
[Parameter(Posit ion = 21,
ParameterSetNam e = PARAMETER_SET_S HARED_CONNECTIO N_SU_USER,
Mandatory = true,
ValueFromPipeli neByPropertyNam e = true,
HelpMessage = "Password used for user switching with su")]
public string SuPassword {
set { suPassword = value; }
get { return suPassword; }
}
private string suUser;
[Parameter(Posit ion = 20,
ParameterSetNam e = PARAMETER_SET_S HARED_CONNECTIO N_SU_USER,
Mandatory = true,
ValueFromPipeli neByPropertyNam e = true,
HelpMessage = "Username to switch to with su; default = \"root\"")]
public string SuUser {
set { suUser = value; }
get { return suUser; }
}
...
}
Probably your curious about
Comment