PowerShell For The .NET Developer

Some time ago I needed to have the validationKey of the machineKey element of an ASP.NET application changed and found out that ASP.NET doesn’t provide a command-line tool (or any other) to do this.

Looking around I found several applications and code samples to do it, but to have a system administrator do this I needed to test and document the application and it was to much work for such task.

I’ve always been a supporter of the idea of PowerShell but I never used it my self. Just because I almost always have Visual Studio open and writing a simple console application is quicker and easier than learning PowerShell.

This time I decide that I would do a PowerShell script instead.

In C# I would have done something like this:

class Program
{
    private static string GenerateKey()
    {
        var buff = new byte[64];
        (new System.Security.Cryptography.RNGCryptoServiceProvider()).GetBytes(buff);
        var sb = new System.Text.StringBuilder();
        foreach (var b in buff)
        {
            sb.AppendFormat("{0:X2}", b);
        }
        return sb.ToString();
    }

    private static void Main(string[] args)
    {
        var path = args[0];
        var config = System.Web.Configuration.WebConfigurationManager.OpenMachineConfiguration(path);
        var systemWeb = config.GetSectionGroup("system.web") as System.Web.Configuration.SystemWebSectionGroup;
        var machineKey = systemWeb.MachineKey;
        machineKey.ValidationKey = GenerateKey();
        config.Save(System.Configuration.ConfigurationSaveMode.Modified);
    }
}


How would it be in PowerShell? As simple as this:



function GenerateKey
{
    [System.Byte[]]$buff = 0..63
    (new-object System.Security.Cryptography.RNGCryptoServiceProvider).GetBytes($buff)
    $sb = new-object System.Text.StringBuilder(128)
    for($i = 0; ($i -lt $buff.Length); $i++)
    {
        $sb = $sb.AppendFormat("{0:X2}", $buff[$i])
    }
    return $sb.ToString()
}

[System.Reflection.Assembly]::LoadWithPartialName("System.Web")
$config = [System.Web.Configuration.WebConfigurationManager]::OpenWebConfiguration("<path>")
$systemWeb = $config.GetSectionGroup("system.web");
$machineKey = $systemWeb.MachineKey
$machineKey.ValidationKey=GenerateKey
$config.save("Modified")



Wonder how I got from no knowledge of PowerShell to this? Simple. Something that every real .NET developer has and loves: .NET Reflector (with a PowerShell add-in, of course).

From Vista To 7 With WET And WHS

My son inherits my old machines.

His current one was a machine that started with Windows Vista x86 Ultimate RTM, then SP1 and finally SP2. Also, along the way, several versions of Microsoft Office, .NET, Visual Studio, SQL Server and much more.

I usually have dozens of applications installed. Retrieving license keys and installing them a gain is such an hassle that I choose to upgrade whenever I can.

Now that Windows 7 is out and there’s a beta of Office 2010, my son wanted to upgrade the machine.

Because the machine had already gone through all those updates and he didn’t like the way I had partitioned the disk, I recommended him to format the disk and do a clean install.

One of the hassles of a clean install is that you loose all your settings such as Internet Explorer settings and favorites and Microsoft Outlook accounts and PSTs (specially if you have IMAP accounts).

Since I had already successfully used Windows Easy Transfer to migrate my work laptop from Windows XP (x86) to Windows Vista x64 Enterprise, I told him not to worry.

Windows Easy Transfer guides you through the process of transferring files and settings from one Windows installation to another.

With Windows Easy Transfer you can transfer:

  • Files and folders.
    Everything within the Documents, Pictures, and Shared Documents folders. Using advanced options, you can select additional files and folders to transfer.
  • E‑mail settings, contacts, and messages.
    Messages, account settings, and address books from Microsoft Outlook Express, Outlook, Windows Mail, and other e‑mail programs.
  • Program settings.
    Settings that keep your programs configured as you had them on your old installation. You must first install the programs on your new computer, because Windows Easy Transfer does not transfer the programs themselves. Some programs might not work on this version of Windows, including security programs, antivirus programs, firewall programs (your new computer should already have a firewall running to help ensure safety during the transfer), and programs with software drivers.
  • User accounts and settings.
    Color schemes, desktop backgrounds, network connections, screen savers, fonts, Start menu options, taskbar options, folders, specific files, network printers and drives, and accessibility options.
  • Internet settings and favorites.
    Internet connection settings, favorites, and cookies.
  • Music.
    Electronic music files, playlists, and album art.
  • Pictures and video.
    Pictures—which includes any visual file type (for example, .jpg, .bmp, .gif) – and personal videos.

After saving everything to the .MIG file, all it took was installing Windows 7, Office 2010 and import the settings back.

To get the other files and folders that were on the disk before being formatted, since we have a Windows Home Server that backs up all the PCs in the house , all it was needed was mounting one of the old backups as a disk and copying the files back.

It’s so easy that he did it al by himself while and he just turned 14. So, if you need to do something like this, don’t stress. It’s that easy.

Windows Live Messenger Unable To Connect With Error Code 80040200

Today I came across this issue when trying to sign in to Windows Live Messenger.

Everyone I talked to was able to sign in, though, so I tried another account and was also able to sign in.

Binging around, I found several blog posts pointing to a post in the Messenger Support blog with the solution to this problem. In my case (Windows 7 x64) the solution would be to remove the %LOCALAPPDATA%\Microsoft\Windows Live Contacts (usually C:\Users\<Windows Logon name>\AppData\Local\Microsoft\Windows Live Contacts) folder.

Instead of removing the folder, I thought of renaming it to avoid removing my contacts for all account that were able to sign in. When I tried it, I got an error stating the the folder was already being used by another application.

Since I had Windows Live Mail open and assumed it uses the same contact store, I closed it and was able to sign in to messenger with no problems.

As a developer and architect, I find it disturbing that such errors are presented to the user. The message hinted that there was a problem signing in to the server and, as it turned out, it was a problem with only one account for only one Windows user in only one machine.

So, developers and architects out there (me included), always give the user an error message meaningful to what problem he/she is running into. Adding technical data to help support is nice but should be expressly accessed by the user (Windows Live Messenger got that part right).