【问题标题】:How to query Windows DHCP server across multiple scopes for a hostname in Powershell如何在 Powershell 中跨多个范围查询 Windows DHCP 服务器以获取主机名
【发布时间】:2018-05-10 19:19:53
【问题描述】:

我正在尝试编写一个单行程序,允许我指定主机名,然后在范围 ID 列表中搜索该 DHCP 租约。 Powershell 版本:

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      14393  576

到目前为止,我正在使用这个:

Get-DhcpServerv4Lease -ComputerName Server -ScopeId 172.17.16.0 | Where-Object HostName -match Hostname | FT ipaddress,hostname,clientid,addressstate
Get-DhcpServerv4Lease -ComputerName Server -ScopeId 172.17.17.0 | Where-Object HostName -match Hostname | FT ipaddress,hostname,clientid,addressstate
Get-DhcpServerv4Lease -ComputerName Server -ScopeId 172.17.18.0 | Where-Object HostName -match Hostname | FT ipaddress,hostname,clientid,addressstate
Get-DhcpServerv4Lease -ComputerName Server -ScopeId 172.17.19.0 | Where-Object HostName -match Hostname | FT ipaddress,hostname,clientid,addressstate
Get-DhcpServerv4Lease -ComputerName Server -ScopeId 172.17.76.0 | Where-Object HostName -match Hostname | FT ipaddress,hostname,clientid,addressstate 

这可行,但我必须使用查找/替换更改每行的主机名。

我在想类似的东西,

Get-DhcpServerv4Lease -ComputerName Server -ScopeId 172.17.16.0,172.17.17.0,172.17.18.0,172.17.19.0,172.17.76.0 | Where-Object HostName -match Hostname | FT ipaddress,hostname,clientid,addressstate

但该数组在 ScopeId 下不起作用。任何帮助将不胜感激。

【问题讨论】:

    标签: powershell dhcp


    【解决方案1】:

    您可以使用foreach 或别名% 输入IP 地址:

    "172.17.16.0","172.17.17.0","172.17.18.0","172.17.19.0","172.17.76.0" | % {
        Get-DhcpServerv4Lease -ComputerName Server -ScopeId $_ |
        Where-Object HostName -match Hostname |
        FT ipaddress,hostname,clientid,addressstate
    }
    

    【讨论】:

    • 这非常有效。惊人的!非常感谢。有什么办法可以跑
    • 是否有任何方法可以运行 Get-DhcpServerv4Scope 并将该范围输出列表作为变量通过管道来替换该数组?
    • (Get-DhcpServerv4Scope -ComputerName Server).ScopeID | % { ...
    • 绝对完美。谢谢@BenH
    【解决方案2】:

    以下内容并不完全是单行的,但适用于具有比手动识别更多范围的人:

    $allscope = Get-DhcpServerv4Scope -ComputerName "dhcpserver"
    
    foreach ($ScopeID in $allscope) 
    {Get-DhcpServerv4Lease -ComputerName "dhcpserver" -ScopeId $ScopeID.scopeid | 
    where {$_.hostname -match "hostname"}}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-17
      • 1970-01-01
      • 1970-01-01
      • 2017-02-15
      • 1970-01-01
      • 2016-03-24
      • 1970-01-01
      相关资源
      最近更新 更多