在我的办公室(大约7000台计算机),每台PC都有IPv4预留安全措施。
如果更换了计算机,则需要清理预约,但可能有多个范围。
我创建了一个脚本,用于搜索您提供的MAC地址,通过每个范围但在每个未找到该MAC地址的范围内都会生成错误。
删除IP保留有效,但我希望脚本执行的操作如下: 首先,它应该在范围列表中搜索计算机所在的正确范围,然后它应该执行实际删除预留的代码。
另外,我尝试在任何范围内都找不到MAC地址时给出文本输出,但这似乎也不起作用。
这是我的代码:
Write-Host "remove mac-address"
$Mac = Read-Host "Mac-Adres"
$ScopeList = Get-Content sometxtfilewithscopes.txt
foreach($Scope in $Scopelist)
{
Remove-DhcpServerv4reservation -ComputerName #ipofdhcpserver# -ClientId $Mac -ScopeId $scope -erroraction SilentlyContinue -PassThru -Confirm -OutVariable NotFound | Out-Null
}
if ($NotFound -eq $false ) {
Write-Host "MAC-address not found!"
}
pause
答案 0 :(得分:1)
尝试类似这样的东西(这是我用于类似的东西):
$mac = Read-Host "Enter MAC Address"
if ($mac -eq $null) { Write-Error "No MAC Address Supplied" -ErrorAction Stop }
$ServerName = "mydhcpserver.mydomain.net"
$ScopeList = Get-DhcpServerv4Scope -ComputerName $ServerName
ForEach ($dhcpScope in $ScopeList) {
Get-DhcpServerv4Reservation -ScopeId $dhcpScope.ScopeId -ComputerName $ServerName | `
Where {($_.ClientID -replace "-","").ToUpper() -eq $mac.ToUpper()} | `
ForEach {
Try {
Remove-DhcpServerv4Reservation -ClientId $_.ClientID -ScopeId $dhcpScope.ScopeId -Server $ServerName -WhatIf
} catch {
Write-Warning ("Error Removing From Scope" + $dhcpScope.ScopeId)
}
}
}
答案 1 :(得分:1)
让PowerShell为您完成所有繁重的工作:
$mac = Read-Host 'Enter MAC address'
$server = 'yourdhcpserver'
$reservation = Get-DhcpServerv4Scope -Computer $server |
Get-DhcpServerv4Reservation -Computer $server |
Where-Object { $_.ClientId -eq $mac }
if ($reservation) {
$reservation | Remove-DhcpServerv4Reservation -Computer $server
} else {
"$mac not found."
}
以上假设输入的MAC地址的格式为##-##-##-##-##-##
。如果您还想允许冒号(##:##:##:##:##:##
),则需要在使用Where-Object
过滤器中的地址之前用连字符替换冒号:
$mac = $mac -replace ':', '-'