Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PSGames-201603-Advanced
function Get-Diacritic {
<#
.SYNOPSIS
Find filenames with diacritic marks, export the list to CSV, and email the list to your boss.
.LINK
http://powershell.org/wp/2016/03/05/2016-march-scripting-games-puzzle/
#>
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[ValidateScript({Test-Path $_ -PathType Container})]
[string]$Path = ((Get-Location).Path),
[string]$EmailServer = 'FakeRelay.powershell.org',
[string]$EmailFrom = 'it@powershell.org',
[string]$EmailTo = 'BossMan@powershell.org'
)
BEGIN {
$Today = Get-Date -Format yyyyMMdd
$CSV = "$env:TEMP\$($Today)_FileNamesWithDiacritics.csv"
$EmailSubject = "Files with Diacritical Marks - " + $Today
$TakeThemToYourLeader = @()
function Format-FileSize ($Length) {
If ($Length -gt 1073741824) {('{0:n1}' -f ($_.Length/1gb)) + ' GB'}
ElseIf ($Length -gt 1048576) {('{0:n1}' -f ($_.Length/1mb)) + ' MB'}
ElseIf ($Length -gt 1024) {('{0:n1}' -f ($_.Length/1kb)) + ' KB'}
Else {('{0:n1}' -f ($_.Length)) + ' B'}
}
}
PROCESS {
Write-Verbose "Scanning files at path $Path"
# Latin-1 letter list: https://en.wikipedia.org/wiki/Latin-1_Supplement_(Unicode_block)
$TakeThemToYourLeader += Get-ChildItem -Path $Path -File -Recurse |
Where {$_.Name -match '[\u00C0-\u00FF]' -and $_.Name -notmatch '[\u00D7]|[\u00F7]'} |
Select Name,
Directory,
@{n='Creation Date'; e={$_.CreationTime}},
@{n='Last Modified'; e={$_.LastWriteTime}},
@{n='File Size'; e={Format-FileSize $_.Length}}
Write-Verbose 'Scan complete'
} # Process block
END {
If ($TakeThemToYourLeader.Count -gt 0) {
Write-Verbose "Exporting results to $CSV"
$TakeThemToYourLeader | Export-Csv -Path $CSV
}
$Head = "Scan of $($Today): $($TakeThemToYourLeader.Count) filenames with diacritic marks found under $Path"
$Post = 'Bi-weekly scheduled task Get-Diacritic.ps1 - brianbunke'
$Body = $TakeThemToYourLeader | ConvertTo-Html -Head $Head -PostContent $Post -As Table | Out-String
$Email = @{SmtpServer = $EmailServer
From = $EmailFrom
To = $EmailTo
Subject = $EmailSubject
Body = $Body
BodyAsHtml = $true
ErrorAction = 'Stop'
}
Try {
Send-MailMessage @Email
} Catch {
Write-Debug 'Who do I email about an email problem?'
}
} # End block
}
# Now call the defined function, since this is the file in the scheduled task
Get-Diacritic -Path \\fileserver\share
# http://powershell.org/wp/2016/03/05/2016-march-scripting-games-puzzle/
# 324 character "one-liner" :)
Invoke-Command fileserver {Register-ScheduledTask -TaskName Diacritics -Description 'Find files with diacritical marks' -Action (New-ScheduledTaskAction powershell.exe '-NoProfile -NonInteractive -Command "& C:\Get-Diacritics.ps1"') -Trigger (New-ScheduledTaskTrigger -Weekly -WeeksInterval 2 -DaysOfWeek Saturday -At 11pm)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment