I am trying to get in error message to pop when certain conditions do not match. The information is browsing through a csv file.
My issue is one of my statements work but does not display anything. Here is my condition statement.
$spellingerror = $hostid -notin $data."Host ID"
$existsnotopen = $hostid -eq $data."Host ID" -and $_.Status -ne $data.Open
if ($spellingerror)
{Write-Host -foregroundcolor Green "Host does not exist"}
elseif ($existsnotopen)
{Write-Host -foregroundcolor Green "Host exists and is not open"}
else
{$data | Sort-Object {[DateTime]$_."Last Modified"} | Where-Object
{$_."Host ID" -eq $hostid -and $_."Status" -eq "Open"} |
Select -First 1 -ExpandProperty "Last Modified"}
this is the issue
elseif ($existsnotopen)
{Write-Host -foregroundcolor Green "Host exists and is not open"}
it will not output Host exists and is not open, I do not get an error message.
$hosted
is a string so I see why I cant use -eq
. I changed it to -match
.
I used $data."Host ID"
because I wanted it to search the whole spreadsheet like I did in -notin $data."Host ID"
. I tried using what you tried because my status are either "Open" or "not applicable". Here is sample data.
Hostname Status
LOG Open
LOG Not Applicable
NETMAN Open
HD Open
NETMAN Not Applicable
LOG Not Applicable
$existsnotopen = $hostid -eq $data."Host ID" -and $_.Status -ne $data.Open
As far as I can tell from your script (without sample data): $hostid
is a single value, ex. $hostid = $_."Host ID"
, while $data
is an array considering you use -notin $data."Host ID"
in the line above.
A string will not be equal an array. Also, $_.Status -ne $data.Open
will always be true since a single value (Status) will never be an array ($data.Open
).
UPDATE: This would also fail since there's nothing called $data.Open
, Open
is a Status
-value.
Try:
$existsnotopen = @($data | Where-Object { $hostid -eq $_."Host ID" -and $_.Status -ne "Open" }).Count -gt 0