April 25th, 2006

Did you know? #3

PowerShell Team
PowerShell Team

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

[Edit: Monad has now been renamed to Windows PowerShell. This script or discussion may require slight adjustments before it applies directly to newer builds.]

Category
PowerShell

Author

PowerShell Team
PowerShell Team

PowerShell is a task-based command-line shell and scripting language built on .NET. PowerShell helps system administrators and power-users rapidly automate tasks that manage operating systems (Linux, macOS, and Windows) and processes.

0 comments

Discussion are closed.