 |
|
|
Accessing PowerShell variables in cmdlet
|
Sort:
|
|
Prev Next |
You are not authorized to post a reply. |
|
| Author |
Messages |
 |
rk
 New Member Posts:1

 |
| 13 Jan 2010 10:47 PM |
|
Hi, I am a newbie to PowerShell. I have a query regarding variables and cmdlets. I need to write a set of cmdlets which perform a set of operations. Almost all these cmdlets work on connection information (similar to database connection). Usability-wise, it would be great for users of my cmdlets to specify this connection information only once (maybe at the top of script in a variable) and all subsequent cmdlets invokation use this information. Is there a way I can see if that variable (of known/predefined name) is declared already from within my cmdlet (written in C#)? I would like to use its value if that variable is defined globally and if it is not, I'd expect it to be provided using an optional parameter. How can this be achieved? Are there any better alternatives? Thanks in advance, rk |
|
|
|
|
George Howarth
 Basic Member Posts:360

 |
| 14 Jan 2010 01:25 AM |
|
I've not really worked on developing custom cmdlets, but I assume you are looking for is something like this?: namespace MyCmdlets { public class ConnectionCmdlets { private string mConnectionVariable1 private string mConnectionVariable2 private string mConnectionVariable3 public ConnectionCmdlets(string ConnectionVariable1, string ConnectionVariable2, string ConnectionVariable3) { mConnectionVariable1 = ConnectionVariable1 mConnectionVariable2 = ConnectionVariable2 mConnectionVariable3 = ConnectionVariable3 } public void Do-Something1() { // use mConnectionVariable1 // use mConnectionVariable2 // use mConnectionVariable3 } public void Do-Something2() { // use mConnectionVariable1 // use mConnectionVariable2 // use mConnectionVariable3 } public void Do-Something3() { // use mConnectionVariable1 // use mConnectionVariable2 // use mConnectionVariable3 } } } Basically, you would have some sort of class, which you instantiate with your connection details from the script, then those details can be used across several cmdlets without having to re-initialise your connection variables? |
|
|
|
|
| You are not authorized to post a reply. |
|
Active Forums 4.3
|
|
 |