1

Morning.

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

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

it throws the following error:

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(',')

This is the original code.

$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"

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

Tr1cky
  • 27
  • 1
  • 4

2 Answers2

4

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 ','

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

"azer,ty".split(',')
$a = "azer,ty"
$a.split(',')
JPBlanc
  • 70,406
  • 17
  • 130
  • 175
  • If the variable `$PSVersionTable` doesn't exist, the version is most likely v1 (according to [this answer](http://stackoverflow.com/a/1825807/1630171)). – Ansgar Wiechers Sep 03 '13 at 09:56
  • Ha. i didn't even realize that it had V1 on there! Over thinking things is bad. Thanks dude. – Tr1cky Sep 09 '13 at 01:16
2

JPBlanc is correct, but the -split operator has been added in PowerShell v2 not v3.

Dirk
  • 666
  • 6
  • 5