Skip to content

Instantly share code, notes, and snippets.

View ghotz's full-sized avatar

Gianluca Hotz ghotz

View GitHub Profile
@ghotz
ghotz / Get-SQLInstance.ps1
Created May 27, 2014 12:19
Connect to a SQL Server instance
[Void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO');
[Void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMOExtended');
function Get-SQLInstance($InstanceName, $Login, $Password)
{
$SQLInstance = New-Object "Microsoft.SqlServer.Management.Smo.Server" $InstanceName;
if ($Login -eq $null) {
$SQLInstance.ConnectionContext.LoginSecure = $true;
}
else {
@ghotz
ghotz / Get-DatabaseLastLogBackupLSN.ps1
Last active August 29, 2015 14:01
Get transaction log backup LSN (log sequence number) for a database
[Void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO');
[Void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMOExtended');
$SQLInstance = New-Object "Microsoft.SqlServer.Management.Smo.Server" "localhost";
$DatabaseName = "TestDB";
$QueryStmt =
@"
SELECT last_log_backup_lsn
FROM sys.database_recovery_status
WHERE database_id = DB_ID('$DatabaseName');
@ghotz
ghotz / Split-ErrorLog.ps1
Created June 7, 2014 07:26
Split and strip SQL Server ERRORLOG file
# just a draft, need massive polishing :)
cls;
$reader = new-object System.IO.StreamReader("ERRORLOG")
$count = 1
$numlines = 0
$fileName = "{0}{1}.{2}" -f ("ERRORLOG_stripped_", $count, "txt")
while(($line = $reader.ReadLine()) -ne $null)
{
if ((++$numlines % 1000) -eq 0) { Write-Output "Processed $numlines lines" }
@ghotz
ghotz / xp_readerrorlog_demo.sql
Last active August 29, 2015 14:02
Example using undocumented procedure xp_readerrorlog
--
-- xp_readerrorlog @p1, @p2, @p3, @p4
-- based on http://www.mssqltips.com/sqlservertip/1476/reading-the-sql-server-log-files-using-tsql
--
-- @p1 0 = current, 1 = archive numeber 1 etc
-- @p2 Log type: 1/NULL=SQL Server, 2=SQL Server Agent
-- @p3 First search string
-- @p4 Second search string
-- @p5 Search start time
-- @p6 Search end time
@ghotz
ghotz / Get-InetHeaders.ps1
Created July 1, 2014 11:51
Get Internet Headers from Outlook MailItem
# Get Iternet headers from MailItem in PowerShell
Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null
$OutlookApp = new-object -comobject outlook.application
$MAPI = $OutlookApp.GetNamespace("MAPI")
$Message = $MAPI.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox).Items.GetFirst();
# http://msdn.microsoft.com/en-us/library/ms530451.aspx
$Message.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001E")
@ghotz
ghotz / jobs-categories.sql
Last active August 29, 2015 14:03
T-SQL Snippets
-- Retrieve job names and categories
SELECT C.name AS category_name, J.name AS job_name
FROM dbo.sysjobs AS J
JOIN dbo.syscategories AS C
ON J.category_id = C.category_id
ORDER BY
C.name;
@ghotz
ghotz / parse-dump-uniqstack.ps1
Created July 11, 2014 06:14
Parse unique stacks from WinDBG output generated with !uniqstack
# Parse WinDBG !uniqstack output
$file = 'SQLDump0008.uniqstack.txt';
$SearchLog = [regex] '\.\s{0,2}(?<thread>\d{1,4}).*?\r\n.*?\r\n.*?\r\n.*?\r\n.{8}`.{8} .{8}`.{8} (?<topstack>.*)\r\n';
$log = [io.file]::ReadAllText($file);
$match = $SearchLog.Match($log);
$logentries = @();
while ($match.Success) {
@ghotz
ghotz / task-session-stats.sql
Created July 28, 2014 14:46
Get a breakdown of waiting tasks and sessions binding
SELECT
task_state
, SUM(CASE WHEN session_id IS NULL THEN 0 ELSE 1 END) AS tasks_session_bound
, SUM(CASE WHEN session_id IS NULL THEN 1 ELSE 0 END) AS tasks_session_unbound
, COUNT(*) AS total_tasks
FROM sys.dm_os_tasks
GROUP BY
task_state
@ghotz
ghotz / Save-MHTML.ps1
Created July 29, 2014 10:02
Save web page in MHTML format with old technique using CDO and ADODB objects
#
# Save to MHTML file
# Adapted from http://stackoverflow.com/questions/995339/saving-webpage-as-mhtm-file
#
# COM SaveOptionsEnum
# Const adSaveCreateNotExist = 1
# Const adSaveCreateOverWrite = 2
#
# COM StreamTypeEnum
# Const adTypeBinary = 1
@ghotz
ghotz / xe_audit_db_ntauth_applications_2012.sql
Created September 22, 2014 17:45
Audit applications that connects to a specified database using Windows Integrated Authentication storing the applications names and the number of connections.
--
-- Audit applications that connects to a specified database using
-- Windows Integrated Authentication storing the applications names
-- and the number of connections.
--
-- Requires SQL Server versione >= 2012
-- Activate SQLCMD mode before running
--
:setvar DatabaseName TestDatabase