2017-04-22 87 views
1

我有一个list.csv列表中的主机/计算机列表host name列。其中一些还没有在一段时间内登录,所以我想删除包含过时计算机记录的所有行。从1 csv从另一个行中删除行

我可以说没有在90天内(ISH)使用下面的代码已经登录到域中的所有计算机,但我想利用计算机的列表并且把它们比作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 
+0

您可以阅读var中的计算机列表,然后使用运算符'contains'。 – JPBlanc

回答

4

只是发布我看到这是@ JPBlanc的提示。

$DaysInactive = 90 
$OutDate = (Get-Date).Adddays(-($DaysInactive)) 

# Get all AD computers with lastLogonTimestamp less than our time 
$OutDated = Get-ADComputer -Filter {LastLogonTimeStamp -lt $Outate} -Properties LastLogonTimeStamp | 
    select-object -ExpandProperty Name 

Import-Csv List.csv | 
    Where { $OutDated -notcontains $($_.'host name'} | 
    Export-csv NewList.csv -notypeinformation 
+0

谢谢。我曾试过。 $过期确实包含计算机名称列表,但它们仍包含在newlist.csv中。它看起来像部分。 – Confuseis

+1

@Confuseis在上面的代码中,用'Select-Object -ExpandProperty Name'替换'select-object Name',或者指定'$ OutDated .Name'。'Select-Object' cmdlet默认返回一个包含所列属性的对象。'Select-Object -ExpandProperty'是返回一个只包含所选属性值的数组所必需的。 –

+0

@BaconBits感谢提示,回答编辑 – LotPings