$limit = 90
$testpath = "h:\"
$resultpath = "c:\ee"
$admins = "[email protected]"
$from = "[email protected]"
$smtpserver = "smtp.domain.com"
Get-ChildItem -Path $testpath -Recurse | ?{$_.fullname.length -gt $limit} |
Select-Object fullname,
@{n="owner";e={
$_.GetAccessControl().GetOwner('System.Security.Principal.NTAccount')}},
@{n="namelength"; e={$_.fullname.length}} |
%{
Out-File -FilePath "$resultpath\Longfiles of $($_.owner -replace "\\","-").txt" -Append -InputObject "$($_.namelength) - $($_.fullname)"
}
Get-ChildItem $resultpath -Filter "longfiles of *" | % {
if($_.name -match "Longfiles\sof\s(.+)\.txt"){
$user = $matches[1] -replace "-","\"
$ntacc = New-Object System.Security.Principal.NTAccount($user)
$sid = $ntacc.Translate([System.Security.Principal.SecurityIdentifier])
$aduser = [ADSI]"LDAP://<SID=$sid>"
$email = $aduser.Properties.mail
if($email) {Send-MailMessage -Attachments $_.fullname -Body "Please change the filenames of the files listed in the attached file to shorter!" `
-From $from -SmtpServer $smtpserver -Subject "System notice" -To $email -cc $admins
}
else {
Send-MailMessage -Attachments $_.fullname -Body "email coudn't be sent to owner" `
-From $from -SmtpServer iqjb-exchback -Subject "System notice" -To $admins
}
}
else {Write-Host "Some error with file $_"}
}
|