Last active
March 8, 2016 23:17
-
-
Save derekables/16f55298cec6a9f7e623 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 | |
)] | |
#Define the Path | |
[string]$Path = "\\SMBShare\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 -ge 1GB) {[string]::Format("{0:0.00} GB", $Size / 1GB)} | |
elseif ($Size -ge 1MB) {[string]::Format("{0:0.00} MB", $Size / 1MB)} | |
elseif ($Size -ge 1KB) {[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}} | |
} | |
End { | |
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" | |
} | |
} | |
} | |
#Call the function | |
Get-Diacritic -Path \\SMBShare\FileShare |
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
Invoke-Command servername { | |
Register-ScheduledTask -TaskName "Files With Diacritics" -Description "Finds files with diacritical letters" -Action (New-ScheduledTaskAction powershell.exe '-NoProfile -NonInteractive -Command "& C:\Get-Diacritic.ps1"') -Trigger (New-ScheduledTaskTrigger -Weekly -WeeksInterval 2 -DaysOfWeek Saturday -At) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment