Let’s take the TS stuff out of the equation.
#$TSEnv = New-Object -ComObject Microsoft.SMS.TSEnvironment
#loop through the 3 varaibles and assign the output of the loop to $models
$models = "IsDesktop", "IsLaptop", "IsVM" | foreach{
#Create a new object with the property TSVariable and Value that is being returned to $models
#In the Value, we take a string value and convert it to boolean
New-Object -TypeName PSObject -Property @{"TSVariable"=$_;"Value"="True"}
}
No matter if it’s a string or a Boolean value, your output would look like this:
Value TSVariable
True IsDesktop
True IsLaptop
True IsVM
However, if you look deeper, if we do the conversion to Boolean, we’ll see this:
PS C:\Users\rsimmers> $models[0].Value.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Boolean System.ValueType
If we change the line to
New-Object -TypeName PSObject -Property @{"TSVariable"=$_;"Value"="True"}
Now, True is a string.
PS C:\Users\rsimmers> $models[0].Value.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
At this point, you are doing string comparison, so checking if a string is a boolean value could give false-positive results. If you read through the article link, “This is because PowerShell will convert any string greater than 0 characters to a Boolean ‘true’” You either have to test string to string, or boolean to boolean. Anything else is going to provide inconsistent results, especially across Powershell versions. It’s better to cast or force the variable to the data type than assume Powershell is going to convert it for you. If you read some Powershell books, Powershell does do some automatic conversions, but there are rules:
#This is basically just string concatenation, put these strings together. If you are wrapping single or double quotes around it, basically you saying it's a string...
PS C:\Users\rsimmers> "10" + "30"
1030
#The first item takes precedence, so even though 30 is last, Powershell converts it to a string because the string preceded it
PS C:\Users\rsimmers> "10" + 30
1030
#This is casting, basically saying that I want the data type to be an integer, now Powershell does the math
PS C:\Users\rsimmers> [int]"10" + 30
40
# To the earlier point, an integer preceded the string, so Powershell converts the next number to a integer
PS C:\Users\rsimmers> 30 + "30"
60
Understanding data types and what commands are expecting is integral to using Powershell. Passing a string when a command expects an integer could cause issue and when you see “Type Mismatch”, that is basically saying you passed me something that is not was is defined. You passed me a letter and I expected a number. Hopefully this makes sense.