2016-07-18 3 views
1

私はこのスクリプトを使用して、非アクティブなユーザーをADから引き抜こうとしていますが、誰かがタイトルを持っていないと、それらは取得されません。論理的には単一のユーザーのロジックをテストしましたが、これはtrueに戻りましたが、foreachを実行すると機能しません(出力にはタイトルが有効な人しか表示されません)。 lastlogontimestampの比較が機能していない可能性がありますので、lastlogondateを追加しましたが、タイトルがまだ問題であると思われます。PowerShell -Filters Not-Notlike Comparing

#Inactivity Process 

Import-Module ActiveDirectory 

# Gets time stamps for all User in the domain that have NOT logged in since after specified date. Exludes Prism and PlanLink providers. 
$DaysInactive = 365 
$time = (Get-Date).Adddays(-($DaysInactive)) 
$Out_file = "C:\scripts\adcleanup\Inactivity-365days-or-longer_$((Get-Date).ToString('MM-dd-yyyy')).csv" 
$Out_file2 = "C:\scripts\adcleanup\FailInactivity-365days-or-longer_$((Get-Date).ToString('MM-dd-yyyy')).csv" 

# Get all AD User with lastLogonTimestamp less than our time and set to enabled. Check All Users for this... 
$Users = Get-ADUser -Filter {samaccountname -like "s0*" -and (enabled -eq $true) -and (Title -notlike "*PRISM*") -and (Title -notlike "*PlanLink*") } -Properties samaccountname,name,enabled,Title,LastLogontimestamp, lastlogondate | 
select SamAccountname,Name,Title,@{Name="LastLogonTimeStamp"; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp)}},lastlogondate, enabled 


Foreach ($User in $Users) { 

    If (($user.LastLogontimestamp -le $time) -or ($user.LastLogondate -le $time)) { 

     $User| export-csv $Out_file -notypeinformation -append 

     } 

    Else { 

     $User | export-csv $Out_file2 -notypeinformation -append 

     } 
     } 

答えて

0

私はそれを理解しました。

If (($user.LastLogontimestamp -le $time) -or ($user.LastLogondate -le $time) -or ($user.title -eq "") -and ($user.title -notlike "*PRISM*") -and ($User.title -notlike "*Planlink*") -and (!([string]::IsNullOrEmpty($user.lastlogondate)))) { 
関連する問題