Skip to content

Instantly share code, notes, and snippets.

@Stuart-Moore
Last active March 10, 2016 10:36
Show Gist options
  • Save Stuart-Moore/bb9ff74933f682e0e665 to your computer and use it in GitHub Desktop.
Save Stuart-Moore/bb9ff74933f682e0e665 to your computer and use it in GitHub Desktop.
Get-Diacritic.ps1
<#
.Synopsis
Get files with accented Latin-1 characters in their name
.DESCRIPTION
Long description
.EXAMPLE
Get-Diacritic -Path c:\fileshare\ -To Boss@contoso.com
Gets all files whose name contains an accented Latin-1 character in the given folders and sends
the results as a CSV attachment to the email address supplied
.Example
Get-Diacritic -Path c:\fileshare\ -To Boss@contoso.com -AsHtml
Includes the results as a HTML formatted table in the body of the Email
.COMPONENT
The component this cmdlet belongs to
.ROLE
The role this cmdlet belongs to
.FUNCTIONALITY
The functionality that best describes this cmdlet
#>
function Get-Diacritic
{
Param
(
# Path Parameter
[Parameter(Mandatory=$true)]
$Path,
#Address to send email to
[Parameter(Mandatory=$True)]
$To,
# Switch to include the details as a HTML body to the email
[Switch] $AsHTML
)
#Check that supplied path exists
if (!(Test-Path $Path -ErrorAction Stop)){
Write-Error "Supplied path $path does not exist"
}
$Files = Get-ChildItem c:\temp\FileShare -recurse |
Where-Object{[int[]][char[]]$_.name -gt 192} |
select name, @{label="Directory"; Expression={$_.DirectoryName}}, `
@{label="Creation Date"; Expression={$_.CreationTime}}, `
@{label="Last Modification Date"; Expression={$_.LastWriteTime}},`
@{label="File Size"; Expression={switch ($_.Length){ `
{$_ -lt [math]::pow(2,20)} {"$([math]::Round($_/[math]::pow(2,10),2)) Kb"; break}
{$_ -lt [math]::pow(2,30)} {"$([math]::Round($_/[math]::pow(2,20),2)) Mb"; break}
{$_ -lt [math]::pow(2,40)} {"$([math]::Round($_/[math]::pow(2,30),2)) GB"; break}
}
}
}
if ($files.Count -gt 0){
$FileDateStamp = Get-Date -Format yyyyMMdd
$FilePath = "c:\temp\$($FileDateStamp)_FileNamesWithDiacritics.csv"
$files | Export-Csv -Path $FilePath
if ($AsHTML){
#Brief wasn't clear if the output should be sent in the body as an html table but shows it in the example email, so it's here as an option
Send-MailMessage -To $To -Subject "Diacritic files $(get-date)" -BodyAsHtml ($files | ConvertTo-HTML | Out-String) -SmtpServer smtphost.ntu.ac.uk -from server@contoso.com -Attachments $FilePath
}else{
Send-MailMessage -To $To -Subject "Diacritic files $(get-date)" -SmtpServer smtphost.ntu.ac.uk -from server@contoso.com -Attachments $FilePath
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment