Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
March Scripting Games Advanced Diacritics script and Scheduled task.
<#
.Synopsis
Scans the file names for Diacritics letters beloning to the Latin-1 Supplement.
.DESCRIPTION
Scans the file names for Diacritics letters beloning to the Latin-1 Supplement.
Before use check that valid values for the -Path, $Mailserver,$From and $To are correct.
#>
function Get-Diacritic
{
[CmdletBinding()]
Param
(
# Location of files Driveletter\folder
[Parameter(Mandatory=$true,
ValueFrompipeline = $true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[ValidateScript({Test-Path $_ -PathType Container})]
[String]$Path,
# E-mail Parameters
[String]$MailServer = 'mailserver.company.com',
[string]$From = 'admin@company.com',
[String]$To = 'boss@company.com'
)
Begin
{
# Get todays date.
$Date = Get-Date -Format yyyy-MM-dd
# Generate CSV file name including todays date.
$CSV = $Date + "_FileNamesWithDiacritics.csv"
# Name including todays date for error log if needed.
$Errorlog = 'Diacritics' + $date + '.log'
# configure Font size and color for Email HTML content.
$head = @'
<style>
h1 {color: #0000FF;
font-size: 12pt}
h2 {color: #FF0000;
font-size: 12pt}
Body{font-size: 12pt}
</style>
'@
# function to format file size
Function format-size($length){
if ($length -gt 1GB){('{0:n1}' -f ($length /1GB)) + 'GB'}
elseif($length -gt 1MB){('{0:n1}' -f ($length /1MB)) + 'MB'}
elseif($length -gt 1KB){('{0:n1}' -f ($length /1KB)) + 'KB'}
else{('{0:n1}' -f ($length )) + 'B'}
}
}
Process
{
# scan for files with Diacritics in file name
$files = Get-ChildItem -path $path -Recurse -force |
Where {$_.Name -match '[\u00C0-\u00ff]' -and $_.Name -notmatch '[\u00d7] | [\u00f7]'} |
select Name,
Directory,
@{N ='Creation Date';E = {$_.Creationtime}},
@{N ='Last Modification Date'; E = {$_.LastWriteTime}},
@{N = 'File Size'; E = {Format-size $_.Length}} #| format-table -AutoSize
# if files found with Diacritics save results in CSV file and generate the content of email in HTML format
If($files.count -gt 0){
$total = $files.count
$files | Export-Csv -Path $env:temp\$CSV
$Message = $files | ConvertTo-Html -Head $head -body $_ -as Table -PostContent "<H2>Generated using Powershell</H2>" `
-Precontent "<H1>Scan Date $Date : Files with Diacritics found under C:\FileShare: $total files in total </H1>" |
Out-String
}
# if no files found generate email content in HTML format
else {$message = $Message = $files | ConvertTo-Html -Head $head `
-body "<H1>Scan Date $Date : Files with Diacritics found under C:\FileShare </H1>",
"Scan completed successfully no files found today.", "<H2>Generated using Powershell</H2>" |
Out-String
}
# Attempts to send Email or generates a log file in the temp folder.
Try{
$ErrorActionPreference = 'stop'
$params = @{SmtpServer = $MailServer
From = $From
To = $To
Subject = "Files with Diacritics Marks"
Body = $Message
BodyAsHtml = $true}
Send-MailMessage @params
}catch{
$_ | out-file -FilePath $env:temp\$Errorlog
}
}
End
{
}
}
# calls the Get-Diacritic function and provides path to the files
Get-Diacritic -Path C:\fileshare
# set valid values for target host machine, the username and password befor use.
Invoke-Command FileServer {Register-ScheduledTask -TaskName Diacritics `
-Description 'List all files using letters from Latin-1 Supplement in filenames' -user 'AccountName' -Password '**********' `
-Action (New-ScheduledTaskAction Powershell.exe '-NoProfile -NonInteractive -command "& C:\scripts\Get-Diacritics.ps1"')`
-Trigger (New-JobTrigger -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