Skip to content

Instantly share code, notes, and snippets.

View dhmacher's full-sized avatar

Daniel Hutmacher dhmacher

View GitHub Profile
@dhmacher
dhmacher / Extract-WooCommerceOrders.ps1
Created October 12, 2021 13:12
Generates a SIE accounting file from WooCommerce sales orders.
# Provided as-is, without any warranty, implied or express.
# General:
$file = "Precon-" + (Get-Date).toString("yyyyMMdd-HHmmss") + ".se"
$baseUri = "https://example.com/wp-json/wc/v3/"
# WooCommerce authentication:
$key = "cs_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
$secret = "cs_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $key, $secret)))
-------------------------------------------------------------------------------
---
--- Reverse $PARTITION function - returns the boundary values for a given
--- partition function and partition number.
---
--- Returns:
---
--- Lower_Boundary sql_variant NULL for first partition
--- Lower_Boundary_Condition varchar(2) "<=" or "<"
@dhmacher
dhmacher / ConvertTo-UTF8
Created June 30, 2021 19:33
Converts an ISO-8859-1 string to UTF-8. Handy on when you receive a response from a web API that does not specify the codepage.
# https://stackoverflow.com/a/53034595/5471286
function ConvertTo-UTF8 {
param(
[Parameter(ValueFromPipeline)] [string]$ISOString
)
return ([Text.Encoding]::UTF8.GetString( `
[Text.Encoding]::GetEncoding(28591).GetBytes($ISOString)))
}
@dhmacher
dhmacher / Widest tables by avg row bytes.sql
Created June 29, 2021 14:14
Show the widest tables (or indexed views) by average bytes/row
/*
Find the widest tables by bytes/row
*/
SELECT OBJECT_SCHEMA_NAME(ps.[object_id])+N'.'+OBJECT_NAME(ps.[object_id]) AS [Object],
ix.[name] AS [Index],
ps.partition_number AS [Partition],
p.data_compression_desc AS [Compression],
REPLACE(CONVERT(varchar(20), CAST(SUM(ps.row_count) AS money), 1), '.00', '') AS [Row count],
REPLACE(CONVERT(varchar(20), FLOOR(CAST(SUM(8192.*ps.in_row_used_page_count) /NULLIF(SUM(ps.row_count), 0) AS money)), 1), '.00', '') AS [In-row, bytes/row],
@dhmacher
dhmacher / Check-ImageUrls.ps1
Last active July 26, 2022 21:41
Validate all image URLs in a WordPress blog export
# Export your WordPress blog to an XML file. Then:
$xmlFile = "./export/sqlsundaycom.wordpress.2021-05-23.001.xml"
$matches = Select-String -Path $xmlFile -Pattern '(?<=src\=\")http(.*?)(?=\")' -AllMatches `
| % { $_.Matches }
| % { $_.Value }
| Sort-Object -Unique
foreach ($url in $matches) {
#$url
@dhmacher
dhmacher / Invoke-ParamQuery.ps1
Created April 10, 2021 13:10
Run a SQL Server query using .NET (without the "SqlServer" module)
Function Invoke-ParamQuery {
param(
[String]$Query,
$Parameters=@{},
[Data.SqlClient.SqlConnection]$Conn,
[int]$Timeout=3,
[switch]$CloseConn,
[switch]$DiscardResults
)
if ($conn.State -eq "Closed") {
@dhmacher
dhmacher / list-agent-job-ssis-steps.sql
Created February 9, 2021 19:57
List jobs, job steps and their SSIS packages and environments respectively.
SELECT j.[name] AS Job,
s.step_id AS Step,
s.step_name AS [Step name],
s.subsystem AS [Subsystem],
s.[command],
x2.p AS [SSIS path],
env.environment_name AS [Environment name]
FROM msdb.dbo.sysjobs AS j
INNER JOIN msdb.dbo.sysjobsteps AS s ON j.job_id=s.job_id
OUTER APPLY (
@dhmacher
dhmacher / update-statistics.sql
Last active September 22, 2023 09:57
Use UPDATE STATISTICS to fake table & index sizes
DECLARE @object_id int=OBJECT_ID('dbo.tablename'),
@rowcount bigint=10000000;
SELECT N'UPDATE STATISTICS '+
--- Name of the table
QUOTENAME(OBJECT_SCHEMA_NAME(@object_id))+N'.'+QUOTENAME(OBJECT_NAME(@object_id))+
@dhmacher
dhmacher / move-deprecated-objects.sql
Last active July 26, 2022 21:43
Move temp/backup objects to Deprecated schema
IF (SCHEMA_ID('Deprecated') IS NULL)
EXEC('CREATE SCHEMA [Deprecated];');
SELECT 'ALTER SCHEMA [Deprecated] TRANSFER '+QUOTENAME(OBJECT_SCHEMA_NAME([object_id]))+'.'+QUOTENAME(OBJECT_NAME([object_id]))+';'
FROM sys.objects
WHERE [schema_id] NOT IN (SCHEMA_ID('Deprecated'), SCHEMA_ID('sys'))
AND [type] NOT IN ('PK', 'F', 'D', 'UQ')
AND ([name] LIKE '%xx%' OR [name] LIKE '%[0-9][0-9][0-9][0-9][0-9][0-9]%'
OR [name] LIKE '%[ _]temp' OR [name] LIKE 'temp[ _]%'
OR [name] LIKE '%[ _]old' OR [name] LIKE 'old[ _]%'
-- TRANSLATE() and STRING_SPLIT() work with SQL Server 2016+, STRING_AGG() with 2017+.
-- language_id=1033 is English (simplified)
SELECT STRING_AGG(REPLACE(TRANSLATE(s.[value] COLLATE database_default, '():.,-/='';', '**********'), '*', ''), ' ')
FROM sys.messages AS msg
CROSS APPLY STRING_SPLIT(msg.[text], ' ') AS s
WHERE msg.language_id=1033
--AND s.[value] COLLATE database_default NOT IN ('the', 'is', 'to', 'not', 'a', 'for', 'in', 'be', 'of', 'or', 'cannot', 'and')
AND s.[value] COLLATE database_default NOT LIKE '%[%@=]%'
GROUP BY msg.message_id