Powershell-批量清理DHCP BAD_ADDRESS地址

1.Powershell查询DHCP BAD_Address列表

需求:

DHCP服务器地址池频繁被Bad_Address垃圾请求打满;

安全及网络同事在溯源排错过程中,需要及时清理垃圾地址信息,防止新用户无法正常获取IP地址信息;

640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1


查询当前作用域信息:

Get-DhcpServerv4Lease -ComputerName 10.78.0.226 -ScopeId 10.78.48.0

640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1

查询当前地址池主机名为BAD_Address:

Get-DhcpServerv4Lease -ComputerName 10.78.0.226 -ScopeId 10.78.48.0 |where {$_.Hostname -like "BAD*" }
Get-DhcpServerv4Lease语法:
Get-DhcpServerv4Lease [-ScopeId] <ipaddress> [<CommonParameters>]
Get-DhcpServerv4Lease  [<CommonParameters>]
Get-DhcpServerv4Lease [-ScopeId] <ipaddress> [-ClientId] <string[]> [<CommonParameters>]
Get-DhcpServerv4Lease [[-ScopeId] <ipaddress>] [<CommonParameters>]
描述:
获取从动态主机配置协议(DHCP)服务器服务的一个或多个租赁记录。
如果指定ScopeId参数,则返回指定范围内的活动租约。要获得包括Active,Offered,Declined和Expired在内的各种租约,必须指定AllLeases参数。
如果指定IPAddress参数,则返回指定IP地址的租约记录。
如果指定ClientId和ScopeId参数,则返回指定范围内指定ClientId参数值的租约。
如果指定BadLeases和ScopeId参数,则返回指定范围的所有错误租约记录。
如果指定不带ScopeId参数的BadLeases参数,则返回DHCP服务器服务中的所有错误租约记录。
 
 

2.批量清理无效Bad_Address地址池

批量过滤并清理无效BAD_Address地址释放地址空间:

$Computername = "10.78.0.226"
$Scopeid = "10.78.48.0"
Import-Module DHCPServer
foreach ($Object in Get-DhcpServerv4Lease -ComputerName $Computername -ScopeId $Scopeid )
{
if ($object.HostName –like 'BAD_A*')
{
Remove-DhcpServerv4Lease -ComputerName $Computername -ScopeId $Scopeid -ClientId $object.ClientId
}
}

640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1

语法:
Remove-DhcpServerv4Lease [<CommonParameters>]
Remove-DhcpServerv4Lease [-ScopeId] <ipaddress> [<CommonParameters>]
Remove-DhcpServerv4Lease [-ScopeId] <ipaddress> [-ClientId] <string[]> [<CommonParameters>]
Remove-DhcpServerv4Lease [[-ScopeId] <ipaddress>] [<CommonParameters>]
描述:
删除从动态主机配置协议(DHCP)服务器服务的一个或多个IPv4租约记录。
如果指定了ScopeId参数,则删除指定范围的所有租约。
如果指定了IPAddress参数,则删除由一个或多个指定IP地址标识的客户端的租约。如果指定了ClientId和ScopeId参数,则会删除指定范围内指定客户端标识符(ID)的租约。
如果指定了BadLeases和ScopeId参数,则此cmdlet将删除指定范围的所有错误租约记录。
如果在没有ScopeId参数的情况下指定了BadLeases参数,则此cmdlet将从DHCP服务器服务上的所有作用域中删除所有错误租约。


640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1

猜你喜欢

转载自blog.51cto.com/wenzhongxiang/2428015