Skip to content

Instantly share code, notes, and snippets.

View JohnLBevan's full-sized avatar
🏠
Working from home

John Bevan JohnLBevan

🏠
Working from home
View GitHub Profile
@JohnLBevan
JohnLBevan / CSharpGeneralHelperFunctions
Created February 3, 2014 16:16
ToStringIfNotNull; allows me to write `return o.ToStringIfNotNull();` instead of `return o==null?null:o.ToString();` ; i.e. a ToString function which can cope with null references.
public static class ObjectExtensions
{
public static string ToStringIfNotNull(this object o)
{
return o == null ? null : o.ToString();
}
}
SELECT dbschemas.[name] as 'Schema',
dbtables.[name] as 'Table',
dbindexes.[name] as 'Index',
indexstats.avg_fragmentation_in_percent,
indexstats.page_count
FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL, NULL) AS indexstats
INNER JOIN sys.tables dbtables on dbtables.[object_id] = indexstats.[object_id]
INNER JOIN sys.schemas dbschemas on dbtables.[schema_id] = dbschemas.[schema_id]
INNER JOIN sys.indexes AS dbindexes ON dbindexes.[object_id] = indexstats.[object_id]
AND indexstats.index_id = dbindexes.index_id
use EnergyLive
go
SELECT REQ.SESSION_ID,
RTRIM(convert(varchar(128),REQ.context_info)) AS BATCH_INFO,
SQL = SQL.text,
QUERYPLAN.query_plan AS EXEC_PLAN,
CPU_TIME,
TOTAL_ELAPSED_TIME,
READS,
#requires -version 2.0
<#
author: Johan Vosloo
date: 24/11/2011
purpose: Retrieve event id’s from multiple machines and add to a CSV file.
#>
Try
{
$servers=get-content c:\scripts\servers.txt
$date=(Get-Date).AddDays(-7)
@JohnLBevan
JohnLBevan / CreateServerList.ps1
Created September 8, 2014 13:13
Populate an array of similarly named servers with minimal effort.
#
# Populate a list of servers where the only thing to change between servers is the number:
#
$servers = 1..10 | %{"ccioaxpi{0:00}edc" -f $_} `
+ 1..2 | %{"ccioaxpf{0:00}edc" -f $_}
#This is the equivelant to:
#$servers = 'ccioaxpi01edc','ccioaxpi02edc','ccioaxpi03edc','ccioaxpi04edc','ccioaxpi05edc','ccioaxpi06edc','ccioaxpi07edc','ccioaxpi08edc','ccioaxpi09edc','ccioaxpi10edc','ccioaxpf01edc','ccioaxpf02edc'
@JohnLBevan
JohnLBevan / GrantSysAdmin.sql
Created September 22, 2014 10:33
Grant user sysadmin access to a SQL instance from the command line
--::example batch file:
--pushd \\ukexchange\sysgrp\scripts\sql
--sqlcmd -S ccioaxpd05edc\axeam_train -i \\ukexchange\sysgrp\scripts\sql\grantsysadmin.sql -E -o \\ukexchange\sysgrp\scripts\sql\GrantSysAdmin.rpt
--popd
--sql file:
-- - usage:
-- sqlcmd -S <server\instance> -i \\ukexchange\sysgrp\scripts\sql\grantsysadmin.sql -E -o \\ukexchange\sysgrp\scripts\sql\GrantSysAdmin.rpt
use master
go
@JohnLBevan
JohnLBevan / PowerShellColors.ps1
Created September 25, 2014 16:43
Powershell Colours (Colors)
[Enum]::GetValues([System.ConsoleColor]) | %{ Write-Host $_ -ForegroundColor $_ }
@JohnLBevan
JohnLBevan / new_gist_file_0
Created September 30, 2014 20:32
Extract all zip files in a folder to sub folders named with the filename. More command line info here: http://sevenzip.sourceforge.jp/chm/cmdline/commands/extract.htm
::ensure 7zip directory is added to path environment variable (%programfiles%\7-zip)
7z x *.zip -o*
@JohnLBevan
JohnLBevan / shrink db
Created October 1, 2014 16:34
shrink db
use [ax-support1-uatb]
go
DBCC SHRINKFILE (N'EnergyLive' , 0, TRUNCATEONLY)
--(get file name from db's properties)
@JohnLBevan
JohnLBevan / PSCustomObject.ps1
Created October 9, 2014 11:35
create a custom object the nice way
$adDummy = New-Object –TypeName PSObject –Prop @{
emailSearched = $null;
notFound = $true;
sAmAccountName = $null;
fullname = $null;
firstname = $null;
lastname = $null;
cn = $null;
countryCode = $null;
country = $null;