• No results found

Validating Variable Contents

In document IderaWP Powershell eBook Part 1.PDF (Page 69-74)

System.Management.Automation.ArgumentTypeConverterAttribute # Delete type specification:

(Get-Variable a).Attributes.Clear()

# Strong type specification is removed; now the variable can

# store text again:

$a = “Test”

$a = “Hello”

$aa = Get-Variable a

$aa.Attributes.Add($(New-Object `

System.Management.Automation.ValidateLengthAttribute ` -argumentList 2,8))

$a = “Permitted”

$a = “This is prohibited because its length is not from 2 to 8 characters”

Because of an invalid value verification (Prohibited because

its length is not from 2 to 8 characters) may not be carried out for the variable “a”.

At line:1 char:3

+ $a <<<< = “Prohibited because its length is not from 2 to 8

Once you assign a specific data type to a variable as shown above, PowerShell will add this information to the variable attributes. .

If you delete the Attributes property, the variable will be unspecific again so in essence you remove the strong type again:

The Attributes property of a PSVariable object can include additional conditions, such as the maximum length of a variable. In the following example, a valid length from two to eight characters is assigned to a variable. An error will be generated if you try to store text that is shorter than two characters or longer than eight characters:

In the above example Add() method added a new .NET object to the attributes with Object. You’ll learn more about New-Object in Chapter 6. Along with ValidateLengthAttribute, there are additional restrictions that you can place on variables.

Examining Strongly Typed Variables

Validating Variable Contents

$email = “[email protected]

$v = Get-Variable email

$pattern = “\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b”

$v.Attributes.Add($(New-Object `

System.Management.Automation.ValidatePatternAttribute ` -argumentList $pattern))

$email = “[email protected]

$email = “invalid@email”

Because of an invalid value verification (invalid@email) may not be carried out for the variable “email”.

At line:1 char:7

+ $email <<<< = “invalid@email”

$age = 18

$v = Get-Variable age

$v.Attributes.Add($(New-Object `

System.Management.Automation.ValidateRangeAttribute ` -argumentList 5,100))

$age = 30

$age = 110

Because of an invalid value verification (110) may not be carried out for the variable “age”.

At line:1 char:7 + $age <<<< = 110

In the following example, the variable must contain a valid e-mail address or all values not matching an e-mail address will generate an error. The e-mail address is defined by what is called a Regular Expression. You’ll learn more about Regular Expressions in Chapter 13.

If you want to assign a set number range to a variable, use ValidateRangeAttribute. The variable $age accepts only numbers from 5 to 100:

Restriction Category

Variable may not be zero ValidateNotNullAttribute

Variable may not be zero or empty ValidateNotNullOrEmptyAttribute

Variable must match a Regular Expression ValidatePatternAttribute Variable must match a particular number range ValidateRangeAttribute Variable may have only a particular set value ValidateSetAttribute

Table 3.6: Options of a PowerShell variable

$email = “[email protected]

$v = Get-Variable email

$pattern = “\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b”

$v.Attributes.Add($(New-Object `

System.Management.Automation.ValidatePatternAttribute ` -argumentList $pattern))

$email = “[email protected]

$email = “invalid@email”

Because of an invalid value verification (invalid@email) may not be carried out for the variable “email”.

At line:1 char:7

+ $email <<<< = “invalid@email”

$age = 18

$v = Get-Variable age

$v.Attributes.Add($(New-Object `

System.Management.Automation.ValidateRangeAttribute ` -argumentList 5,100))

$age = 30

$age = 110

Because of an invalid value verification (110) may not be carried out for the variable “age”.

At line:1 char:7 + $age <<<< = 110

In the following example, the variable must contain a valid e-mail address or all values not matching an e-mail address will generate an error. The e-mail address is defined by what is called a Regular Expression. You’ll learn more about Regular Expressions in Chapter 13.

If you want to assign a set number range to a variable, use ValidateRangeAttribute. The variable $age accepts only numbers from 5 to 100:

Restriction Category

Variable may not be zero ValidateNotNullAttribute

Variable may not be zero or empty ValidateNotNullOrEmptyAttribute

Variable must match a Regular Expression ValidatePatternAttribute Variable must match a particular number range ValidateRangeAttribute Variable may have only a particular set value ValidateSetAttribute

Table 3.6: Options of a PowerShell variable

$age = “yes”

Verification cannot be performed because of an invalid value (don’t know) for the variable “option”.

At line:1 char:8

+ $option <<<< = “don’t know”

If you would like to limit a variable to special key values, ValidateSetAttribute is the right option. The variable $option accepts only the contents yes, no, or perhaps:

Summary

Variables store information. Variables are by default not bound to a specific data type, and once you assign a value to a variable, PowerShell will automatically pick a suitable data type. By strongly-typing variables, you can restrict a variable to a specific data type of your choice. You strongly-type a variable by specifying the data type before the variable name:

You can prefix the variable name with “$” to access a variable. The variable name can consist of numbers, characters, and special characters, such as the underline character “_”. Variables are not case-sensitive. If you’d like to use characters in variable names with special meaning to PowerShell (like parenthesis), the variable name must be enclosed in brackets. PowerShell doesn’t require that variables be specifically created or declared before use.

There are pre-defined variables that PowerShell will create automatically. They are called “automatic variables.” These variables tell you information about the PowerShell configuration. For example, beginning with PowerShell 2.0, the variable $psversiontable will dump the current PowerShell version and versions of its dependencies:

# Strongly type variable a:

PSCompatibleVersions {1.0, 2.0}

SerializationVersion 1.1.0.1 PSRemotingProtocolVersion 2.1

You can change the way PowerShell behaves by changing automatic variables. For example, by default PowerShell stores only the last 64 commands you ran (which you can list with Get-History or re-run with Invoke-History). To make PowerShell remember more, just adjust the variable $MaximumHistoryCount:

PowerShell will store variables internally in a PSVariable object. It contains settings that write-protect a variable or attach a description to it (Table 3.6). It’s easiest for you to set this special variable options by using the New-Variable or Set-Variable cmdlets (Table 3.1).

Every variable is created in a scope. When PowerShell starts, an initial variable scope is created, and every script and every function will create their own scope. By default, PowerShell accesses the variable in the current scope, but you can specify other scopes by adding a prefix to the variable name\: local:, private:, script:, and global:.

PS > $MaximumHistoryCount

64

PS > $MaximumHistoryCount = 1000

PS > $MaximumHistoryCount

1000

Whenever a command returns more

In document IderaWP Powershell eBook Part 1.PDF (Page 69-74)

Related documents