Skip to content

Instantly share code, notes, and snippets.

View jpomfret's full-sized avatar

Jess Pomfret jpomfret

View GitHub Profile
--restore history
SELECT rh.restore_date,bs.backup_start_date, rh.stop_at, rh.destination_database_name,
case rh.restore_type
when 'D' then 'Full'
when 'I' then 'Differential'
when 'L' then 'Log' end as Restore_Type, bs.server_name as SourceServer, bs.database_name as SourceDB, backup_size as backupsize_bytes, (backup_size * 0.000001) as backupsize_MB
FROM MSDB.DBO.restorehistory rh
inner join msdb.dbo.backupset bs
on rh.backup_set_id = bs.backup_set_id
ORDER BY rh.restore_date DESC
<#
.SYNOPSIS
Function to get a list of Quarterfinal scores from CrossFit Games API for a specific affiliate
.DESCRIPTION
Function to get a list of Quarterfinal scores from CrossFit Games API for a specific affiliate
.PARAMETER affiliateName
The name of the affiliate to filter by, e.g. 'CrossFit Southampton'
@jpomfret
jpomfret / PowerShellReport.ps1
Last active November 18, 2023 11:18
Create great looking email reports with PowerShell and PSHTML
## Using PSHTML to create great looking email reports
## Blog post with more information: https://jesspomfret.com/pshtml-email-reports
## Email details
$emailTo = 'me@jesspomfret.com'
$emailFrom = 'reports@jesspomfret.com'
$emailSubject = ('Authors: {0}' -f (get-date -f yyyy-MM-dd))
$smtpServer = 'smtp.server.address'
## Query details
@jpomfret
jpomfret / SQLPermissionsFromSpreadsheet.ps1
Created December 22, 2022 12:00
Takes permissions from a spreadsheet and assigns them to the SQL Servers. If the logins are SQL Logins it will prompt for the password to be entered.
$perms = import-excel -Path C:\Temp\spreadsheetName.xlsx -WorksheetName permissions
<#
The spreadsheet should have a worksheet named permissions with the following columns:
Username Server Database Permissions
JessUser mssql1 database1 db_datareader
UserName2 mssql2 database2 db_datareader, db_datawriter, execute
#>
@jpomfret
jpomfret / AzureVMSnappyShots.ps1
Created October 21, 2022 12:27
AzureVMSnappyShots
# some variab;es
$resourceGroupName = 'rgTest'
$location = 'uksouth'
$vmName = 'rg-sm'
$snapshotName = ('snappyshot-SomethingUseful')
## take a snapshot of the disk
$vm = Get-AzVM -ResourceGroupName $resourceGroupName -Name $vmName
$currentOsDisk = $vm.StorageProfile.OsDisk.Name
$snapshot = New-AzSnapshotConfig -SourceUri $vm.StorageProfile.OsDisk.ManagedDisk.Id -Location $location -CreateOption copy
@jpomfret
jpomfret / ParseGarminWeightCSV.ps1
Last active July 29, 2022 08:57
Reformat the weight.csv you get when you download garmin connect weight data
# Login to https://connect.garmin.com/ & Navigate to 'Health Stats > Weight > 1 Year'
# at the top there is an export, that'll get you the csv.
# but the csv is in a poor format, with the date on the row above the data - this will reformat it for you
$weight = import-csv 'C:\Users\JessPomfret\Downloads\Weight.csv'
$counter = 0
$final = $weight.foreach{
if ( ($counter % 2) -eq 0) {
@jpomfret
jpomfret / CopyAndExportScripts.ps1
Created May 24, 2022 09:59
Copying logins & jobs from one server to another, but would also like to script them out for source control
## I'd like to copy logins/jobs but also script them out for source control
# get logins from source
$logins = Get-DbaLogin -SqlInstance $dbatools1
# first export, then copy
$logins | Export-DbaLogin -FilePath ('Export\Logins_{0}.sql' -f (get-date -f 'yyyyMMdd_hhmmss'))
$logins | Copy-DbaLogin -Destination $dbatools2
@jpomfret
jpomfret / IndexUsageAcrossAGNodes.ps1
Created November 15, 2021 16:28
Script to combine index usages stats across multiple AG nodes
$PrimaryInstance = 'Sql1'
$SecondaryInstance = 'Sql2'
$SqlInstance = $PrimaryInstance, $SecondaryInstance
$Database = 'DbName'
$results = Get-DbaHelpIndex -SqlInstance $SqlInstance -Database $Database
$export = @()
$labname = 'FailOverLab1'
New-LabDefinition -Name $labname -DefaultVirtualizationEngine HyperV -VmPath C:\AutomatedLab-VMs\VMs
Add-LabDomainDefinition -Name pomfret.com -AdminUser Install -AdminPassword Somepass1
Set-LabInstallationCredential -Username Install -Password Somepass1
Add-LabVirtualNetworkDefinition -Name $labname -AddressSpace 192.168.50.0/24
$PSDefaultParameterValues = @{
@jpomfret
jpomfret / AutomatedLab_SQLServer.ps1
Created February 23, 2020 17:27
Create a SQL server lab with the AutomatedLab PowerShell module
# Define the Lab, specify the virtualization engine and the path for the VMs to live
New-LabDefinition -Name SQLLab -DefaultVirtualizationEngine HyperV -VmPath C:\AutomatedLab-VMs\VMs
# Configure the Lab Network
Add-LabVirtualNetworkDefinition -Name SQLLab
Add-LabVirtualNetworkDefinition -Name 'Default Switch' -HyperVProperties @{ SwitchType = 'External'; AdapterName = 'Wi-Fi' }
$netAdapter = @()
$netAdapter += New-LabNetworkAdapterDefinition -VirtualSwitch SQLLab
$netAdapter += New-LabNetworkAdapterDefinition -VirtualSwitch 'Default Switch' -UseDhcp