【问题标题】:How to get the rogue DHCP clients using PowerShell?如何使用 PowerShell 获取恶意 DHCP 客户端?
【发布时间】:2019-10-31 08:43:22
【问题描述】:

我需要获取不是 AD 域成员但从 DHCP 获得租约的客户端/计算机的列表。

我使用的代码看起来适用于其他用户,但我的输出文件中没有任何主机名。

Get-DhcpServerv4Lease -allleases -ScopeId 191.168.1.1 | Select-Object @{expression= {$_.hostname}; label='name' } | export-CSV -notypeinformation C:\dhcp\LeaseLog.csv

$leaselogpath = "c:\DHCP\LeaseLog.csv"
Import-csv -path $leaselogpath | foreach-object {
    $ComputerName = $_.name.Replace(".domain.com",$null)
    $Result = Get-ADComputer $ComputerName
    If ($Result -eq $null) {
        $RogueSystem = $_.Name
    }
    $RogueSystem | Out-File C:\DHCP\RogueClients.txt -Append $RogueSystem = $null
}

执行时,我收到有关域中缺少计算机的错误消息 - 正是我需要在输出文件中列出的计算机,但输出文件仍然为空。

Get-ADComputer:找不到标识为“TEST-PC1”的对象....

【问题讨论】:

  • 您发布的代码由于换行符不正确(或更具体地说,缺少)而损坏。请edit您的问题并复制/粘贴您在脚本中的代码。当您发布的代码引入了您尝试调试的代码中不存在的其他问题时,我们无法为您提供帮助。

标签: powershell dhcp


【解决方案1】:

看来问题出在重置 $Result


$ScopeID = Get-DhcpServerv4Scope -ComputerName DC1 | select ScopeID

$ScopeID | foreach-object `
{
    Get-DhcpServerv4Lease -allleases -ScopeId $_.ScopeID -ComputerName DC1  | Select-Object @{expression= {$_.hostname}; label='name' } | export-CSV -notypeinformation C:\dhcp\LeaseLog.csv -Append
}

$leaselogpath = "c:\DHCP\LeaseLog.csv"
Import-csv -path $leaselogpath | 
#query AD for computer name based on csv log
foreach-object `
{ 
   $ComputerName = $_.name.Replace(".domain.com",$null)
   $Result = $null
   $Result = Get-ADComputer $ComputerName -ErrorAction Continue
   If ($Result -eq $null) {$RogueSystem = $_.Name}
   $RogueSystem | Out-File C:\DHCP\RogueClients.txt -Append
   $RogueSystem = $null

}

刚刚添加了一个命令,用于从指定的域控制器获取所有 ScopeID,并在 csv 文件中列出所有租约。

谢谢

【讨论】:

    【解决方案2】:

    使用 Try...Catch 会增加清晰度

    # query AD for computer name based on csv log 
    Import-csv -path $leaselogpath | foreach-object {
        Try {
            $ComputerName = $_.name.Replace(".domain.com",$null)
            $Result = Get-ADComputer $ComputerName -ErrorAction Stop
        }
        Catch {
             Write-Error $_
             $RogueSystem = $_.Name
             $RogueSystem | Out-File C:\DHCP\RogueClients.txt -Append
             $RogueSystem = $null 
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-19
      • 1970-01-01
      • 1970-01-01
      • 2016-02-13
      • 1970-01-01
      • 2017-03-10
      相关资源
      最近更新 更多