
Windows Server 2012R2 を実行している DHCP サーバーから DHCP 予約のリストを取得したいと考えています。リストには、IP、MAC、名前、説明、および予約のリースの状態 (クライアントがオンラインかどうかを確認するため) が含まれている必要があります。予約を取得するための CMDLet があることは知っています。
$IP_res = (Get-DhcpServerv4Reservation -ComputerName $env:COMPUTERNAME -ScopeId 10.10.0.0)
結果にはリース ステータスは含まれません。ただし、それを取得する別の CMDLet があります。
$IP_lease =(Get-DhcpServerv4Lease -ComputerName $env:COMPUTERNAME -ScopeId 10.10.0.0)
ここで私が考えたのは、必要な属性をすべて含むカスタム オブジェクトを構築することでした。
$save = New-Object System.Collections.Generic.List[System.Object]
foreach($line in $IP_res)
{
$new_IP_Obj = "" | Select IP, MAC, Name, Description, LeaseStatus
$var = $IP_lease | Where-Object {$_.ClientId -eq $line.ClientId }
$new_IP_Obj.IP = $line.IPAddress.IPAddressToString
$new_IP_Obj.MAC = $line.ClientId
$new_IP_Obj.Name = $line.Name
$new_IP_Obj.Description = $line.Description
$new_IP_Obj.LeaseStatus = $var.AddressState
$save.add(new_IP_obj)
}
残念ながら、巨大なデータを比較する必要がある場合、Where-Object は非常に遅くなります。where
-object の速度を改善する可能性はありますか?
答え1
これは私が見つかったそしてこれに合わせて修正しました。
$Merged = @()
$Scopes = Get-DhcpServerv4Scope -ComputerName dc2008 #-ScopeId '10.1.230.0'
Foreach ($Scope In $Scopes) {
$IP_res = (Get-DhcpServerv4Reservation -ComputerName dc2008 -ScopeId $Scope.ScopeId)
$IP_lease =(Get-DhcpServerv4Lease -ComputerName dc2008 -ScopeId $Scope.ScopeId)
$IP_lease + $IP_res | Group-Object -Property ClientId | ForEach {
If ($_.group[1].AddressState -ne $null) {
$Record = New-Object -TypeName psCustomObject -Property @{
IP=$_.group[0].IPAddress.IPAddressToString;
MAC=$_.group[0].ClientId;
Name=$_.group[1].Name;
Description=$_.group[0].Description;
LeaseStatus=$_.group[1].AddressState
};
$Merged += $Record
}
}
}
$Merged | ft -AutoSize
証明はできませんが、Group-Object の方が高速な方法であると考えます (両方のリストを受け取るため、リストと検索する項目を 1 つ受け取る 'where' とは異なり、より高速な検索方法を使用できます)。