powershell - 从另一个 csv 中删除 1 个 csv 中的行

标签 powershell

我在 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/

相关文章:

powershell - 基于命令行的ProcessID如何匹配

powershell - 启动进程不适用于 VSIXInstaller.exe

powershell - 如果使用 -Wait 参数运行,如何更改 powershell 中进程的优先级?

python - Windows 上的 virtualenv/virtualenvwrapper-win 中的环境变量

powershell - 需要监视服务器资源利用率30分钟并在Powershell中发送邮件

powershell - 使用 import-csv 的 Foreach 循环

powershell - 删除IIS授权规则 "All users"

powershell - 如何为 powershell 脚本设置一些内置变量?

sharepoint - PowerShell-具有UniquePermissions的新SPWeb

powershell - 向文本文件添加引号 powershell