2016-05-06 29 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

謝謝安東尼和馬特 –