Google
 

Monday, January 6, 2014

Articles I read in 2013

A new year, a new list...

2012's list can be found here
2011's list can be found here
2010's list can be found here
2009's list can be found here
2008's list can be found here 
2007's list can be found here


Friday, May 24, 2013

Output to multiple destinations in PowerShell

Sometimes you need to output a result of a Cmdlet execution or a variable to screen and a file, for example if you want to log all operations in a script in addition to showing the output to the user.
Calling both Out-Host and Out-File for each operation is clearly not a good option.
The nice Tee-Object Cmdlet can perform this functionality, but till before version 3.0, it cannot append output to an existing file. So be it, I have to code it:


function Out-All([string]$FilePath, [switch]$Append)
{
    Begin
    {
        if($Append -eq $False)
        {
            New-Item -Path $FilePath -ItemType File -Force
        }
    }
    Process
    {
        $_ | Out-Host
        $_ | Out-File -FilePath $FilePath -Append
    }
}



The above function writes the pipline input to both a file using Out-File and screen (or whatever taking the output) using Out-Host. The advantage is that it has the Append  switch.
The Begin block which executes before processing the pipline data checks  the Append switch and creates a new file (or not) accordingly.
The Process block is responsible for the actual writing of data.

Sample use:

(1..100) | Out-All -FilePath "C:\log\data.log"

(1..100) | Out-All -FilePath "C:\log\data.log" -Append

dir | Out-All -FilePath "C:\log\data.log" -Append

Thursday, January 3, 2013

Articles I read in 2012


So it's a new year!! 2012 was a year full of excitement for software lovers. The cloud service providers are competing for the good of the developers and businesses. Mobile technologies continue to emerge and steadily taking more market share from PCs. Also Microsoft released a series of new products.
In this list of articles I read in 2012, you might notice that not all articles are purely technical as I'm trying to find a way in the startup world.

2011's list can be found here
2010's list can be found here
2009's list can be found here
2008's list can be found here 
2007's list can be found here

Friday, October 19, 2012

Adding "Copy as Path" menu item to Nautilus

I use both Windows and Ubuntu Linux. One of the features I miss in Nautilus file manager under Ubuntu is the "Copy as Path" menu item which is available in Windows explorer context menu by holding the Shift key while right-clicking on a file or folder.
I tweaked Nautilus to do have "Copy as Path":



This required two steps:

First step:
A simple Python script to copy the path to clipboard:

#!/usr/bin/python
import gtk
import sys

clipboard = gtk.clipboard_get()
text = sys.argv[1]
clipboard.set_text(text)
clipboard.store()


This script uses gtk Clipboard to copy the path of the file, which is passed as the second parameter, the first is the script itself.
Save the script to a .py file and then move to the:

Second step:
Use Nautilus-Actions to add the menu item that will invoke the script.
It can be installed from Ubuntu Software Center or using terminal:

sudo apt-get install nautilus-actions

Open Nautilus Actions and Add a new action:




In the Command tab, enter python for the Path, and "/path_to_script/script_name.py %f" in the Parameters text box (replace with your own values, where you saved the script).
Note that the %f parameter means the selected file or folder path.

By default, Nautilus Actions creates a sub menu, and adds the custom actions to it. I did not like this behavior, which can be changed from the preferences by un-checking: "Create a root 'Nautilus-Actions' menu".
Changing this setting requires quiting nautilus:

nautilus -q

This simple tweak saved me a lot of time !

Saturday, September 29, 2012

Tip: Sending email to local folder for ASP.NET local development

Many web applications and sites have features that require sending emails. In production, usually mailSettings element of system.net is configured to send emails using SMTP server.
But when developing on a local machine, you want to check the output email quickly and without relying on a SMTP server. Sometimes a local antivirus can make the process harder.
It's better to configure the mail to send to a local folder, this can be easily done by setting the delivery method to SpecifiedPickupDirectory and setting the local directory location:
<system.net>
<mailSettings>
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="c:\Temp"/>
</smtp>
</mailSettings>
</system.net>

Now, when your code sends an email, a .eml file will be saved to c:\Temp, and you can double click it to check its content.

Wednesday, March 28, 2012

Returning full path of records in tables with recursive relationships

A common requirement when dealing with tables with recursive relationship where a record points to another (parent) record in the same table, is to get the full path of the record name. like the case with full folder paths in a file system hierarchy.
For example: (data from http://www.ida.liu.se/~iislab/projects/secont/main/)


CategoryId Name ParentCategoryId
 1 Asset NULL
2 Countermeasure NULL
3 Cryptography 2
4 Encryption 3
5 SignatureAlgorithm 4
6 CryptographicHashFunction 5
7 DSA 6
8 MD5 6
9 EncryptionAlgorithm 4
10 BlockCipher 9
11 AES 10
12 DES 10

For the above data, we need to get this:

CategoryId Name
1 Asset
2 Countermeasure
3 Countermeasure > Cryptography
4 Countermeasure > Cryptography > Encryption
5 Countermeasure > Cryptography > Encryption > SignatureAlgorithm
9 Countermeasure > Cryptography > Encryption > EncryptionAlgorithm
10 Countermeasure > Cryptography > Encryption > EncryptionAlgorithm > BlockCipher
11 Countermeasure > Cryptography > Encryption > EncryptionAlgorithm > BlockCipher > AES
12 Countermeasure > Cryptography > Encryption > EncryptionAlgorithm > BlockCipher > DES
6 Countermeasure > Cryptography > Encryption > SignatureAlgorithm > CryptographicHashFunction
7 Countermeasure > Cryptography > Encryption > SignatureAlgorithm > CryptographicHashFunction > DSA
8 Countermeasure > Cryptography > Encryption > SignatureAlgorithm > CryptographicHashFunction > MD5

This can be achieved using Recursive Common Table Expressions:

WITH CategoryCTE
AS
(
SELECT C.CategoryId, CONVERT(NVARCHAR(500), C.Name) AS Name FROM dbo.Category C
WHERE ParentCategoryId IS NULL
UNION ALL
SELECT C.CategoryId, CONVERT(NVARCHAR(500), CTE.name + N' > ' + C.Name) AS Name FROM dbo.Category C
JOIN CategoryCTE CTE ON C.ParentCategoryId = CTE.CategoryId
)
SELECT * FROM CategoryCTE 

It works like this:
  • The CTE selects from the base table data, the level which has no parents
  • The result is union-ed with the recursive part, which joins the base table with the last value of the CTE up to the current level of recursion
  • The name field of a record is a concatenation between its parent name and its own name
  • Select the result of the CTE