Skip to content

Instantly share code, notes, and snippets.

@jeremybeavon
jeremybeavon / XmlToMarkdown.cs
Last active March 16, 2017 22:36 — forked from lontivero/gist:593fc51f1208555112e0
Convert XML to markdown
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Linq;
namespace GithubWikiDoc
{
@jeremybeavon
jeremybeavon / all_table_sizes.sql
Created February 15, 2015 20:48
Find the sizes of all tables in a SQL database
SELECT
t.NAME AS TableName,
i.name as indexName,
sum(p.rows) as RowCounts,
sum(a.total_pages) as TotalPages,
sum(a.used_pages) as UsedPages,
sum(a.data_pages) as DataPages,
(sum(a.total_pages) * 8) / 1024 as TotalSpaceMB,
(sum(a.used_pages) * 8) / 1024 as UsedSpaceMB,
(sum(a.data_pages) * 8) / 1024 as DataSpaceMB
@jeremybeavon
jeremybeavon / search_all_data.sql
Created February 15, 2015 20:51
Search an entire database for some text
DECLARE @SearchStr nvarchar(100)
SET @SearchStr = 'Text to find'
-- Copyright © 2002 Narayana Vyas Kondreddi. All rights reserved.
-- Purpose: To search all columns of all tables for a given search string
-- Written by: Narayana Vyas Kondreddi
-- Site: http://vyaskn.tripod.com
-- Tested on: SQL Server 7.0 and SQL Server 2000
-- Date modified: 28th July 2002 22:50 GMT
@jeremybeavon
jeremybeavon / all_database_sizes.sql
Created February 16, 2015 02:02
Find sizes of all databases on a server
SELECT
database_name = DB_NAME(database_id)
, log_size_mb = CAST(SUM(CASE WHEN type_desc = 'LOG' THEN size END) * 8. / 1024 AS DECIMAL(8,2))
, row_size_mb = CAST(SUM(CASE WHEN type_desc = 'ROWS' THEN size END) * 8. / 1024 AS DECIMAL(8,2))
, total_size_mb = CAST(SUM(size) * 8. / 1024 AS DECIMAL(8,2))
FROM sys.master_files WITH(NOWAIT)
GROUP BY database_id
ORDER BY total_size_mb
function AddItemProperties($item, $properties, $output)
{
if($item -ne $null)
{
foreach($property in $properties)
{
$propertyHash =$property -as [hashtable]
if($propertyHash -ne $null)
{
$hashName=$propertyHash["name"] -as [string]
@jeremybeavon
jeremybeavon / delete_all_data.sql
Created March 10, 2015 22:41
Delete all data from database
-- disable referential integrity
EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
GO
EXEC sp_MSForEachTable 'DELETE FROM ?'
GO
-- enable referential integrity again
EXEC sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL'
GO
@jeremybeavon
jeremybeavon / PowershellLinqSupport.cs
Last active July 28, 2021 23:50
Powershell LINQ Support
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
public static class PowershellLinqSupport
{
/// <example>[PowershellLinqSupport]::Range(1, 4)</example>
public static PowershellLinqSupport<int> Range(int start, int count)
function Using-Object
{
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[AllowEmptyString()]
[AllowEmptyCollection()]
[AllowNull()]
[Object]
$InputObject,
@jeremybeavon
jeremybeavon / FullJoin.cs
Created April 9, 2015 19:03
LINQ Full Join
public static IEnumerable<TResult> FullJoin<TOuter, TInner, TKey, TResult>(
this IEnumerable<TOuter> outer,
IEnumerable<TInner> inner,
Func<TOuter, TKey> outerKeySelector,
Func<TInner, TKey> innerKeySelector,
Func<TOuter, TInner, TResult> resultSelector)
{
return from key in outer.Select(outerKeySelector).Union(inner.Select(innerKeySelector))
join outerItem in outer on key equals outerKeySelector(outerItem) into outerGroup
from outerItem in outerGroup.DefaultIfEmpty()
@jeremybeavon
jeremybeavon / LinqJoinComplexCondition.cs
Created April 9, 2015 19:22
LINQ Join Complex Condition
public static IEnumerable<TResult> Join<TOuter, TInner, TResult>(
this IEnumerable<TOuter> outer,
IEnumerable<TInner> inner,
Func<TOuter, TInner, bool> condition,
Func<TOuter, TInner, TResult> resultSelector)
{
return from outerItem in outer
from innerItem in inner
where condition(outerItem, innerItem)
select resultSelector(outerItem, innerItem);