PowerShell -Filters失败 - 不比较

时间:2016-07-18 20:12:43

标签: powershell-v4.0

我有这个脚本试图从AD中吸引不活跃的用户,但如果有人没有标题,他们就不会被接听。我在单个用户的空标题上测试了逻辑,返回为true,但是当它在foreach中运行时,它不起作用(输出显示只有具有Title的人才有效)。我想也许lastlogontimestamp比较没有工作所以我添加了lastlogondate,但似乎标题仍然是问题,我不明白为什么?

#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 

        }
        }

1 个答案:

答案 0 :(得分: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)))) {