Find files older then month and Email the owner


hi guys,

trying build no success, hope can give me hint.

what i’m trying build script has 3 purposes:

1.       find files in share older month , group them owner.

2.       get email address of owner ad. 1 going little bit of problem since run script win xp , understand powershell module ad not supported. email naming convention in organization have nothing user name format, must ad. i’ll try achieve 1 using cmd dsquery.

3.       send mail list of files owner.

so far managed create this:

 

$cd = get-date

$item = get-childitem "\\server\share" -recurse | {( ! $_.psiscontainer ) -and (( new-timespan $cd  $_.lastwritetime ).days -lt -30)} | get-acl

 

the above code find files (not folders) older 30 days. pipelining the get-acl give owner of these files.

now, need find way create foreach, use owner gather files match criteria , send him mail. not want send separate mail each item in $item variable.

so if add:

group-object owner

i can list of owner count of how many files, need form loop find files older month belong same owner , send mail him.

maybe it’s simple 1 couldn’t find way.

thanks

i've tested in test domain, , seems work fine.  give try, , let me know:

 

$a = [adsisearcher]"objectcategory=user"  $cd = get-date $item = get-childitem  | {(! $_.psiscontainer ) -and (( new-timespan $cd  $_.lastwritetime ).days -lt -30)} | get-acl $groups = $item | group owner foreach ($group in $groups) {     $username =  (($group.values) -split '\\')[1]     $a.filter = "samaccountname=$username"     $email = ($a.findone()).properties.item("mail") | out-string     $userfiles = $group.group | select path | split-path -noqualifier | out-string     $filecount = $group.count     $mailparams = @{from='admin@contoso.com';to=$email;subject="$filecount old files";body=$userfiles;smtpserver='server1'}     send-mailmessage @mailparams     } # end foreach $group  

 


([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')


Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

WIMMount (HSM) causing cluster storage to go redirected (2012r2 DC)

Failed to delete the test record dcdiag-test-record in zone test.com

Azure MFA with Azure AD and RDS