Freigeben über


Did you know? #3

Did you know that you can put constraints on variables using the same
attributes as cmdlet parameters?
Unfortunately we had to postpone the language support for this but you can
still do it using new-object.

MSH > $a = "foo"
MSH > $varA = get-variable a
MSH > $varA

Name                           Value
----                           -----
a                              foo

MSH > $varA.Attributes.Add($(new-object
System.Management.Automation.ValidateLengthAttribute -arg 1,10))
MSH > $a = ""
 : Cannot validate because of invalid value () for variable a.
At line:1 char:3
+ $a  <<<< = ""
MSH > $a = "somethingreallylong"
 : Cannot validate because of invalid value (somethingreallylong) for
variable
a.
At line:1 char:3
+ $a  <<<< = "somethingreallylong"
MSH > $a = "just right"
MSH > $a
just right

-Jeff Jones

Comments

  • Anonymous
    August 26, 2005
    So why bother with using $varA in this example? Couldn't you just have applied those constraints directly to $a?
  • Anonymous
    August 28, 2005
    Postponing means a later beta or V2?
  • Anonymous
    August 28, 2005
    Variable constraint is nice feature but what would be more interesting for me are constraints and other attributes on script parameters and script itself. This would be very useful because these attributes can define contract and contain documentation. And I really miss -? on scripts (built-in documentation support).

    I know that Cmdlets offer more features than scripts but I cannot use Cmdlets for these reasons:
    1. As far as I know custom Cmdlets cannot be used in original msh.exe, you must build your own shell. It is impossible to force other colleaques to use particular shell because they might want to write their own Cmdlets.
    2. Cmdlets must be compiled first. This is more practical reason than principal problem but consider situation where Java developers work on Windows but master-build runs on Linux.

    So based on these Cmdlets problems I want to be able to do in script everything what can be done in Cmdlet.

    I love Monad, it is excelent and my expectations are big :-)
    Thank you