我在 list.csv
中有一个主机/计算机列表,其中包含 主机名
列。其中一些已经有一段时间没有登录了,所以我想删除包含过期计算机记录的所有行。
我可以使用下面的代码获取域中 90 天内未登录的所有计算机,但是我想获取该计算机列表并将它们与 list.csv
,从 list.csv
中删除匹配的计算机。
我已经尝试了一段时间其他文章和 Import-CSV
等,但无法破解它。
import-module activedirectory
$domain = "contoso.local"
$DaysInactive = 90
$time = (Get-Date).Adddays(-($DaysInactive))
# Get all AD computers with lastLogonTimestamp less than our time
Get-ADComputer -Filter {LastLogonTimeStamp -lt $time} -Properties LastLogonTimeStamp |
# Output hostname and lastLogonTimestamp into CSV
select-object Name | export-csv "C:\users\bicard\Desktop\allComputerslistFromDomainLookup.csv" -notypeinformation
最佳答案
刚刚发帖,我发现这是@JPBlanc 的提示。
$DaysInactive = 90
$OutDate = (Get-Date).Adddays(-($DaysInactive))
# Get all AD computers with lastLogonTimestamp less than our time
$OutDated = Get-ADComputer -Filter {LastLogonTimeStamp -lt $OutDate} -Properties LastLogonTimeStamp |
select-object -ExpandProperty Name
Import-Csv List.csv |
Where { $OutDated -notcontains $($_.'host name'} |
Export-csv NewList.csv -notypeinformation
关于powershell - 从另一个 csv 中删除 1 个 csv 中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43561186/