Skip to content

Instantly share code, notes, and snippets.

View paschott's full-sized avatar

Peter Schott paschott

View GitHub Profile
@paschott
paschott / SetUserRights.ps1
Created March 10, 2026 18:00
Set service account rights that may have been missed by GPO
#Needs to be run as administrator
#Reqires the UserRights.psm1 file
set-location $PSScriptRoot
import-module .\userRights.psm1
# Grants rights to the SQL Service account
Grant-UserRight -Account "domain\account" -Right SeServiceLogonRight #Logon as a Service
Grant-UserRight -Account "domain\account" -Right SeChangeNotifyPrivilege #Bypass traverse checking
Grant-UserRight -Account "domain\account" -Right SeIncreaseQuotaPrivilege #Adjust memory quotas for a process
@paschott
paschott / Backup_Restore_Status.sql
Created March 7, 2026 12:53
TSQL Backup and Restore Status
SELECT
session_id as SPID, command, a.text AS Query, start_time, percent_complete *1.0 as percent_complete,
dateadd(second,estimated_completion_time/1000.0, getdate()) as estimated_completion_time
FROM sys.dm_exec_requests r
CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) a
WHERE r.command in ('BACKUP DATABASE','RESTORE DATABASE','BACKUP LOG','RESTORE LOG')
@paschott
paschott / ExportTablesWithBCP.ps1
Last active February 27, 2025 23:12
Use PowerShell, the sqlserver module, and BCP.exe to extract data into files
Import-Module sqlserver
$serverName = "localhost"
$ExportFolder = "C:\temp\$serverName"
$databaseName = "AdventureWorks"
$tablesToExport = 'Person.Address','Person.AddressType','Person.BusinessEntity','Person.BusinessEntityAddress','Person.BusinessEntityContact','Person.ContactType','Person.CountryRegion','Person.EmailAddress','Person.Password','Person.Person','Person.PersonPhone','Person.PhoneNumberType','Person.StateProvince'
New-item -ItemType Directory -Path $ExportFolder -Force
foreach ($table in $tablesToExport) {
@paschott
paschott / QueryToHtmlTable.sql
Created June 19, 2024 21:44
Send HTML Formatted Emails from query results in SQL Server
-- Description: Turns a query into a formatted HTML table. Useful for emails.
-- Any ORDER BY clause needs to be passed in the separate ORDER BY parameter.
-- Adapted and added code to format the code
-- =============================================
CREATE OR ALTER PROC ##QueryToHtmlTable
(
@query nvarchar(MAX), --A query to turn into HTML format. It should not include an ORDER BY clause.
@orderBy nvarchar(MAX) = NULL, --An optional ORDER BY clause. It should contain the words 'ORDER BY'.
@html nvarchar(MAX) = NULL OUTPUT --The HTML output of the procedure.
)
@paschott
paschott / calendar_table.sql
Last active March 25, 2024 14:29
Calendar Table and Population
/* Adapted from ChilledSql.com */
/* https://www.chilledsql.com/welcome/tip_category_dateandtime/tip_detail_dateandtime_createmastercalendartable */
/* Update the Start/End dates as appropriate for your needs */
/* This is done after all creates */
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SET NOCOUNT ON
SET ANSI_WARNINGS OFF
SET ARITHABORT OFF
@paschott
paschott / Generate_Spelling_Audio.py
Last active November 15, 2023 00:45
Text to Speech for creating a practice spelling bee audio file
# requires gtts
# requires pydub
# requires ffmpeg to be installed on the system
from gtts import gTTS
from pydub import AudioSegment
import os
def generate_word_audio(word, output_folder='audio'):
if not os.path.exists(output_folder):
@paschott
paschott / Restore-InstallerFiles.ps1
Created July 18, 2023 14:50
This script was designed as a fix Windows "Installer" by restoring the missing Package(*.msi)/Patches(*.msp) files with the following steps: 1. Identifying the missing files. 2. Crawl the missing files from specified folder or other healthy machine.
<#
Code Type: Function
Description: Restore the missing Package(*.msi)/Patches(*.msp) files from another remote source (machine or folder).
Author: Ahmad Gad
Contact Email: ahmad.gad@jemmpress.com, ahmad.adel@jemmail.com
WebSite: http://ahmad.jempress.com
Created On: 21/09/2016
Updated On: 11/03/2017
Title: Restore-InstallerFiles
Minimum PowerShell Version: 2.0
@paschott
paschott / AzureSQLPermissions.sql
Last active July 17, 2024 16:01
Get Azure SQL Database Permissions
/* Get all db principals and their roles for Azure SQL */
SELECT
@@ServerName as ServerName,
DB_NAME() as DatabaseName,
pr.name AS PrincipalName,
pr.type_desc AS PrincipalType,
r.name AS RoleName,
dp.state_desc AS RoleState,
NULL as PermissionName,
NULL as PermissionState,
@paschott
paschott / Find-UnusedDatabases.ps1
Last active November 15, 2022 19:09
Steps through all Azure SQLDB Servers and databases to find user-specific index usage
import-module az.sql
import-module az.resources
import-module importexcel
# Update-AzConfig -DisplayBreakingChangeWarning $false
# sign in to Azure account
Connect-AzAccount
#Folder containing sql script and place to create Excel file
@paschott
paschott / AzureElasticJobTest.ps1
Created November 8, 2022 20:10
Scratch work for SQL Elastic Jobs in Azure w/ a PowerShell script
import-module az.sql
<# TODO
Based on:
https://learn.microsoft.com/en-us/azure/azure-sql/database/elastic-jobs-powershell-create?view=azuresql
* Add "Get" commants to top of script as appropriate
* Be able to loop through set of servers
* Document steps and/or separate files
* Parameterize passwords for master & job user accounts