您必须在“-"运算符的右侧提供值表达式 [英] You must provide a value expression on the right-hand side of the '-' operator

查看:78
本文介绍了您必须在“-"运算符的右侧提供值表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好.

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

I have a script that blocks IP's at a specific time if over a threshold of 10 failed logins per day.

它适用于每台服务器,但只有一台.(总有一个!)该服务器特别是 Server 2008(在其他 08 上工作正常)

It works on every server, but one. (there's always one!) This server in particular is Server 2008 (Works fine on other 08's)

它抛出以下错误:

您必须在-"的右侧提供值表达式操作员.在 C:\Users\admin\Desktop\Block.ps1:11 字符:34+ $arRemote = $ar.RemoteAddresses -s <<<<plit(',')

You must provide a value expression on the right-hand side of the '-' operator. At C:\Users\admin\Desktop\Block.ps1:11 char:34 + $arRemote = $ar.RemoteAddresses -s <<<< plit(',')

这是原始代码.

$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 个服务器上显示此错误,但在其他服务器上运行良好.

I honestly do not understand why it would show this error on 1 server, but works fine on the rest.

推荐答案

这当然是因为 -split 运算符存在于 PowerShell V3.0 而不是在以前的版本中(看看$PSVersionTable) 语法是:

This certainly comes to the fact that -split operator exists on PowerShell V3.0 but not in previous versions (have a look to $PSVersionTable) the syntax is :

"aze,rt" -split ','

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

In all Powershell versions you can use the split method of the string class :

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

这篇关于您必须在“-"运算符的右侧提供值表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆