【问题标题】:You must provide a value expression on the right-hand side of the '-' operator您必须在“-”运算符的右侧提供一个值表达式
【发布时间】:2013-09-03 00:29:16
【问题描述】:

早上。

如果超过每天 10 次登录失败的阈值,我有一个脚本会在特定时间阻止 IP。

它适用于每台服务器,但只有一个。 (总有一个!) 该服务器特别是 Server 2008(在其他 08 上运行良好)

它会抛出以下错误:

您必须在“-”的右侧提供一个值表达式 操作员。在 C:\Users\admin\Desktop\Block.ps1:11 char:34 + $arRemote = $ar.RemoteAddresses -s

这是原始代码。

$DT = [DateTime]::Now.AddHours(-24)

$l = Get-EventLog -LogName 'Security' -InstanceId 4625 -After $DT | Select-Object @{n='IpAddress';e={$_.ReplacementStrings[-2]} }

$g = $l | group-object -property IpAddress  | where {$_.Count -gt 10} | Select -property Name 

$fw = New-Object -ComObject hnetcfg.fwpolicy2 

$ar = $fw.rules | where {$_.name -eq 'Blacklist'} 

$arRemote = $ar.RemoteAddresses -split(',') 

$w = $g | where {$_.Name.Length -gt 1 -and !($arRemote -contains $_.Name + '/255.255.255.255') }

$w| %{ 
  if ($ar.RemoteAddresses -eq '*') {
    $ar.remoteaddresses = $_.Name
  }else{
    $ar.remoteaddresses += ',' + $_.Name
  }
}

if ($w.length -gt 1) {
  $w| %{(Get-Date).ToString() + '  ' + $_.Name >> 'C:\blocked.txt'} 
}
clear-eventlog "Security"

老实说,我不明白为什么它会在 1 台服务器上显示此错误,但在其他服务器上运行良好。

【问题讨论】:

    标签: powershell


    【解决方案1】:

    这肯定是因为-split 运算符存在于 PowerShell V3.0 上,但在以前的版本中不存在(看看$PSVersionTable),语法是:

    "aze,rt" -split ','
    

    在所有 Powershell 版本中,您都可以使用 string 类的 split 方法:

    "azer,ty".split(',')
    $a = "azer,ty"
    $a.split(',')
    

    【讨论】:

    • 如果变量$PSVersionTable不存在,则版本很可能是v1(根据this answer)。
    • 哈。我什至没有意识到它上面有V1!想太多是不好的。谢谢老兄。
    【解决方案2】:

    JPBlanc 是正确的,但是在 PowerShell v2 而不是 v3 中添加了 -split 运算符。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-19
      • 2013-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多