473,804 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Nov 13 '08 #1
2 3744


"Patrick Dreyer" <bl*******@nosp am.netwrote in message
news:ew******** ******@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)
http://joe.fawcett.name

Nov 13 '08 #2
Joe Fawcett schrieb:
>
"Patrick Dreyer" <bl*******@nosp am.netwrote in message
news:ew******** ******@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.
Nov 13 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
30341
by: rg | last post by:
Hi all, I have query about passing a parameter from form to a query. My situation is as follows: I have a query that pulls up some data from a table, however there is a condition that must be filled in by the user. Hence when you run the query there is a "Enter Parameter Value" prompt that must be completed. A further complication is that the parameter is actually within the WHERE statement, i.e.:
1
1831
by: Jim Heavey | last post by:
If I trying to write code which is not dependant on which database I am using. I have developed the code for SQL Server and it is working just fine, and now I have created the same data in MS Access. I am currently running all stored procedures. I know that I have run "procedures" in access, but I do not quite remember how to set this up. When I build the query in Access, how do I identify a parameter value is required and where? Do I...
21
5773
by: Marc DVer | last post by:
I am trying to create a query that can be loaded as a querydef object but not having to assign values to the parameters if I don't want to. Normally when using a parameter query in VBA my code would go something like this: dim qry as dao.querydef set qry = currentdb.querydefs("myquery") qry.parameters("Par1") = "blah"
3
2056
by: Hewit | last post by:
I have a stored procedure which returns records and output parameters(SQL2K). How to call this stored procedure using ADO.Net so that I can use both results in my application. I have .Net Framework 1.1. when I use ExecuteReader() of Command object I cannot read the output parametrs of the stored procedure in code. Please advice. Hewit
8
2016
by: Patreek | last post by:
Hi, On the line where I'm assigning RecordCount to be the value of my output parameter, I'm getting the generic "Object reference not set to an instance of an object" error. I've isolated it down to this line, as the line of code commented out just beneath it runs fine. Can anyone see why my parameter isn't an object? When I grab the command from SQL profiler and run it in Query Analyzer, I get my output parameter returned with a...
8
2706
by: Alec MacLean | last post by:
Hi, I'm using the DAAB Ent Lib (Jan 2006) for .NET 2.0, with VS 2005 Pro. My project is a Web app project (using the WAP add in). Background: I'm creating a survey system for our company, for which invites will target selected personnel among our customers via email. Each email will provide a custom hyperlink for each respondent using a SQL generated GUID value in the querystring. The GUID will be checked for validity before the user...
9
2701
by: serge | last post by:
/* Subject: How to build a procedure that returns different numbers of columns as a result based on a parameter. You can copy/paste this whole post in SQL Query Analyzer or Management Studio and run it once you've made sure there is no harmful code. Currently we have several stored procedures which final result is a select with several joins that returns many
1
2487
by: bhavu10 | last post by:
i got date parameter but it shows only 2007 records and not for other year and would like to know where i have writte code wrong it shows month only from 1/3/2007 to 2/4/2007 not able to figureout y. Option Compare Database Private Sub cmdMonth_Click() Public Function fncFirstofMonth(dtmDate As Date) As Date Dim dtmResult As Date fncFirstofMonth = DateSerial(Year(dtmDate), Month(dtmDate), 1)
0
1241
by: Alexander Vasilevsky | last post by:
Hi! What a difference PowerShell Cmdlet and PSCmelet? -- http://www.alvas.net - Audio tools for C# and VB.Net developers
0
9711
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10343
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10088
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9169
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5529
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5668
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.