Created
March 8, 2016 23:04
-
-
Save derekables/21a60c8d14c2f014ea28 to your computer and use it in GitHub Desktop.
Powershell.org Scripting Games - March - Advanced
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.Synopsis | |
Find files with names that contain diacritical marks, and send this list to a specified email address. | |
#> | |
function Get-Diacritic { | |
[CmdletBinding()] | |
Param | |
( | |
[Parameter(ValueFromPipeline=$true, | |
ValueFromPipelineByPropertyName=$true, | |
Mandatory=$true | |
)] | |
[ValidateScript({Test-Path $_ -PathType Container})] | |
#Define the Path | |
[string]$Path = "C:\Fileshare", | |
#Define the SMTP Server | |
[string]$MailServer = "relay.powershell.org", | |
#Define the 'From' email address | |
[string]$MailFrom = "admin@powershell.org", | |
#Define the 'To' email address | |
[string]$MailTo = "boss@powershell.org" | |
) | |
Begin { | |
#Store date and filename | |
$Date = get-date -Format yyyyMMdd | |
$FileName = "$Date" + "_FileNamesWithDiacritics.csv" | |
#Function to convert values to readable content | |
function Format-Size() { | |
Param([long]$Size) | |
if ($Size -gt 1073741824) {[string]::Format("{0:0.00} GB", $Size / 1GB)} | |
elseif ($Size -gt 1048576) {[string]::Format("{0:0.00} MB", $Size / 1MB)} | |
elseif ($Size -gt 1024) {[string]::Format("{0:0.00} KB", $Size / 1KB)} | |
else {[string]::Format("{0:0.00} Bytes", $Size)} | |
} | |
} | |
Process { | |
Write-Verbose "Scanning for files with diacritical marks" | |
#Get files with diacritical marks (letters only, no symbols or numbers) | |
$FilesWithDiacritics = Get-ChildItem $Path -Recurse | | |
Where {$_.Name -match "[\u00C0-\u00FF]"} | | |
Select Name, | |
Directory, | |
@{Label="Created";Expression={$_.CreationTime}}, | |
@{Label="Last Modified";Expression={$_.LastWriteTime}}, | |
@{Label="Size";Expression={Format-Size $_.Length}} | |
Try { | |
if (($FilesWithDiacritics).Count -gt 0) { | |
#convert the findings to HTML and Send a message to the specified recipients | |
$FilesWithDiacritics | Export-CSV -Path $env:TEMP\$FileName | |
$Body = $FilesWithDiacritics | ConvertTo-Html -Head "Files with Diacritics found during bi-weekly scan" -PostContent "Created by dables on $Date" -As Table | Out-String | |
Send-MailMessage -From $MailFrom -To $MailTo -SmtpServer $MailServer -Subject "Files with Diacritics for the last two weeks" -Body $Body -ErrorAction Stop | |
} | |
else { | |
Write-Verbose "No Files Found" | |
} | |
} | |
Catch { | |
Write-Debug "Check your email server and try again" | |
} | |
} | |
End { | |
#If the total number of files is greater than zero | |
#If no files are found, report and end | |
} | |
} | |
#Call the function | |
Get-Diacritic -Path C:\FileShare |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment