Cmdlet and parameter sets

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Patrick Dreyer

    Cmdlet and parameter sets

    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
  • Joe Fawcett

    #2
    Re: Cmdlet and parameter sets



    "Patrick Dreyer" <blackhole@nosp am.netwrote in message
    news:ewtG$MbRJH A.3932@TK2MSFTN GP02.phx.gbl...
    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
    You may have better luck posting this in
    microsoft.publi c.windows.power shell.

    --

    Joe Fawcett (MVP - XML)


    Comment

    • Patrick Dreyer

      #3
      Re: Cmdlet and parameter sets

      Joe Fawcett schrieb:
      >
      "Patrick Dreyer" <blackhole@nosp am.netwrote in message
      news:ewtG$MbRJH A.3932@TK2MSFTN GP02.phx.gbl...
      >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
      >"SharedConnect ionSu")
      >3) run-sshcmd Command SuUser SuPassword (Parameter set
      >"SharedConnect ionSuUser")
      >>
      >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 =
      >"SharedConnect ionSuUser"
      >>
      >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
      >$_.Parameter s | Select
      >@{n="Parameter Name";e={$_.nam e}},@{n="Mandat ory";e={$_.IsMa ndatory}},@{n=" Position";e={$_ .Position}},@{n ="HelpMessage"; e={$_.HelpMessa ge}},@{n="Param eterSet";e={$ps }}
      >| Format-Table
      >}
      >>
      >>
      >Output of parameters.ps1
      >============== ==========
      >>
      >ParameterNam e Mandatory
      >Position HelpMessage ParameterSet
      >------------- ---------
      > -------- ----------- ------------
      >Command True
      >0 Command to execute SharedConnectio n
      >Verbose
      > False -2147483648
      >SharedConnecti on
      >Debug
      > False -2147483648
      >SharedConnecti on
      >ErrorAction
      > False -2147483648
      >SharedConnecti on
      >ErrorVariabl e
      > False -2147483648
      >SharedConnecti on
      >OutVariable
      > False -2147483648
      >SharedConnecti on
      >OutBuffer
      > False -2147483648
      >SharedConnecti on
      >>
      >ParameterNam e 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
      >SharedConnecti onSu
      >Debug
      > False -2147483648
      >SharedConnecti onSu
      >ErrorAction
      > False -2147483648
      >SharedConnecti onSu
      >ErrorVariabl e
      > False -2147483648
      >SharedConnecti onSu
      >OutVariable
      > False -2147483648
      >SharedConnecti onSu
      >OutBuffer
      > False -2147483648
      >SharedConnecti onSu
      >>
      >ParameterNam e 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
      >SharedConnecti onSuUser
      >Debug
      > False -2147483648
      >SharedConnecti onSuUser
      >ErrorAction
      > False -2147483648
      >SharedConnecti onSuUser
      >ErrorVariabl e
      > False -2147483648
      >SharedConnecti onSuUser
      >OutVariable
      > False -2147483648
      >SharedConnecti onSuUser
      >OutBuffer
      > False -2147483648
      >SharedConnecti onSuUser
      >>
      >>
      >Cmdlet implementation
      >============== =======
      >[Cmdlet("Run", "SSHCmd", DefaultParamete rSetName =
      >PARAMETER_SET_ SHARED_CONNECTI ON)]
      >public class RunSSHCmd : PSCmdlet
      >{
      >public const string PARAMETER_SET_S HARED_CONNECTIO N =
      >"SharedConnect ion";
      >public const string PARAMETER_SET_S HARED_CONNECTIO N_SU =
      >"SharedConnect ionSu";
      >public const string PARAMETER_SET_S HARED_CONNECTIO N_SU_USER =
      >"SharedConnect ionSuUser";
      >>
      >private string command;
      >[Parameter(Posit ion = 0,
      >Mandatory = true,
      >ValueFromPipel ine = true,
      >ValueFromPipel ineByPropertyNa me = 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
      You may have better luck posting this in
      microsoft.publi c.windows.power shell.
      >
      Just did so. Thank you for the hint.

      Comment

      Working...