Skip to content

Instantly share code, notes, and snippets.

View tcartwright's full-sized avatar

Tim Cartwright tcartwright

  • Houston, Texas
View GitHub Profile
@tcartwright
tcartwright / GetExtendedEventsPaths.sql
Created October 10, 2021 02:53
SQL SERVER: Get extended events destination paths
SELECT s.name,
LEFT(fn2.target_path, Len(fn2.target_path) - Charindex('\', Reverse(fn2.target_path))) AS target_dir,
fn2.target_path
FROM sys.dm_xe_session_targets AS t
JOIN sys.dm_xe_sessions AS s
ON s.address = t.event_session_address
CROSS APPLY (
SELECT CAST(t.target_data AS XML) AS [target_data_xml]
) fn
CROSS APPLY (
@tcartwright
tcartwright / RemoveAllTFSWorkSpaces.ps1
Created October 7, 2021 22:51
POWERSHELL: Removing all TFS workspaces for a user
# Original Source: https://www.timschaeps.be/post/using-the-tf-command-to-clean-up-old-tfvc-workspaces/
$moduleName = "EnterDeveloperPowerShell"
$module = Get-InstalledModule $moduleName -ErrorAction SilentlyContinue
if (-not $module) {
Write-Host "Installing module: $moduleName"
Install-Module -Name $moduleName -Force
}
Import-Module $moduleName
Enter-VisualStudioDeveloperShell
@tcartwright
tcartwright / GetOptionalPropertyValue.ps1
Created September 28, 2021 23:59
POWERSHELL: get optional property value
# only way to safely get an optional property off an object when strict mode is on
function GetOptionalPropertyValue($obj, [string]$propertyName, [object]$defaultValue, [switch]$isCollection) {
$prop = $obj.PsObject.Properties[$propertyName]
if ($prop) {
if ($isCollection.IsPresent) {
return ,$prop.Value
} else {
return $prop.Value
}
} elseif ($isCollection.IsPresent) {
@tcartwright
tcartwright / AssemblyResolve.ps1
Last active September 20, 2021 14:47
POWERSHELL: A C# AssemblyResolve event. Pure PS AssemblyResolve was causing stackoverflow exceptions
<#
Original credits: https://newbedev.com/powershell-assembly-binding-redirect-not-found-in-application-configuration-file
Purpose: To provide an assembly resolve event that does not crash PowerShell with a stackoverflow exception, and works.
Tim Cartwright: rewrote to be less complicated, and to resolve 100% of the time IF the assembly has already been loaded with a diff version.
If need be, before the resolver is called, either use Add-Type, or [System.Reflection.Assembly]::LoadWithPartialName("")
to load the assembly needed before hand if it is not already in your cache.
TO VERIFY IF THE NEEDED ASSEMBLY IS IN YOUR CACHE:
@tcartwright
tcartwright / GIT ALTER EMAIL
Last active September 6, 2021 15:14
GIT: ALTER EMAIL
To reset last commit:
git commit --amend --reset-author --no-edit
FOR MANY:
Found this answer here: https://stackoverflow.com/questions/750172/how-to-change-the-author-and-committer-name-and-e-mail-of-multiple-commits-in-gi/9491696#9491696
START:
@tcartwright
tcartwright / EnableTraceFlag460ForLogin.sql
Last active August 31, 2021 16:45
SQL SERVER: Server trigger that will allow TF 460 to be enabled per user for their connection only
/*
- BEWARE: Login triggers can lock down your system if not coded right (Knew that, just learned it the hard way)
- This login running the trigger should be a certificate account, and not have a password.
- Can also optionally disable the login. Does not seem to affect trigger execution
- Don't leave the trigger enabled.
- Don't test modifications on a server that other users are connecting to.
*/
USE master
@tcartwright
tcartwright / 1.Test-Json-Lib.ps1
Last active September 13, 2021 01:31
POWERSHELL: Examples on how to use newtonsoft nuget packages to Test-Json (PRE PS 6.1) We cant upgrade, so I wrote this workaround.
$scriptRoot = [System.IO.Directory]::GetParent($MyInvocation.MyCommand.Definition)
. ([System.IO.Path]::Combine($scriptRoot, "Install-Package.ps1"))
$jsonAssemblyFile = InstallPackage -packageName Newtonsoft.Json
$jsonSchemaAssemblyFile = InstallPackage -packageName Newtonsoft.Json.Schema
$validatorSource = @"
using Newtonsoft.Json.Linq;
@tcartwright
tcartwright / InstallPackage.ps1
Created August 26, 2021 23:59
POWERSHELL: Installs a nuget package if not installed, and adds a reference to the dll
$principal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
$isAdmin = $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator);
function InstallPackage($packageName, $dllName, [switch]$force) {
if (!$dllName) {
$dllName = "$($packageName).dll"
}
$package = Get-Package $packageName -ErrorAction SilentlyContinue
if (-not $package -or $force.IsPresent) {
@tcartwright
tcartwright / TimeSpan.sql
Last active August 26, 2021 20:58
SQL SERVER: Timespan between two dates including milliseconds
DECLARE @sd DATETIME2(7) = '2021-08-25 12:16:16.6969221',
@ed DATETIME2(7) = '2021-08-26 15:40:52.4579221'
DECLARE @diff DECIMAL(19, 7) = DATEDIFF(MILLISECOND, @sd, @ed)
SELECT CONCAT (
CAST(@diff / (1000 * 60 * 60 * 24) AS INT), --days
'.',