2016-05-06 106 views
2

我有这样的PowerShell脚本来检查未使用的IPPowerShell的IP扫描仪

$ipgroupes = "192.168.2" 
$ipstart = 1 
$ipend = 255 

$ipstart..$ipend | ForEach-Object{ 
    $ip = "$ipgroupes.0" -replace "0$",$_ 

If (-not(Resolve-DnsName -Name $ip -QuickTimeout -Type ANY -ErrorAction    
    SilentlyContinue) -And (!(Test-Connection $ip -count 1 -quiet))) 

    { 

     write-host "$ip is not used" -ForegroundColor Green 


    } 



} 

脚本工作正常,但我一直在问从搜索中排除的IP 80到149。我需要如何完成这项工作的帮助?

+0

你可以做'1..79 + 150..255 | ForEach-Object {',(但它不像具有“排除范围”那样灵活)。 – TessellatingHeckler

回答

2

试试这个。撤消排除,您可以同时设置排除值设置为0

$ipgroupes = '192.168.2' 
$iprange = 1..255 
$excluderange = 80..149 

$iprange | Where-Object {$_ -notin $excluderange} | ForEach-Object { 
    "$ipgroupes.$_" 
} 

*编辑:马特的帮助更新

+2

'Where-Object {$ excludestart .. $ excludeend -notcontains $ _}'看起来更简单,因为我们已经在使用数组运算符了。如果不是,你应该使用'-le'和'-ge' – Matt

+0

谢谢安东尼和马特 –