Skip to content

Instantly share code, notes, and snippets.

View paschott's full-sized avatar

Peter Schott paschott

View GitHub Profile
@paschott
paschott / usp_PurgeSSISCatalogLogs.sql
Created October 28, 2020 18:32
Creates stored procedure to purge SSIS catalog in batches of rows from child to parent.
use SSISDB
GO
CREATE PROC dbo.usp_PurgeSSISCatalogLogs
@RowsToDelete int = 5000
AS
BEGIN --Proc
/*
Script name: Purge SSIS Catalog log tables
Author: Tim Mitchell (www.TimMitchell.net)
@paschott
paschott / Generate-CSR.ps1
Created November 24, 2020 22:38
PowerShell script to generate a Certificate Request for a server using certain criteria
#Create new Certificate Request for SQL Server security
# Should be made into a function at some point
# Needs to be able to handle Cluster names/IP addresses
#Set location of the server
$Location = "City"
$State = "State"
$OU = "OU"
$Company = "Organization"
@paschott
paschott / New-SQLServerCertificate.ps1
Created December 10, 2020 01:08
Passes a CSR to the Certificate Authority, downloads the certificate, passes it to the target server, then imports and sets it for SQL Server
#requires dbatools
$server = "servername"
$localCertPath = "C:\CertificateRequests"
$remoteCertPath = "C:\CertificateRequest\"
$adminuser = Import-Clixml C:\user.cred #stored credentials to access remote server
# Generate the CSR and download locally
$session = New-PSSession $server -Credential $adminuser
--Get SQL Agent Job names for SSRS Subscriptions
SELECT Schedule.ScheduleID AS JobName,
[Catalog].Name AS ReportName,
Subscriptions.Description AS Recipients,
[Catalog].Path AS ReportPath,
StartDate,
Schedule.LastRunTime
FROM [ReportServer].dbo.ReportSchedule
INNER JOIN [ReportServer].dbo.Schedule
ON ReportSchedule.ScheduleID = Schedule.ScheduleID
SELECT
objects.name AS Table_name,
indexes.name AS Index_name,
dm_db_index_usage_stats.user_seeks,
dm_db_index_usage_stats.user_scans,
dm_db_index_usage_stats.user_updates
FROM
sys.dm_db_index_usage_stats
INNER JOIN sys.objects ON dm_db_index_usage_stats.OBJECT_ID = objects.OBJECT_ID
INNER JOIN sys.indexes ON indexes.index_id = dm_db_index_usage_stats.index_id AND dm_db_index_usage_stats.OBJECT_ID = indexes.OBJECT_ID
Import-Module dbatools
Import-Module sqlserver #used for Invoke-SqlCmd to run scripts w/ the "GO" batch separator
$CurrentDate = (Get-Date).ToString("yyyyMMdd_hhmmss")
$Servername = "localhost"
$databases = 'CSV_Set_Of_DB_Names'
#Get permissions for databases
@paschott
paschott / ExportUsers.ps1
Created December 14, 2021 20:05
Export SQL Users/Logins/Role Members into an Excel spreadsheet for reporting purposes
import-module dbatools
import-module ImportExcel
$servers="server1"
$date = get-date #format yyyymmdd
foreach ($server in $servers) {
$ExcelFile = "C:\path\$server_Users_And_Logins_$date.xlsx"
Get-DbaDbRoleMember -SqlInstance $server | Export-Excel -Path $ExcelFile -WorksheetName UserRoleMembers -AutoSize -AutoFilter -ExcludeProperty @("ItemArray", "RowError", "RowState", "Table", "HasErrors") -BoldTopRow
@paschott
paschott / Late-Arriving-Customer.sql
Created May 25, 2022 15:32
Example of handling a late-arriving customer as part of an SSIS Partial Cache Lookup Transform
DECLARE @CustomerID INT
SELECT @CustomerID = ?
IF NOT EXISTS (SELECT *
FROM WideWorldImportsDW.Dimension.Customer
WHERE [WWI Customer ID] = @CustomerID)
INSERT WideWorldImportersDW.Dimension.Customer
([WWI Customer ID],
Customer,
[Bill To Customer],
@paschott
paschott / Set up AG Job Steps.sql
Created May 31, 2022 13:51
Sets up a job to add a check for whether the AG is primary or not when determining whether jobs should run.
use master
go
-- Adds a first step to specified job, which checks whether running on Primary replica
create procedure dbo.AddAGPrimaryCheckStepToAgentJob
@jobname nvarchar(128)
as
set nocount on;
@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