[Powershell] Know your Variable Type

You can define variables in your Powershell script by various means, it’s a good idea to check if the parameter type is defined as expected,

in one of the scripts I came across, one command was failing with this error:

Cannot process argument transformation on parameter <Variable Name>. Cannot convert value to type System.String.

Displaying the parameter alone, I found it looks as an array, also I did query the variable type, and of course it was not System.String which is required by the command to complete:

 $MyVariable.GetType().FullName

the result can be one of the following, weather it’s correct or no, it depends on how you plan to use this variable in your script:

Alias Type
[int] 32-bit signed integer
[long] 64-bit signed integer
[string] Fixed length string of Unicode characters
[char] A Unicode 16-bit character
[bool] True/False value
[byte] An 8-bit unsigned integer
[double] Double-precision 64-bit floating point number
[decimal] A 128-bit decimal value
[single] Single precision 32-bit floating point number
[array] An array of values
[xml] XML objects
[hashtable] A hashtable object (similar to a dictionaryobject)

For my case, We redefined the variable so it only hold the correct single value, and hence $MyVariable.GetType().FullName returned: System.String

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s