Author Posts

November 12, 2014 at 11:49 am

Here is my code:

$b = Get-WmiObject -Class Win32_LocalTime  | Select-Object -ExpandProperty Hour
if ($b -contains 15 -or 16 -or 17){
    Write-Host "Time equals $b" -ForegroundColor Yellow}

$b has the value of 14. My comparison operation is evaluating to True and writing host. I'm hitting my head against the desk here!

The goal is if the hour returned is between 15, 16, or 17, do something. If not, do nothing.

November 12, 2014 at 11:56 am

You can't use contains like that

Either do this

$b = Get-WmiObject -Class Win32_LocalTime | Select-Object -ExpandProperty Hour
if ($b -ge 15 -and $b -le 17){
Write-Host "Time equals $b" -ForegroundColor Yellow
}
else {
Write-Host "$b is out of range"
}

OR if you want to use -contains
$h = 15..17
$b = Get-WmiObject -Class Win32_LocalTime | Select-Object -ExpandProperty Hour
if ($h -contains $b){
Write-Host "Time equals $b" -ForegroundColor Yellow
}
else {
Write-Host "$b is out of range"
}

November 12, 2014 at 12:25 pm

Thanks Richard. Appreciate the fast response.