如何自定义“DHCP 所有范围和租约”脚本?

问题描述 投票:0回答:2

大家好,

我想修改下面的脚本

$DHCPServers = Get-DhcpServerInDC
$R = @()
ForEach ($Server in $DHCPServers)
    { $S = $Server.DnsName
      $Scopes = Get-DhcpServerv4Scope –ComputerName $S
      ForEach ($Scope in $Scopes) 
        {
            $AllScopes = $Scope.ScopeID.IPAddressToString
            $Results = Get-DhcpServerv4Lease -ComputerName $S -ScopeId $AllScopes -AllLeases | Select-Object @{Name="DHCPServer"; Expression={$S}},ScopeId,IPAddress,AddressState,ClientId,ClientType,HostName,Description,DnsRegistration,DnsRR,LeaseExpiryTime,NapCapable,NapStatus,PolicyName,ProbationEnds,ServerIP,PSComputerName
            $R += $Results
        }
                    
    }
$R | Sort-Object -Property DHCPServer,ScopeId,IPAddress | Out-GridView
  1. 如何在 ScopeId 列之后向显示“Scope Id Name”的输出添加额外的列?例如 |DHCPServer|ScopeId|ScopeIdName|IPAddress|

  2. 按 IP 地址排序时,192.168.1.19 之前的输出中显示 192.168.1.100 等 IP。我怎样才能让 Sort-Object 命令识别出 .100 在 .19 之后。大多数以个位数或双位数结尾的地址都已正确排序。

  3. 这不是必需的,但如果可以缩短将适用的服务器 DnsName。从 DC1.orginization.org 到 DC1

我尝试向输出添加一个额外的表达式,以便我可以添加“ScopeIdName”列,但收到错误。我试过的表达方式是……

@{Name="ScopeName"; Expression={$Scopes.Name}}

谢谢你的时间。

powershell server scope dhcp
2个回答
0
投票

这几乎是在黑暗中拍摄,我无权访问您用于测试的 cmdlet,但可能能够帮助您完成 3 个要求。

$sortproperties = @(
    'DHCPServer'
    'ScopeId'
    # lexicographical sorting
    { '{0:D3}.{1:D3}.{2:D3}.{3:D3}' -f @($_.GetAddressBytes()) }
)

Get-DhcpServerInDC | ForEach-Object {
    foreach($scope in Get-DhcpServerv4Scope -ComputerName $_.DnsName) {
        $getDhcpServerv4LeaseSplat = @{
            ComputerName = $_.DnsName
            ScopeId      = $scope.ScopeID.IPAddressToString
            AllLeases    = $true
        }

        foreach($lease in Get-DhcpServerv4Lease @getDhcpServerv4LeaseSplat) {
            [pscustomobject]@{
                # it is possible that `$_` already has a `.Name` property here
                # and splitting on `DnsName` might not be needed
                DHCPServer      = $_.DnsName.Split('.')[0]
                ScopeId         = $lease.ScopeId
                # Assuming `$scope` object has a `.Name` property:
                ScopeName       = $scope.Name
                # not sure if this is already an `ipaddress` instance or not,
                # using `-as` just in case (might not be needed):
                IPAddress       = $lease.IPAddress -as [ipaddress]
                AddressState    = $lease.AddressState
                ClientId        = $lease.ClientId
                ClientType      = $lease.ClientType
                HostName        = $lease.HostName
                Description     = $lease.Description
                DnsRegistration = $lease.DnsRegistration
                DnsRR           = $lease.DnsRR
                LeaseExpiryTime = $lease.LeaseExpiryTime
                NapCapable      = $lease.NapCapable
                NapStatus       = $lease.NapStatus
                PolicyName      = $lease.PolicyName
                ProbationEnds   = $lease.ProbationEnds
                ServerIP        = $lease.ServerIP
                PSComputerName  = $lease.PSComputerName
            }
        }
    }
} | Sort-Object $sortproperties | Out-GridView

0
投票

谢谢圣地亚哥!在您的帮助下,我能够使脚本正常工作。

$sortproperties = @(
    'DHCPServer'
    'ScopeId'
    # lexicographical sorting
    { '{0:D3}.{1:D3}.{2:D3}.{3:D3}' -f @($_.GetAddressBytes()) }
)

Get-DhcpServerInDC | ForEach-Object {
    foreach($scope in Get-DhcpServerv4Scope -ComputerName $_.DnsName) {
        $getDhcpServerv4LeaseSplat = @{
            ComputerName = $_.DnsName
            ScopeId      = $scope.ScopeID.IPAddressToString
            AllLeases    = $true
        }

        foreach($lease in Get-DhcpServerv4Lease @getDhcpServerv4LeaseSplat) {
            [pscustomobject]@{
                # it is possible that `$_` already has a `.Name` property here
                # and splitting on `DnsName` might not be needed
                DHCPServer      = $_.DnsName.Split('.')[0]
                ScopeId         = $lease.ScopeId
                # Assuming `$scope` object has a `.Name` property:
                ScopeName       = $scope.Name
                # not sure if this is already an `ipaddress` instance or not,
                # using `-as` just in case (might not be needed):
                IPAddress       = $lease.IPAddress -as [ipaddress]
                AddressState    = $lease.AddressState
                ClientId        = $lease.ClientId
                ClientType      = $lease.ClientType
                HostName        = $lease.HostName
                Description     = $lease.Description
                DnsRegistration = $lease.DnsRegistration
                DnsRR           = $lease.DnsRR
                LeaseExpiryTime = $lease.LeaseExpiryTime
                NapCapable      = $lease.NapCapable
                NapStatus       = $lease.NapStatus
                PolicyName      = $lease.PolicyName
                ProbationEnds   = $lease.ProbationEnds
                ServerIP        = $lease.ServerIP
                PSComputerName  = $lease.PSComputerName
            }
        }
    }
} | Sort-Object -Property DHCPServer,ScopeId,{ [Version]$_.IPAddress.IPAddressToString } | Out-GridView -Title "All Scopes and Leases for All DHCP Servers"
© www.soinside.com 2019 - 2024. All rights reserved.