【问题标题】:DHCP IP address lookupDHCP IP 地址查找
【发布时间】:2018-04-10 19:42:03
【问题描述】:

所以我还有另一个基本的 PowerShell 东西,我还不够先进,无法弄清楚。我有几个脚本用于在 AD 中查找信息(一个是我从描述字段中提供信息,另一个是我输入计算机编号)我想要从 DHCP 中提取 IP 地址租约。我需要从那里拉它以解决 MS Surface 设备问题。我还有 2 台服务器需要轮询 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 并提取租约。

【问题讨论】:

    标签: powershell dhcp


    【解决方案1】:

    你可以这样做:

    $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 并用 * 替换它

    【讨论】:

    • 使用原始版本时,我遇到了 DHCP 错误,因为它试图将我的计算机用作服务器,所以我做了一些更改,现在我没有得到任何结果
    • '$entry = Read-Host "输入计算机名称" $computerObject = Get-ADComputer -Filter {Name -eq $entry} -Properties Name, DNSHostname $lease = Get-DHCPServerv4scope -ComputerName ' SERVERINFOHERE' |获取-DHCPServerv4Lease -ComputerName 'SERVERINFOHERE' | Where-Object {$_.Hostname -eq "$($computerObject.DNSHostName)"}'
    • DHCPServer4Scope 需要在 2012 及更新版本的服务器上运行。我已更新脚本以在本地机器上工作
    • 我已更新脚本以提示输入 DHCP 服务器的凭据。只有当您当前的帐户没有对该服务器的权限时,您才需要这样做。另外,请确保您以管理员身份运行 powershell
    猜你喜欢
    • 2020-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多