tailieunhanh - Professional Windows PowerShell Programming phần 4

là một phần mặc định của Windows PowerShell. Khi một snap-in được tải trong Windows PowerShell, tất cả các cmdlet và các nhà cung cấp trong snap-in có sẵn cho người dùng. Mô hình này cho phép các quản trị viên để tùy chỉnh shell bằng cách thêm | Chapter 4 Developing Cmdlets Parameter Mandatory true ValidateEvenNumber public int Number get return number set number value protected override void ProcessRecord Now try running the Get-EvenNumber cmdlet with an odd argument value for the Number parameter PS C user gxie get-evennumber -number 13 Get-EvenNumber Cannot validate argument on parameter Number . Not an even num Ber. At line 1 char 23 get-evennumber -number 13 As you can see an error is reported because even number validation failed. Parameter Transformation In cmdlet development parameters can be defined as any .NET types from simple types such as string int to complicated types such as . One common task however is to design the cmdlet so that parameter values can be easily typed in from the command line. For example assume that you want to develop a cmdlet Unite-Rectangle which calculates the union of two rectangles Using Cmdlet Unite Rectangle public class UniteRectangleCommand PSCmdlet Rectangle rectangle1 new Rectangle 0 0 0 0 Parameter Mandatory true Position 1 public Rectangle Rectangle1 80 Chapter 4 Developing Cmdlets get return rectanglei set rectanglei value Rectangle rectangle2 new Rectangle 0 0 0 0 Parameter Mandatory true Position 2 public Rectangle Rectangle2 get return rectangle2 set rectangle2 value protected override void ProcessRecord WriteObject rectangle1 rectangle2 You can see that both parameters Rectanglei and Rectangle2 have the type . Rectangle. To specify rectangle values for these command parameters you would have to create rectangle objects first using the new-object cmdlet and then pass them to the new cmdlet as shown in the following example PS C user gxie ri new-object 1 2 1 1 PS C user gxie r2 new-object 3 4 1 1 PS C user gxie Unite-Rectangle r1 r2 Location Size X Y Width Height Left X 1 Y 2 Width 3 Height 3 1 2 3 3 1 81 Chapter 4 Developing Cmdlets Top 2 Right 4 .

TỪ KHÓA LIÊN QUAN