首页 文章

DHCP Powershell排序

提问于
浏览
0

好的,所以这更像是一个基本的Powershell问题,我很确定,但这就是我要做的事情:

我正在编写一个快速脚本,读取给定范围内的所有DHCP租约,查找客户端名称的任何匹配项(在本例中为名称中的“iphone”),然后从DHCP中删除这些租约 . 这是我到目前为止所拥有的:

$leases = Get-DhcpServerv4Lease -ScopeId 192.168.1.0 | select hostname, clientid
#Find all hostnames w/ 'android' or 'iphone' in name, delete lease
$trouble = $leases | select-string -Pattern "android","iphone","ipad"
Remove-DhcpServerv4Lease -ScopeId 192.168.1.0 -ClientId $trouble

问题是$ trouble的输出现在看起来像这样:

@{hostname=Someones-iPhone.domain.com; clientid=00-00-00-00-c7-cc}

由于我无法根据主机名删除租约(因为这不是全局唯一的,我假设),我需要传递MAC,即客户端ID .

如何在没有所有其他数据的情况下将输出缩小到只有clientid?我用Google搜索了一下,但这没有帮助 . 提前致谢!

1 回答

  • 0

    您不需要使用 Select-String 来过滤属性名称 - 使用 Where-Object

    $Troubles = Get-DhcpServerv4Lease -ScopeId 192.168.1.0 | Where-Object {
        $_.Hostname -match "android" -or
        $_.Hostname -match "iphone"  -or 
        $_.Hostname -match "ipad"
    } | Select-Object ClientId
    

    如果您只想要 ClientId 的值,请使用 Select-Object -ExpandProperty ClientId

相关问题