Skip to content

Instantly share code, notes, and snippets.

@kvprasoon

kvprasoon/MarScriptingGame.ps1 Secret

Last active Mar 21, 2016
Embed
What would you like to do?
Scripting Games March 2016
#Helped MSDN reference https://msdn.microsoft.com/en-us/library/20bw873z.aspx
#region Beginner
Invoke-Command -ComputerName Fileserver -ScriptBlock { Get-ChildItem -Path 'C:\powershell.org\Scripting Games\FileShare' -Recurse -File |
Where-Object -FilterScript { @('©','¼','½','÷') -notcontains $_.Name -and [regex]::IsMatch($_.Name,"\p{IsLatin-1Supplement}") } |
Format-Table @{ L='Name';E='Name' },@{ L='Directory';E='directoryname' },@{ L='Creation Date';E='CreationTime' },@{ L='Last Modification Date';E='LastWriteTime' },@{ L='File Size';E='Length' } }
#endregion Beginner
#region Advanced
#Job Script
Invoke-Command -ComputerName fileserver -ScriptBlock { Register-ScheduledJob -Name 'Find files with diacritical marks' -FilePath "\\server\c$\Scripts\GeTDiacritics.ps1" -Trigger (New-JobTrigger -DaysOfWeek Saturday -WeeksInterval 2 -At '11:00 PM' -Weekly) }
#region GetDiacritics.ps1
Function Get-Diacritic
{
process
{
[string]$path='\\server\C$\FileShare'
$Date=Get-Date
$OutFile="$env:TEMP\{0}{1:D2}{2}_FileNamesWithDiacritics.csv " -f $Date.Year,$Date.Month,$Date.Day
$DFiles=Get-ChildItem -Path $path -Recurse -File -ErrorAction Stop |
Where-Object -FilterScript { $_.Name -notmatch '[©¼½÷]' -and ([regex]::IsMatch($_.Name,"\p{IsLatin-1Supplement}")) } |
Select-Object -Property @{ L='Name';E='Name' },@{ L='Directory';E='directoryname' },@{ L='Creation Date';E='CreationTime' },@{ L='Last Modification Date';E='LastWriteTime' },@{ L='File Size';E={"{0:0.00}KB" -f ($_.length/1KB)} }
$Content = "Scan of $($Date.ToShortDateString()). $($DFiles.Count) filenames with diacritics marks found under $path"
if($DFiles.Count -ne 0)
{
$DFiles | Export-Csv -Path $OutFile -NoTypeInformation
$ContentHTML = $DFiles | ConvertTo-Html -Head "<h4 style='color:blue;'> Scan of $($Date.ToShortDateString()). $($DFiles.Count) filenames with dicritics marks found under $path </h4>" -PostContent "<p style='color:orange;'>Generated by PowerShell </p>" -as Table
$Content = $Content.ToString()
}
Send-MailMessage -Body $Content -BodyAsHtml -To 'Toaddress@mail.com' -From 'fromaddresss@mail.com' -SmtpServer 'smtpserver.mail.com' -Subject 'Files with Diacritical Marks'
}
}
Get-Diacritic
#endregion GetDiacritics.ps1
#endregion Advanced
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment