Question : Powershell script to detect long path/file name and email file owner

Hello everyone,

I was hoping to get some help with a powershell script that will do the following:

1. ID long path/file name combo that are more than 200 character in length
2. ID the file owners of those file(s) if found and create a txt file to list them
3. Email that txt file to owner and suggest to rename to something different
4. Cc the IT team whenever an email like that is sent to a file owner

I have a following code, just not sure how to make it all work:
### get list of path/file longer than 250 char###
$CI = get-Childitem -recurse
 
foreach($_. in $CI)
{
    if($_.FullName.ToString().Length -gt 200)
    {
        $_.FullName + "-->" + $_.FullName.ToString().Length
    }
}

########### define file owner ###########
get-acl (list of files).owner
##################################

########## sending SMTP Email ###########

#Sending SMTP Email Code
   $smtpServer = “our mail server”
   $msg = new-object Net.Mail.MailMessage
   $att = new-object Net.Mail.Attachment($filename)
     
   $smtp = new-object Net.Mail.SmtpClient($smtpServer)
   $msg.From = “”
   $msg.To.Add(””)
   $msg.Cc.Add(””)


This is just different set of codes that I found via Google search.  again, just not sure how to put them all together.  Please feel free to let me know if you have any questions.  I would give over 500 points for this if i could ..

Answer : Powershell script to detect long path/file name and email file owner

This roughly works in my test environment. This uses PowerShell V2 send-mailmessage cmdlet, but you can change it to the "old" version of sending mail.
Problems may arise if the usernames include a dash character "-", tell me if this can arise.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
$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 $_"}
}
Random Solutions  
 
programming4us programming4us