A Gaffe a Day
Counting Outgoing TCP connections by Port

Here’s the PowerShell script. Great for identifying the types of connections being held.

[Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties().GetActiveTcpConnections() |
    where { $_.State -eq "Established" } |
    foreach { $_.RemoteEndPoint } | 
    group -Property Port | 
    sort -Descending -Property Count
SQL Server, add a random time to a DATETIME column

Seems simple, the but NEWID() is a great trick.

dateadd(millisecond, ABS(CHECKSUM(NEWID()))%86400000, dateToIncrement)

The third part is your column name that you want to increment.

Pulled and modified from here.

Looking forward to this from voxer:

Voxer is hosting Hard Hack, a Node.js hardware hacking weekend at our headquarters in downtown San Francisco on December 8-9, 2012.

We’re going to get together, collaborate and hack all the things. There will be sharing of ideas, good company, experimentation…and lots of JavaScript. Snacks and…

Seven Core Flaws:

  1. Failure to instrument
  2. Failure to exception log
  3. No change history on data
  4. Failure to consider deletes
  5. Poor deployment process
  6. High Coupling
  7. Not Free
Group By Week

When ranging over data, it’s nice to group out data by week of the year. But you also want to display the first day of that week.

Here it is for SQL Server.

The key parts are

GROUP BY DATEPART(YEAR,trans_date),DATEPART(wk,trans_date)

and

MIN(DATEADD(wk, DATEDIFF(wk,0,trans_date), 0)) AS 'Week date',
world-shaker:

This is amazing.

world-shaker:

This is amazing.

SQL Server finding slow queries

Lifted from here:

SELECT TOP 20 SUBSTRING(qt.text, (qs.statement_start_offset/2)+1, 
    ((CASE qs.statement_end_offset
      WHEN -1 THEN DATALENGTH(qt.text)
     ELSE qs.statement_end_offset
     END - qs.statement_start_offset)/2)+1), 
qs.execution_count, 
qs.total_logical_reads, qs.last_logical_reads,
qs.min_logical_reads, qs.max_logical_reads,
qs.total_elapsed_time, qs.last_elapsed_time,
qs.min_elapsed_time, qs.max_elapsed_time,
qs.last_execution_time,
qp.query_plan
FROM sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) qt
CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) qp
WHERE qt.encrypted=0
ORDER BY qs.total_logical_reads DESC

Note that this can be tweaked by sorting on different things such as total_logical_reads / execution_count.

Sorting out folder sizes with PowerShell

praeclarum:

I have recently finished porting iCircuit to Microsoft Windows Phone 7 (WP7) and wanted to see how I performed on the code reuse front.

I use MonoTouch to do iOS development, and that was the first version of the application. Some months ago, I also released a Mac version of the app using

PowerShell sorting numeric

To sort a list of strings as numeric, use the sort’s function block.

strings | sort { [int]$_ }