Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

whenchanged attribute in ad using powershell

$
0
0

Hello,

I have two users in a specific OU in AD , I need to use powershell script to write "first when those users come to this OU, then if those users have been more than 6 monthes in this OU remove them"

It seems an easy senario but I've searched more than a weeks and still no tips to solv it......

finally i came in to use :

"Whenchanged attribute" to compair with Get-Date . but problem is it works untill i have one user in OU, If I have more than one users powershell tell me i cant compair the output with Get-Date. This is my script : 

(((plz help me if there is another way to solv the problem)))

                          

Get-ADUser -Filter * -SearchBase 'OU=testOU,DC=fire,DC=cloud' -Properties whenChanged | ForEach {
     $userinfo = Get-ADUser -filter {Enabled -eq $True} -SearchBase 'OU=testOU,DC=fire,DC=cloud' -properties whenChanged | Sort-Object DisplayName | Select-Object DisplayName, whenChanged 
     $today = Get-Date -Format "yyyy-MM-dd"
     $userData = $userinfo.Whenchanged
     $convert = ([string]$userData.ToshortDatestring()).Split()


     $result = (New-TimeSpan -Start $convert -End $today).Days 


        If ($result -ge 180) {
            #Get-ADUser -Filter * -SearchBase "OU=testOU,DC=fire,DC=cloud" | Disable-ADAccount
            #Get-ADUser -filter * -searchbase "OU=testOU,DC=fire,DC=cloud" | where-object { $_.Enabled -eq $false } | remove-ADobject -Confirm:$false


            "Hello"
            }


}


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>