在 Get-ADComputer 中排序

Sort in Get-ADComputer


powershell不好,求社区帮助
任务是通过不同的文件过滤 Get-ADComputer 输出。按 属性 IPv4Address 排序。在文件本身中,您需要查看所有具有排序 IPv4Address + Measure-Object 输出的对象。
例如。\

SAD-MAZINA                                                                                          192.168.8.172                                                                                      
SAD-DISPETCHER                                                                                      192.168.8.173                                                                                      
SAD-POHORSKA                                                                                        192.168.8.179                                                                                      
SAD-PROPUSKA                                                                                        192.168.8.181                                                                                      
SAD-DERADMIN1                                                                                       192.168.8.182                                                                                      
SAD-LIZUNKINA                                                                                       192.168.8.184                                                                                      
SAD-CHEGLAKOV                                                                                       192.168.8.33                                                                                       
184-DSKTP                                                                                           192.168.8.34                                                                                       
LAPTOP-1                                                                                            192.168.8.35                                                                                       
SAD-MAKAROVA                                                                                        192.168.8.50
All                                                                                                 10

起初我尝试使用 foreachif 构造自己实现它,但是没有足够的知识来完成任务。任何帮助,谢谢!
例如。\

$regex = [Regex]"192.168.0.*"

$scan = Get-ADComputer -Filter * -ResultPageSize 0 -Prop CN,lastLogonTimestamp,IPv4Address |
Where {[datetime]::FromFileTime($_.lastLogonTimestamp) -ge (Get-Date).AddDays(-30)} | Sort-Object -Property IPv4Address | FT CN,IPv4Address
foreach ($onepc in $scan){

if ($onepc = $regex){
out-file -filepath C:\comps192.168.csv
}
}

根据我的评论,您的代码有很多问题。

您正在定义一个正则表达式,它实际上是一个通配符字符串。稍后您使用赋值运算符 =

将 IP 地址与此“正则表达式”进行比较

通过使用 | FT CN,IPv4Address,您的变量 $scan 将包含一个字符串数组,而不是创建有效 CSV 文件所需的对象数组

尝试

$ipRange = "192.168.0.*"
$refDate = (Get-Date).AddDays(-30).Date  # set time part to all 0, so this date is midnight
$scan = Get-ADComputer -Filter * -Properties CN,LastLogonDate,IPv4Address |
        Where {$_.LastLogonDate -ge $refDate -and $_.IPv4Address -like $ipRange} | 
        Sort-Object -Property @{Expression = {[int]($_.IPv4Address.Split(".")[-1])}}

# output to console
$scan | Format-Table -AutoSize

# output to CSV
$scan | Export-Csv -Path 'C:\comps192.168.csv' -NoTypeInformation

Sort-Object -Property @{Expression = {[int]($_.IPv4Address.Split(".")[-1])}} 使用计算的 属性 进行排序。在这种情况下,最后一个 IP 地址八位字节的整数值。这样,IP 192.186.0.2 将排在 192.168.0.11 之前,而如果您对 ip strings 进行排序,结果将相反。
此外,使用 Get-ADComputer,您可以请求 属性 LastLogonDate,这是 lastLogonTimeStamp 的值已经转换为(本地)DateTime 对象。


编辑

如果像您所说的那样,您的目标是测试 IP 地址是否在某个子网内,您可以使用下面的辅助函数

function Test-IsIPv4InSubnet ([string]$IpAndCidr, [string]$IpAddress) {
    $network, [int]$subnetlen = $IpAndCidr.Split('/')
    $a = [uint32[]]$network.split('.')
    [uint32] $unetwork = ($a[0] -shl 24) + ($a[1] -shl 16) + ($a[2] -shl 8) + $a[3]

    $subnetlen = [Math]::Min([Math]::Max([Math]::Abs($subnetlen), 0), 32)
    $mask = (-bnot [uint32]0) -shl (32 - $subnetlen)

    $a = [uint32[]]$IpAddress.split('.')
    [uint32] $uip = ($a[0] -shl 24) + ($a[1] -shl 16) + ($a[2] -shl 8) + $a[3]

    return ($unetwork -eq ($mask -band $uip))
}

将其放在脚本之上,代码可以如下所示:

$ipSubnet = "192.168.0.0/24"
$refDate = (Get-Date).AddDays(-30).Date  # set time part to all 0, so this date is midnight
$scan = Get-ADComputer -Filter * -Properties CN,LastLogonDate,IPv4Address |
        Where {$_.LastLogonDate -ge $refDate -and (Test-IsIPv4InSubnet $ipSubnet $_.IPv4Address)} | 
        Sort-Object -Property @{Expression = {[int]($_.IPv4Address.Split(".")[-1])}}

# output to console
$scan | Format-Table -AutoSize

# output to CSV
$scan | Export-Csv -Path 'C:\comps192.168.csv' -NoTypeInformation