Powershell在哪里对象的速度提高

我想从运行Windows Server 2012 R2的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) 

现在我的想法是build立一个自定义对象,其中包含我需要的所有属性。

 $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非常慢。
有没有机会提高对象的速度?

这里是我find和修改的代码。

 $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 

虽然我无法certificate它,但我倾向于认为Group-Object是一种更快的方法(因为它接收到两个列表,他可以使用更快的search方法,而不像'where'谁收到一个列表和一个项目find) 。