Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Last Logon Time Stamp comparison and calculation problem

$
0
0

I have a script that goes out and grabs the last logon time stamp and puts it into a readable format.  Then i want to take that and say if it is old than 60 days then do some stuff.  I also have in there that if the lastlogontimestamp is null then give me the whencreated date so i can see if the account was just made then skip over that and give the user some time to log in.  My issue is that the .adddays piece isn't working.  i can just take that out and the same number of people get displayed.

Also if i don't put in .ToShortDateString then the logic works but only dealing with the month and day, not the year.  So if you ran this and had some accounts that haven't logged in since December of last year it wouldn't look at them because it is only looking at 60 days older than this month and day.

I just dont know where i am missing the calculation.  I also tried to calculate a this date minus this older date equal a number and if that number is GE to 60 then blah but i couldn't get the date math to work.

Here is the script:

$ADusers = Get-ADUser -filter {-not (memberof -eq $specificDn) } -SearchBase "OU=Accounts,DC=at,DC=domain,DC=com" -Properties enabled, lastlogontimestamp, samaccountname, CN, whencreated, passwordneverexpires, emailaddress,distinguishedname, info | where { $_.PasswordNeverExpires -eq $false } | where {$_.enabled -eq $true}

foreach ($ADuser in $ADusers){

$Login=$ADuser.samaccountname

$AcctCN=$Aduser.CN

$fname=$aduser.givenname

$lname=$aduser.surname

$info=$ADuser.info

 

if ($ADUser.lastlogontimestamp -ne $null){

       $lastlogon = Get-Date -date ([DateTime]::FromFileTime([Int64]::$ADUser.lastlogontimestamp)) -Format MM/dd/yyyy

         }

    else {        

        $lastlogon = Get-Date -Date $ADUser.whencreated -Format MM/dd/yyyy

         }            

  if ($lastlogon -lt ((Get-Date).adddays(-60).ToShortDateString())) {

    $scriptdate= Get-Date -Format MM/dd/yyyy

    SET-ADUSER $ADuser –replace @{info=”Disabled by stale account cleanup script on $scriptdate”}

    Add-ADGroupMember -identity " Account Cleanup Disabled Users" -Members $ADuser

    Disable-ADAccount -Identity $Login 

    write-host $login, $fname, $lname, "," $lastlogon


Viewing all articles
Browse latest Browse all 6937

Trending Articles