Reputation: 527
I am working on a script and I want to compare an array to another array, and change a boolean value (from $false to $true) based on a result. This works fine on strings using the Replace method, but that doesn't exist for boolean values. Can anyone tell me how to do this?
$bv
is an array of objects, as follows.
ServerName,Domain,Environment,Tier0
ServerA,usa,dev,$false
ServerB,usa,sit,$false
I am trying to compare that list to another list of Tier0
computers ($t0List
) that looks like this.
ServerB ServerC ServerD
So if there is a match between the ServerName in column 1 of $bv
and an entry in $t0List
, then I want to change the Tier0
column in $bv
to $true
.
foreach ($b in $bv) {
if ($t0List -contains $b.ServerName) {
$b.Tier0.Replace($b.Tier0,$true)
}
}
The error I get with the above code is...
Method invocation failed because [System.Boolean] does not contain a method named 'Replace'.
Upvotes: 1
Views: 363
Reputation: 1598
Maybe this helps you:
You can flip between $true
and $false
just by telling it -not
to be
-not $true
brings back $false
-not $false
brings back
$true
So
foreach ($b in $bv) {
if ($t0List -contains $b.ServerName) {
$b.Tier0 = -not ($b.Tier0)
}
}
or
foreach ($b in $bv) {
if ($t0List -contains $b.ServerName) {
$b.Tier0 = $true
}
}
should work
Upvotes: 0
Reputation: 47832
There's no need to use something like replace, just assign the value with =
:
foreach ($b in $bv) {
if ($t0List -contains $b.ServerName) {
$b.Tier0 = $true
}
}
Want to simplify it even more? Just assign it the result of your -contains
:
foreach ($b in $bv) {
$b.Tier0 = $t0List -contains $b.ServerName
}
Upvotes: 2