DHCP IP地址查找

时间:2018-04-10 19:42:03

标签: powershell dhcp

所以我有另一个基本的PowerShell,我还不够先进。我有几个脚本用于在AD中查找信息(一个我从描述字段中提供信息,另一个我输入计算机编号)我想要的是从DHCP中提取IP地址租约。我需要从那里拉出MS Surface设备问题。我还有两台服务器需要轮询IP租约....

我已经开始了,但我的结果确实很混乱

 $scopes = Get-DhcpServerv4Scope -ComputerName 'XXXXXXX- need to have 2 here if possible XXXX' 
foreach($scope in $scopes)
{
    #write-host "The current scope name is:" $scope.name -ForegroundColor Red
    $lease = Get-DhcpServerv4Lease -ComputerName 'XXXXXXX- need to have 2 here if possible XXXX' -ScopeId $scope.ScopeId
    #$lease
}
    $pro = @{'name'=$scope.Name;'ipaddress'=$lease.IPAddress;'ScopeId'=$lease.ScopeId;'ClientId'=$lease.ClientId} | Format-Table
    $obj = New-Object -TypeName psobject -Property $pro
    $obj | Format-Table

我知道我并没有很好地解释这一点 - 但最终我想输入一个计算机编号并让它提取AD信息,然后进入DHCP并提取租约。

1 个答案:

答案 0 :(得分:0)

你可以这样做:

$DHCPServer = "Your Server Name"

$entry = Read-Host "Enter in computer name"
Import-Module ActiveDirectory
$computerObject = Get-ADComputer -Filter {Name -eq $entry} -Properties Name, DNSHostname

    $lease = Invoke-Command -ComputerName $DHCPServer -ScriptBlock {
    Import-Module ServerManager
    $info = Get-DHCPServerv4scope | Get-DHCPServerv4Lease | Where-Object {$_.Hostname -eq "$($using:computerObject.DNSHostName)"}
    return $info    
} -Credential
$lease

$ lease 将是您的所有范围信息,而 $ computerObject 应包含该计算机的所有AD信息。如果您想要更多AD属性,只需删除Name,DNSRecord并将其替换为* for all