Using Windows PowerShell as an IT Pro – Part 12
In my last post I began looking at variables. Now I will go over types and how they function.Windows PowerShell supports a number of different types such as strings, integers, arrays, etc. You can explicitly specify a type or allow Windows PowerShell to do it dynamically, which is sometimes referred to as automatic type conversion. Type conversions are used when an object of one type is assigned a value that requires another type (such as adding a string to a number). This conversion happens automatically as long as the type was not specified manually, and it is not destructive to the original object.
For example we can assign a new value to the $Loc variable which currently has the System.Management.Automation.PathInfo type, and then use the GetType method to show its new type.
$Loc = "Test"
$Loc.GetType().FullName
Then, assign it a numeric value and check the type again.
$Loc = 3
$Loc.GetType().FullName
$Loc was made an Int32 because the value wasn’t enclosed in quotes and because the value was composed solely of digits. Had it been in quotes, it would have been interpreted as a System.String.
In both cases, Windows PowerShell determined the data type that was the most appropriate for the value of the variable. This should work for most variables but there may be situations where you want the variable type to remain as it is. Suppose you are reading values out of a file and you always want the values to be treated as strings. Some of the values, however, might contain only digits, raising the possibility that Windows PowerShell would treat them as Int32 or another numeric type, which may create problems for your script. If Windows PowerShell does not recognize the value as a string, then all the methods of the System.String class are not available (and your script might rely on one of these unavailable methods).
Manually assigning a type is simple and is done when you create it. Assigning a string to a variable essentially forces the variable to be of the System.String class. Assigning a number to a variable, on the other hand, usually results in the variable becoming an Integer (or, more specifically, an Int32, which can store a specific range of values). For example, we can create a new variable and define it as a string.
[String]$var = 5
$Var.GetType().FullName
Normally, $Var would have been an Int32, but because we defined it as a String, it makes the numeric value we assigned to it a string.
Forcibly declaring variables does have repercussions, though they are not necessarily bad. In the next example, a new variable is created, defined as an integer, and assigned a numeric value. Then, it is assigned a string value.
[Int]$Num = 4
$Num = "test"
As shown above, when we tried to assign a string value to it, an error message was displayed. Because $Num was defined as a Int32, Windows PowerShell expected to convert the string “test” into an integer value. It was unable to do this, nor was it able to change the type of $Num to String.
In my next post we continue to look at types.
No comments:
Post a Comment