Skip to content

Instantly share code, notes, and snippets.

@XPlantefeve
Created March 21, 2016 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save XPlantefeve/5b9a668d87052724cc4d to your computer and use it in GitHub Desktop.
Save XPlantefeve/5b9a668d87052724cc4d to your computer and use it in GitHub Desktop.
# Answer to http://powershell.org/wp/2016/03/05/2016-march-scripting-games-puzzle/
Function Get-HumanReadableSize($size) {
If ( $size -lt 1KB) { Return $size }
ElseIf ( $size -lt 1MB) { Return "$([math]::Round(($size / 1KB),2))Kb" }
ElseIf ( $size -lt 1GB) { Return "$([math]::Round(($size / 1MB),2))Mb" }
ElseIf ( $size -lt 1TB) { Return "$([math]::Round(($size / 1MB),2))Gb" }
Else { Return "$([math]::Round(($size / 1TB),2)) TB" }
}
Function Test-Diacritics ([string]$str) {
# We iterate through the character and return true if we find one that doesn't match
# the [a-z] regexp while still being consider a letter in Unicode (UppercaseLetter or
# LowercaseLetter. )
foreach ( $char in $str.ToCharArray() ) {
if ( $char -notmatch '[a-z]' -and [Globalization.CharUnicodeInfo]::GetUnicodeCategory($char) -match 'caseLetter$' ) { Return $true }
}
Return $false
}
Function Get-Diacritics {
[CmdletBinding()]
Param (
[Parameter( Mandatory = $true )]
[string]$Path,
[Parameter( Mandatory = $true )]
[string]$SendTo,
[string]$SMTPServer = 'SMTP01'
)
If ( Test-Path -Path $Path ) {
# We make a list of files whose name contain diacritics
$DiacFiles = Get-ChildItem -Path $Path -Recurse | Where-Object -FilterScript { Test-Diacritics -str $_.Name }
# If we found any, we create the CSV, then send it.
If ( $DiacFiles ) {
$CSVPath = "$($env:TEMP)\$(Get-Date -Format 'yyyyMMdd')_FileNamesWithDiacritics.csv"
$DiacFiles | Select-Object -Property Name,Directory,CreationTime,LastWriteTime,@{
name = 'length'
expression = {Get-HumanReadableSize -size ($_.length)}
} | Export-Csv -Path $CSVPath -Encoding UTF8 -Force
$MailInfo = @{
'To' = $SendTo
'Attachments' = $CSVPath
'From' = $SendTo
'Subject' = 'Files with diacritical marks'
'Body' = "Scan of $(Get-Date -Format 'yyyy-MM-dd'): $($DiacFiles.count) filenames with diacritic marks found under ${Path}"
'SMTPServer' = $SMTPServer
}
Send-MailMessage @MailInfo
}
}
}
Get-Diacritics -Path C:\FileShare -SendTo carlo.mancini@contoso.com
# No bells & Whistles: the mail is basic, and the csv stays in the temp folder. We want the exercice to be easy to read.
# One liner.
# That's more or less the same test that in the Test-Diacritics function in the
# Get-Diacritics.ps1 file, but we use the integer values of UppercaseLetter and
# LowercaseLetter, as per the following table:
# https://msdn.microsoft.com/en-us/library/system.globalization.unicodecategory%28v=vs.110%29.aspx
invoke-command -computername lbdscsrv02 { ls C:\FileShare\ -Recurse | ? { ( $_.Name.ToCharArray() | ? { $_ -notmatch '[a-z]' -and [int][Globalization.CharUnicodeInfo]::GetUnicodeCategory($_) -le 2 } ).Count -ne 0 } } | ft Name,Directory,CreationTime,LastWriteTime,length
# One liner, but broke down to be easy to read.
# I work in an environment where Register-ScheduledTask is unavailable, so it's untested.
Invoke-Command -ComputerName 'lbdscsrv02' -ScriptBlock { Register-ScheduledTask `
-Action ( New-ScheduledTaskAction -Execute 'Powershell.exe' `
-Argument '-NoProfile -WindowStyle Hidden -file \\labdscsrv02\c$\Scripts\Get-Diacritics.ps1' ) `
-Trigger ( New-ScheduledTaskTrigger -Weekly -At '23:30' -DaysOfWeek Saturday -WeeksInterval 2 ) `
-TaskName 'Find files with diacritical marks'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment