如何在Powershell中跨多个作用域查询Windows DHCP服务器的主机名

如何在Powershell中跨多个作用域查询Windows DHCP服务器的主机名,powershell,dhcp,Powershell,Dhcp,我正在尝试编写一个单行程序,允许我指定主机名,然后在ScopeID列表中搜索该DHCP租约。Powershell版本: Major Minor Build Revision ----- ----- ----- -------- 5 1 14393 576 到目前为止,我使用的是: Get-DhcpServerv4Lease -ComputerName Server -ScopeId 172.17.16.0 | Where-Object HostName -ma

我正在尝试编写一个单行程序,允许我指定主机名,然后在ScopeID列表中搜索该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 
这是可行的,但我必须使用find/replace更改每行的主机名

我在想这样的事情

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下不工作。任何帮助都将不胜感激。

您可以使用
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
}

您可以使用
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
}

以下不是一行代码,但对于那些拥有比手动识别范围多得多的范围的人来说:

$allscope = Get-DhcpServerv4Scope -ComputerName "dhcpserver"

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

以下不是一行代码,但对于那些拥有比手动识别范围多得多的范围的人来说:

$allscope = Get-DhcpServerv4Scope -ComputerName "dhcpserver"

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

这很有效。令人惊叹的!非常感谢。是否有任何方法可以运行Get-DhcpServerv4Scope并将该范围输出列表作为变量进行管道化以替换该数组?
(Get-DhcpServerv4Scope-ComputerName Server)。ScopeID |%{…
绝对完美。谢谢@BenHThis工作得很好。太棒了!非常感谢。有没有办法运行Get-DhcpServerv4Scope并将该范围输出列表作为变量来替换该数组?
(Get-DhcpServerv4Scope-ComputerName Server)。ScopeID |%{…
绝对完美。谢谢@BenH