2017-04-22 88 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