Yubz
Yubz

Reputation: 25

Powershell Return only TRUE if All Values are the same

I have the script below to read registry values from a certain key(taking no credit for it). My end goal is to only return TRUE if all the values in the array Match. However I'm not quite getting it as

Example Registry Entry

$array = @()
$regval = Get-Item -Path HKLM:\SOFTWARE\Runner\Event
$regval.GetValueNames() | 
ForEach-Object {
$name = $_
        $rv.Value
        $array += New-Object psobject -Property @{'Value' = $rv.Value }
}

$Matchvalue = 'A'

Foreach ($v in $array){
if ($v -match $Matchvalue){
$true
}
}

Update: I've just tried again and it appears my array is empty. So any tips welcome for me.

Upvotes: 1

Views: 2263

Answers (2)

js2010
js2010

Reputation: 27423

Here's my attempt to do something like Haskell's "all".

function foldr ($sb, $accum, $list) {
  if ($list.count -eq 0) { $accum }
  else { & $sb $list[0] (foldr $sb $accum $list[1..$list.length]) }
}

function and ($list) {
  foldr { $args[0] -and $args[1] } $true $list
}

function all ($list, $sb) { 
  and ( $list | foreach $sb ) 
}


all 1,1,1 { $_ -eq 1 }
True

all 1,2,1 { $_ -eq 1 }
False

Upvotes: 0

Tomalak
Tomalak

Reputation: 338208

How about this:

$regkey = Get-Item HKLM:\SOFTWARE\Runner\Event
$matchPattern = 'A'

$values = $regkey.GetValueNames()
$matchingValues = $values | Where { $regkey.GetValue($_) -match $matchPattern }

# this is going to be true or false
$values.Count -eq $matchingValues.Count

Note that by default, Powershell is case-insensitive. So $matchPattern = 'A' and $matchPattern = 'a' will behave the same.

Upvotes: 1

Related Questions