【发布时间】:2016-03-10 16:58:37
【问题描述】:
我试图在某些条件不匹配时弹出错误消息。信息正在通过 csv 文件浏览。
我的问题是我的陈述之一有效,但没有显示任何内容。这是我的条件语句。
$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"}
这就是问题
elseif ($existsnotopen)
{Write-Host -foregroundcolor Green "Host exists and is not open"}
它不会输出主机存在且未打开,我没有收到错误消息。
$hosted 是一个字符串,所以我明白为什么我不能使用-eq。我将其更改为-match。
我使用了$data."Host ID",因为我希望它像在-notin $data."Host ID" 中那样搜索整个电子表格。我尝试使用您尝试过的内容,因为我的状态是“开放”或“不适用”。这是示例数据。
Hostname Status
LOG Open
LOG Not Applicable
NETMAN Open
HD Open
NETMAN Not Applicable
LOG Not Applicable
【问题讨论】:
标签: powershell if-statement powershell-3.0 comparison-operators