Deletion of Home Drive of an Active Directory User after 10 days of deletion of its Active Directory

Hi All,

Im in midst of an urgent scripting requirement. I am not at all good at powershell. So need your help, guys out there.

Actually what I need is - Deleting the home drive of an active directory user after 10 days of deletion of its active directory.
So, the logic that I thought of was - there can be two separate scripts. 

The first one will trigger as soon as the Active Directory of the user is deleted by the HELP DESK GUY, this will go and save the home drive path (which is already present in the properties of the active directory), the deletion date (current date + 10 days) and the user name  (for identity)  of the same user in corresponding 3 columns of an excel sheet (.csv file).

And, the Second script can contain the logic of reading the deletion date column entries and if the current date = deletion date, it will delete its HOME DRIVE by referring to its path saved in the home drive path column of the same user; and if current date is not matching any deletion date then it has to do nothing. This second script will be running two times everyday with the help of task scheduler.

I was even trying to write something down..
it would be greatly appreciable if you can help me in this.
Thanks in advance.


July 24th, 2015 4:10pm

Since this is critical and you are not a technician or someone who understands the scope of what you are asking, I recommend that you contact a consultant.

Note that we do not write scripts on demand.  It is your responsibility to write the script. We will answer specific questions about a script.pt

You can also fimd pre-written scripts here: https://gallery.technet.microsoft.com/ScriptCenter/

You can also post requests for custom scripts on that page.

Free Windows Admin Tool Kit Click here and download it now
July 24th, 2015 4:18pm

Hi jrv,

yes, i understand what  say.
I am even writing down this logic, and cmon its not that difficult.

if ur saying that this is beyond the scope of powershell, then my frnd u need to rethink, coz i ve seen many scripts saving and fetching data from .csv files. and what logic ive thot off is doable ofcourse.

Its just that i need some help in this, actualy if someone can give me an idea as to how to do this or an outline or some functions that will be useful, and i would know that im kicking in the right direction.

so.....
pls...

July 25th, 2015 12:53am

Yes - PowerShell can do thi but you will need to learn how to use PowerShell. Teaching you how to do this is beyond the scope of this forum.

If you don't have time to learn how to write a script then you will need  to contact a consultant.

Start here: https://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx?f=255&MSPPError=-2147217396

Free Windows Admin Tool Kit Click here and download it now
July 25th, 2015 12:59am

its not that i dont know powershell,, its just that i m very humble t say it,

I need some idea on this - deletion of active directory and deletion of home drive shud have a 1o days period difference.
if it would have been a simple script asking deletion of home drive as and when the AD is deleted, then i wouldnt even had written this post altogether...

can u help me with sm good idea, and maybe a two liner that can help me achieve this.

 
July 25th, 2015 1:28am

What have you written so far? Please post your scripts. If you at least show some effort, it will increase your chances of getting help.
Free Windows Admin Tool Kit Click here and download it now
July 25th, 2015 3:11am

its not that i dont know powershell,, its just that i m very humble t say it,

I need some idea on this - deletion of active directory and deletion of home drive shud have a 1o days period difference.
if it would have been a simple script asking deletion of home drive as and when the AD is deleted, then i wouldnt even had written this post altogether...

can u help me with sm good idea, and maybe a two liner that can help me achieve this.

 
  • Edited by MartinWood1 Saturday, July 25, 2015 5:23 AM
July 25th, 2015 5:22am

i actually changed some logic bcoz it was getting beyond my scope.

1st script - move the homedrive of the user to a different directory as soon as its AD is deleted.

2nd script  - delete any folders older than 10 days, thru a scheduled script  everyday.

what i wrote for 2nd script is - 

$limit = (Get-Date).AddDays(-10)
$path = "C:\Some\Path"

Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force

CAN SOMEONE HELP ME WITH THE FIRST ONE!
AND PLS CHECK IF 2nd one WILL DO IN THIS SCENARIO

Free Windows Admin Tool Kit Click here and download it now
July 25th, 2015 9:43am

I suggest learning to use the help.

Help Move-Item

I also recommend researching how to use RoboCopy for moving folders in batch.

Next you need too learn to test your scripts.  Loook up what "WhatIf" does,

Remove-Item -Force -Whatif

July 25th, 2015 11:10am

hi jrv,

u mean to say 2nd script is wrong in some sense?
can u pin out the problem?

Free Windows Admin Tool Kit Click here and download it now
July 25th, 2015 11:18am

I don't know if it is wrong.  YO need to test it to see if it does what you want.

Start by studying the basics of PowerShell. You cannot do this by guessing or asking vague questions.

July 25th, 2015 11:26am

I think I have found a way that only requires one script, scheduled to run daily. This script will search Deleted Objects in Active Directory for user accounts deleted at least 10 days ago. If a homeDirectory is found it will be deleted along with all subfolders and files. Run the script, and if it does what you want, remove -WhatIf.

$date = (Get-Date).AddDays(-10)
$filter = 'ObjectClass -ne "computer" -and ObjectClass -eq "user" -and whenChanged -lt $date'
$defaultnamingcontext = (Get-ADRootDSE).defaultNamingContext
$searchbase = "CN=Deleted Objects,$defaultnamingcontext"
$properties = 'sAMAccountName', 'whenChanged', 'homeDirectory'
Get-ADObject -Filter $filter -IncludeDeletedObject -SearchBase $searchbase -SearchScope OneLevel -Properties $properties |
    Select-Object $properties | ForEach-Object {
        $_
        if (Test-Path [string]$_.HomeDirectory) {
            Remove-Item -Path $_.HomeDirectory -Force -Recurse -WhatIf
        }
    }

Free Windows Admin Tool Kit Click here and download it now
July 25th, 2015 2:34pm

Great!

So does this mean, there is a record of deleted users in AD which is maintained?

So, will this directly read the home directory of the deleted user, from its AD properties --> Profile ---> Home Folder ---> Home directory?

and is thr no need of home drive?

July 26th, 2015 1:21am

I've done some more research and I'm sorry to say my script is useless as homeDirectory is one of many attributes not preserved in the tombstone object.
Free Windows Admin Tool Kit Click here and download it now
July 26th, 2015 3:35am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics