home *** CD-ROM | disk | FTP | other *** search
/ Chip 2008 June / CHIP-2008-06.iso / software / PowerShell / PowerShell_Setup_x86.msi / product.cab / about_scriptcmdletparameters.help_EN.txt < prev    next >
Encoding:
Text File  |  2007-10-29  |  9.9 KB  |  379 lines

  1. TOPIC
  2. Script Cmdlet Parameters
  3.  
  4. SHORT DESCRIPTION
  5. How to declare script cmdlet parameters.
  6.  
  7. LONG DESCRIPTION
  8. Script cmdlets can declare their own parameters, as well as access the 
  9. common parameters available to compiled cmdlets.
  10.  
  11. Note: Common parameters include -Verbose, -Debug, -ErrorAction, 
  12.       -ErrorVariable, and -OutVariable.
  13.  
  14. The following example shows a parameter declaration that defines a 
  15. ComputerName parameter that takes an array of strings as input, is 
  16. required, takes input from the pipeline, and its value is validated 
  17. to make sure that it is not set to Null. There are no limits to the 
  18. number of attributes that can be added to a parameter declaration.
  19.  
  20. Param
  21.   (
  22.     [Mandatory]
  23.     [ValueFromPipeline]
  24.     [String[]]
  25.     $ComputerName
  26.   ) 
  27.   
  28.  
  29.  
  30. When declaring parameters there are several attributes that can be added 
  31. to the parameter declaration. These include parameter metadata attributes 
  32. and parameter validation attributes. 
  33.  
  34.  
  35.  
  36. PARAMETER METADATA ATTRIBUTES
  37.  
  38. These attributes define the characteristics of the parameter, such as is 
  39. the parameter required or optional.
  40.  
  41.  
  42. Mandatory Attribute
  43. Syntax: [Mandatory]
  44.  
  45. This attribute indicates that a value must be specified for the parameter. 
  46. If this attribute is not specified, the parameter is 
  47. an optional parameter. The following example shows the declaration of a 
  48. parameter that is required when the cmdlet is run.
  49.  
  50.   Param
  51.   (
  52.     [Mandatory]
  53.     [String[]]
  54.     $ComputerName
  55.   )
  56.  
  57.  
  58.  
  59. Position Attribute
  60. Syntax: [Position(<Int32>)]
  61.  
  62. This attribute specifies the position of the parameter. If this attribute 
  63. is not specified, the parameter name or alias must be explicitly specified 
  64. when setting the parameter. Also, if none of the parameters of a cmdlet 
  65. have positions, then the PowerShell runtime assigns positions to each 
  66. parameter based on the order that they are received. 
  67.  
  68. The following example shows the parameter 
  69. declaration of a parameter whose value must be specified as the first 
  70. argument when the cmdlet is run. Notice that this cmdlet could be run with 
  71. or without specifying the name of the parameter.
  72.  
  73.   Param
  74.   (
  75.     [Position(0)]
  76.     [String[]]
  77.     $ComputerName
  78.   )
  79.  
  80.  
  81. Alias Attribute
  82. Syntax: [Alias(<String[]>)]
  83.  
  84. This attribute specifies an alias that can be used as a short cut in 
  85. place of the parameter name. The following example shows the parameter 
  86. declaration a parameter that can be set using "ComputerName" or "CN".
  87.  
  88.   Param
  89.   (
  90.     [Alias("CN")]
  91.     [String[]]
  92.     $ComputerName
  93.   )
  94.   
  95.  
  96. ParameterSetName Attribute
  97. Syntax: [ParameterSetName(<String>)]
  98.  
  99. This attribute specifies which parameter set a parameter belongs to. If 
  100. no parameter set is specified, the parameter belongs to all parameter 
  101. sets. Also, it is important to remember that each parameter set must 
  102. have one unique parameter that is not a member of any other parameter 
  103. set. The following example shows the parameter declaration of two 
  104. parameters that belong to two different parameter sets. 
  105.  
  106.   Param 
  107.   (
  108.     [Mandatory]
  109.     [ParameterSetName ("Name")]
  110.     [String[]]
  111.     $ComputerName,
  112.  
  113.     [Mandatory]
  114.     [ParameterSetName ("User")]
  115.     [String[]]
  116.     $ComputerUser
  117.   )
  118.  
  119.  
  120. For information on parameter sets, see Cmdlet Parameters in The Windows 
  121. PowerShell Programmer's Guide on MSDN.
  122.  
  123.  
  124. ValueFromPipeline Attribute
  125. Syntax: [ValueFromPipeline]
  126.  
  127. This attribute specifies that the parameter accepts input from pipeline 
  128. object. Specify this attribute if the cmdlet accesses the complete object, 
  129. not just a property of the object. The following example shows the 
  130. parameter declaration of a ComputerName parameter that accepts the input 
  131. object passed to the cmdlet through the pipeline.
  132.  
  133.   Param 
  134.   (
  135.     [ValueFromPipeline]
  136.     [String[]]
  137.     $ComputerName
  138.   )
  139.  
  140.  
  141. ValueFromPipelineByPropertyName Attribute
  142. Syntax: [ValueFromPipelineByPropertyName] 
  143.  
  144. This attribute specifies that the parameter accepts input from a property 
  145. of a pipeline object. Specify this attribute if the parameter accesses a 
  146. a property of the piped-in object that has either the same name or the same 
  147. alias as this parameter. For example, if the cmdlet has a Computer Name 
  148. parameter and the piped in object has a ComputerName property, the value of 
  149. the ComputerName property is assigned to the ComputerName parameter of the 
  150. cmdlet.
  151.  
  152. The following example shows the parameter declaration of a ComputerName 
  153. parameter that accepts input from the ComputerName property of the input 
  154. object passed to the cmdlet.
  155.  
  156.   Param
  157.   (
  158.     [ValueFromPipelineByPropertyName]
  159.     [String[]]
  160.     $ComputerName
  161.   )
  162.  
  163.  
  164. ValueFromRemainingArduments Attribute
  165. Syntax: [ValueFromRemainingArguments] 
  166.  
  167. This attribute specifies that the parameter accepts all the remaining 
  168. arguments that are not bound to the parameters of the cmdlet. The 
  169. following example shows the parameter declaration of a ComputerName 
  170. parameter that accepts all the remaining arguments of the input object 
  171. passed to the cmdlet.
  172.  
  173.   Param 
  174.   (
  175.     [ValueFromRemainingArguments]
  176.     [Object[]]
  177.     $ComputerName
  178.   )
  179.     
  180.  
  181. HelpMessage Attribute
  182. Syntax: [HelpMessage(<String>)] 
  183.  
  184. This attribute specifies a message that contains a short description of 
  185. the cmdlet parameter. The following example shows the parameter 
  186. declaration that provides a description of the parameter.
  187.  
  188.   Param 
  189.   (
  190.     [HelpMessage ("An array of computer names.")]
  191.     [String[]]
  192.     $ComputerName
  193.   )
  194.     
  195.  
  196.  
  197. PARAMETER VALIDATION ATTRIBUTES
  198.  
  199. These attributes define how the Windows PowerShell runtime validates the 
  200. values of script cmdlet parameters.
  201.  
  202.  
  203. AllowNull Attribute
  204. Syntax: [AllowNull]
  205.  
  206. Allows null as the argument of a mandatory cmdlet parameter. In the 
  207. following example, the ComputerName parameter can be Null even though 
  208. it is a mandatory parameter.  
  209.  
  210.   Param
  211.   (
  212.     [Mandatory]
  213.     [AllowNull]
  214.     [String[]]
  215.     $ComputerName
  216.   )
  217.  
  218.  
  219. AllowEmptyString Attribute
  220. Syntax: [AllowEmptyString]
  221.  
  222. Allows an empty string as the argument of a mandatory cmdlet parameter. 
  223. In the following example, the ComputerName parameter can be an empty 
  224. string ("") even though it is a mandatory parameter.  
  225.  
  226.   Param
  227.   (
  228.     [Mandatory]
  229.     [AllowEmpty]
  230.     [String]
  231.     $ComputerName
  232.   )
  233.  
  234.  
  235. AllowEmptyCollection Attribute
  236. Syntax: [AllowEmptyCollection]
  237.  
  238. Allows an empty collection as the argument of a mandatory cmdlet parameter. 
  239. In the folcollection even though it is a mandatory parameter.  
  240.  
  241.   Param
  242.   (
  243.     [Mandatory]
  244.     [AllowEmpty]
  245.     [String]
  246.     $ComputerName
  247.   )
  248.  
  249.  
  250. ValidateCount Attribute
  251. Syntax: [ValidateCount(<Int32>,<Int32>)]
  252.  
  253. This attribute specifies the minimum and maximum number of arguments that 
  254. the parameter can accept. The Windows PowerShell runtime generates an 
  255. error if the number of arguments is outside that range. In the following 
  256. example, the ComputerName parameter can have 1 to 5 arguments.  
  257.  
  258.   Param
  259.   (
  260.     [ValidateCount(1,5)]
  261.     [String[]]
  262.     $ComputerName
  263.   )
  264.  
  265.  
  266. ValidateLength Attribute
  267. Syntax: [ValidateLengtht(<Int32>,<Int32>)]
  268.  
  269. This attribute specifies the minimum and maximum length of the parameter 
  270. argument. The Windows PowerShell runtime generates an error if the length 
  271. of the parameter argument is outside that range. In the following example, 
  272. the specified computer names must have 1 to 10 characters. 
  273.  
  274.   Param
  275.   (
  276.     [ValidateLength(1,10)]
  277.     [String[]]
  278.     $ComputerName
  279.   )
  280.  
  281.  
  282.  
  283. ValidatePattern Attribute
  284. Syntax: [ValidatePatternt(<String)]
  285.  
  286. This attribute specifies a regular expression that validates the pattern 
  287. of the parameter argument. The Windows PowerShell runtime generates an 
  288. error if the parameter argument does not match this pattern. In the 
  289. following example, the argument of the parameter must be a four digit 
  290. number, with each digit being a number 0 to 9.  
  291.  
  292.   Param
  293.   (
  294.     [ValidatePattern("[0-9][0-9][0-9][0-9]")]
  295.     [String[]]
  296.     $ComputerNames
  297.   )
  298.  
  299.  
  300. ValidateRange Attribute
  301. Syntax: [ValidateRange(<Int32>,<Int32>)]
  302.  
  303. This attribute specifies the minimum and maximum values of the parameter 
  304. argument. The Windows PowerShell runtime generates an error if the 
  305. parameter argument is outside that range. In the following example, the 
  306. argument of the parameter cannot be less than 0 or grater than 10.  
  307.  
  308.   Param
  309.   (
  310.     [ValidateRange(0, 10)]
  311.     [Int[]]
  312.     $Count
  313.   )
  314.  
  315.  
  316. ValidateScript Attribute
  317. Syntax: [ValidateScript(<ScriptBlock>)]
  318.  
  319. This attribute specifies a script that is used to validate the parameter 
  320. argument. The Windows PowerShell runtime generates an error if the 
  321. script block result is false or throws an exception.   
  322.  
  323.  
  324. ValidateSet Attribute
  325. Syntax: [ValidateSet(<String[]>]
  326.  
  327.  
  328. This attribute specifies a set of valid values for the argument of the 
  329. parameter. The Windows PowerShell runtime generates an error if the 
  330. parameter argument does not match one of the values in the set. In the 
  331. following example, the argument of the parameter can contain only the 
  332. names Steve, Mary, and Carl.  
  333.  
  334.   Param
  335.   (
  336.     [ValidateSet("Steve", "Mary". "Carl")]
  337.     [String[]]
  338.     $UserNames
  339.   )
  340.  
  341.  
  342. ValidateNotNull Attribute
  343. Syntax: [ValidateNotNull]
  344.  
  345.  
  346. This attribute specifies that the argument of the parameter cannot be 
  347. set to Null. The Windows PowerShell runtime generates an error if the 
  348. parameter value is Null.   
  349.  
  350.   Param
  351.   (
  352.     [ValidateNotNull]
  353.     [String[]]
  354.     $UserNames
  355.   )
  356.  
  357.  
  358. ValidateNotNullOrEmpty Attribute
  359. Syntax: [ValidateNotNulorEmptyl
  360.  
  361. This attribute specifies that the argument of the parameter cannot be set 
  362. to Null or empty. The Windows PowerShell runtime generates an error if the 
  363. parameter is specified but its value is Null, an empty string, or an empty 
  364. array.   
  365.  
  366.   Param
  367.   (
  368.     [ValidateNotNullOrEmpty]
  369.     [String[]]
  370.     $UserNames
  371.   )
  372.  
  373.  
  374.  
  375. SEE ALSO
  376.  
  377. about_ScriptCmdlets
  378. about_ScriptCmdletAttributes
  379. about_ScriptCmdletMethods