6

Reason I'm Doing This

I'm trying to set a token in a file I have. The contents of the token is 1 line in the file, and it's string value is $token=$false

Simplified to test code

When I try to convert this token into a bool value, I'm having some problems. So I wrote test code and found I'm not able to convert the string to a bool value.

[String]$strValue = "$false"
[Bool]$boolValue = $strValue

Write-Host '$boolValue =' $boolValue

This gives the following error...

Cannot convert value "System.String" to type "System.Boolean", parameters of this type only accept booleans or numbers, use $true, $false, 1 or 0 instead.
At :line:2 char:17
+   [Bool]$boolValue <<<<  = $strValue

As you can see, I am using the $false value as is suggested by the error message, but it's not accepting it. Any ideas?

Community
  • 1
  • 1
Fiddle Freak
  • 1,923
  • 5
  • 43
  • 83

1 Answers1

9

In PowerShell, the usual escape character is the backtick. Normal strings are interpolated: the $ symbol is understood and parsed by PowerShell. You need to escape the $ to prevent interpolation. This should work for you:

[String]$strValue = "`$false"

To convert "$true" or "$false" to a boolean in a generic way, you must first drop the leading $:

$strValue = $strValue.Substring(1)

Then convert to boolean:

[Boolean]$boolValue = [System.Convert]::ToBoolean($strValue)

Using your code from your comment, the shortest solution would be:

$AD_Export_TokenFromConfigFile =
   [System.Convert]::ToBoolean(Get-Content $AD_Export_ConfigFile
                               | % {
                                      If($_ -match "SearchUsersInfoInAD_ConfigToken=") {
                                          ($_ -replace '*SearchUsersInfoInAD_ConfigToken*=','').Trim()
                                      }
                                   }.Substring(1))
wonea
  • 4,783
  • 17
  • 86
  • 139
Paul Hicks
  • 13,289
  • 5
  • 51
  • 78
  • Problem is this is assigning it as true. It should be false because that is what the string reads as. Maybe I should just add another if/else statement to handle it. But I would think I wouldn't have to. – Fiddle Freak Mar 29 '16 at 00:58
  • I tried parsing "false" as a boolean, but no luck and same result. The bool is used for a token in a complex design I am writing. So It is preferred. Otherwise instead of just saying `$tokenInUse`, I would have to write `$tokenInUse -eq 'false'` in multiple if statements, for and while loops. I try to keep my code as clean as possible. – Fiddle Freak Mar 29 '16 at 01:03
  • Note: the entire line looks like this `$AD_Export_TokenFromConfigFile = Get-Content $AD_Export_ConfigFile | % {If($_ -match "SearchUsersInfoInAD_ConfigToken=") {($_ -replace '*SearchUsersInfoInAD_ConfigToken*=','').Trim()}}`. This gets the value of `$true` or `$false` as a string. I'm just trying to cast it to a bool, but it always casts as true... – Fiddle Freak Mar 29 '16 at 01:06
  • 1
    ah I see, I can easily just as well right `[String]$strValue = "false"` and then `[Bool]$boolValue = [System.Convert]::ToBoolean($strValue)`. I didn't know to convert (using the system.convert method), the `$` must not be included. Thanks :) It would be nice if they updated that error message to ensure programmers did not include the `$` in their system.conversion method. – Fiddle Freak Mar 29 '16 at 01:11
  • 1
    You could also do the following with `[bool]`: `[bool]::Parse($strValue)` – boeprox Mar 29 '16 at 16:50