Skip to content

Instantly share code, notes, and snippets.

View Stuart-Moore's full-sized avatar

Stuart Moore Stuart-Moore

View GitHub Profile
#One liner answer to 'Beginner Task'
#relies on the fact that all Latin-1 accented characters have an ASCII value > 192 (https://msdn.microsoft.com/en-us/library/ms537495(v=vs.85).aspx)
#Not clear from brief on how this is meant to be run, but works locally:
Get-ChildItem c:\temp\FileShare -recurse | Where-Object{[int[]][char[]]$_.name -gt 192} | Format-Table name, @{label="Directory"; Expression={$_.DirectoryName}}, @{label="Creation Date"; Expression={$_.CreationTime}},@{label="Last Modification Date"; Expression={$_.LastWriteTime}}, @{label="File Size"; Expression={$_.length}}
#Over UNC
Get-ChildItem \\labbox1\share$\FileShare -recurse | Where-Object{[int[]][char[]]$_.name -gt 192} | Format-Table name, @{label="Directory"; Expression={$_.DirectoryName}}, @{label="Creation Date"; Expression={$_.CreationTime}},@{label="Last Modification Date"; Expression={$_.LastWriteTime}}, @{label="File Size"; Expression={$_.length}}
#Or via RemotePS:
@Stuart-Moore
Stuart-Moore / Get-Diacritic.ps1
Last active March 10, 2016 10:36
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
@Stuart-Moore
Stuart-Moore / ScheduleGet-Diacritic.ps1
Created March 10, 2016 10:35
ScheduleGet-Diacritic.ps1
#Assumes the solution ps1 is in the c:\scripts folder
Register-ScheduledTask -Action (New-ScheduledTaskAction -execute 'PowerShell.exe' -Argument "c:\scripts\Get-Diacritic.ps1; get-diacritic -Path 'c:\temp\fileshare\' -To stuart.moore@ntu.ac.uk -AsHTML"
) -Trigger (New-ScheduledTaskTrigger -weekly -WeeksInterval 2 -DaysOfWeek Saturday -At 11pm) -TaskName 'Find files with diacritic marks'
@Stuart-Moore
Stuart-Moore / gist:75690fa0ef83c04c1806614f2f0af014
Last active August 5, 2016 10:30
Split file on blank lines in powershell, quick and dirty
$testtext = @"
test1
test 2
The quick brown fox jumps over the lazy fox
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum
test4
$output = @()
$plineblank = $false
$tline = ''
$reader = [System.IO.File]::OpenText("C:\full\path\to\testfile.txt ")
while($null -ne ($line = $reader.ReadLine())) {
if ($line -eq '' -and $pline -ne '' ){
$output += $tline
$tline = $null
$externals = Get-ChildItem "$ModulePath\functions\"
$Rules = (Get-ScriptAnalyzerRule).Where{$_.RuleName -notin ('PSAvoidUsingPlainTextForPassword') }
Describe -tags ('ScriptAnalysis')'Script Analyzer Tests' {
ForEach ($ScriptFile in $externals)
{
Context "Testing $ScriptFile for Standard Processing" {
foreach ($rule in $rules)
{
$i = $rules.IndexOf($rule)
It "$ScriptFile passes the PSScriptAnalyzer Rule number $i - $rule " {
@Stuart-Moore
Stuart-Moore / gist:1215b9bba857a49430aab9243bb1557b
Created January 31, 2017 07:35
Restore striped SQL Server backup from share using powershell
#Written off the top of my head, check before relying on it.
import-module "SQLPS" -DisableNameChecking
$sqlsvr = New-Object -TypeName Microsoft.SQLServer.Management.Smo.Server("Server1")
$restore = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Restore
$devicetype = [Microsoft.SqlServer.Management.Smo.DeviceType]::File
$files=(Get-ChildItem \\YourServer\Yourshare\FULL\ -Filter *.bak | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-1)})
function Invoke-DbaDBRestoreSeeding
{
<#
.SYNOPSIS
Syncs 2 databases ready for database mirroring or Availability Group setup
.DESCRIPTION
Scans the database backup history on the Source SQL Instance, and restores them onto the source Instance
If more transanction backups occur before the restores complete, these will then be applied afterwards.
The function may also be used to resume an already partially restored databas as long as it's based on the same full database backup chain
use master
go
if exists (select * from sys.databases where name='RestoreTimeDiffStripe')
begin
ALTER DATABASE RestoreTimeDiffStripe SET single_USER with rollback immediate
drop database RestoreTimeDiffStripe
end
go
use master
go
if exists (select * from sys.databases where name='RestoreCopyOnly')
begin
ALTER DATABASE RestoreCopyOnly SET single_USER with rollback immediate
drop database RestoreCopyOnly
end
go