Monday, April 29, 2013

Retrospectively timing long-running operations in PowerShell

Sometimes I run an operation that takes longer than I expect to execute, but once it is finished, the only way to see how long it did take is to run it again in Measure-Command or use some other timing mechanism. This PowerShell prompt preserves your existing prompt (for example PoshGit) and tacks on an execution time for each and every command you run.
C:\Demo [master]> Start-Sleep 3
00:00:03.0048920
C:\Demo [master]> Start-Sleep 5
00:00:04.9974939
C:\Demo [master]>
00:00:00.0004274
C:\Demo [master]>

Thursday, April 25, 2013

Old SysInternals source code

The source code for SysInternals tools is no longer published, although it was for some of the tools before Microsoft purchased them.

The source code can be downloaded for those tools at the Internet Archive.

Monday, April 22, 2013

Launch NUnit GUI with multiple assemblies

The NUnit GUI does not support loading multiple assemblies from the command line, so this PowerShell function creates an NUnit project file that can be specified as a command line argument. Just provide a file name for the new project file (must end in .nunit or NUnit will barf) and an array of assembly files to load.

As a bonus, this project file will load your assemblies in multiple process mode, which means that any associated assembly configuration files will be correctly loaded.

Monday, March 4, 2013

"I believe you're the owner of..." spam

I recently received the following email (with domain names changed to protect the innocent), which is notable due to the absence of the usual grammar mistakes that give away most scams. The most obvious giveaway is that the site owner already owns both domains - including the one that Faheem is so generously offering to sell them.

I am curious to see whether "Faheem" is able to upload an HTML file as he claims, which would indicate that the site has been compromised.

Hello,

I believe you're the owner of [adomain.org]. I've got a proposition concerning your website. Would you be interested in acquiring [adomain.com]?

I can upload an HTML file temporarily to verify ownership of the domain, in case you're concerned. Let me know what you think to discuss further.

PS: I'm only emailing you because I believe you can benefit from this. I do not intend to email you again unless you respond to this inquiry.

Regards,
Faheem.

Friday, February 15, 2013

Debugging NUnit tests

I generally use NCrunch or ReSharper to run my NUnit tests, but it seems each test runner has its own quirks. Sometimes, integration tests only fail on the build machine because they run in the command line runner. Debugging these is a little trickier.

The solution I found that works well is to launch the NUnit GUI and run the tests from there, attaching to the correct process by running the following command in the Package Manager Console (i.e. PowerShell in VS):

($dte.Debugger.LocalProcesses | ? { $_.Name.EndsWith("nunit-agent.exe") }).Attach()

Wednesday, January 2, 2013

Copying Windows Azure SQL Database to a local server

The following script will download a SQL Azure database and import it into a local running instance. It assumes the local database does not exist. The import step could also be used to create an off-site backup.

This script uses the SqlPackage tool, which can be downloaded using the Web Platform Installer to install Microsoft SQL Server Data-Tier Application Framework (DACFx).

You will need to replace the values surrounded in asterisks (*) for the script to work correctly.

param([string][parameter(mandatory)]$DatabaseAdministratorPassword)

$sqlPackage = 'C:\Program Files (x86)\Microsoft SQL Server\110\DAC\bin\sqlpackage.exe'

$bacPacFile = join-path $env:TEMP 'Import.bacpac'
try 
{
    & $sqlPackage /a:Export /ssn:*****AZURE_DATABASE_SERVER***** /sdn:*****DATABASE_NAME***** /su:*****AZURE DATABASE ADMIN USER NAME***** /sp:$DatabaseAdministratorPassword /tf:$bacPacFile
    if(!$?) {
        throw "Failed to export database."
    }
    & $sqlPackage /a:Import /tdn:*****DATABASE_NAME***** /tsn:localhost\sqlexpress /sf:$bacPacFile
    if(!$?) {
        throw "Failed to import database."
    }
}
finally {
    del $bacPacFile
}

Sunday, December 23, 2012

Getting more detail from Azure PowerShell Cmdlet 400 errors

Some of the Azure PowerShell Cmdlets return less than useful errors:
New-AzureDeployment : The remote server returned an unexpected response: (400) Bad Request.
At line:1 char:5
+     New-AzureDeployment -Slot $slot -Package "$root\NewSite.Azure\bin\Release\ap ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [New-AzureDeployment], ProtocolException
    + FullyQualifiedErrorId : Microsoft.WindowsAzure.Management.ServiceManagement.HostedServices.NewAzureDeploymentCommand
The way to get more detailed information is on this blog:
(New-Object System.IO.StreamReader($Error[0].Exception.InnerException.Response.GetResponseStream())).ReadToEnd()
I have simply formatted this into a useful PowerShell oneliner. Note that this command can only be run once. The problem in my case was that I had changed my storage account to camel case, but it must be all lower case.
<Error xmlns="http://schemas.microsoft.com/windowsazure" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <Code>BadRequest</Code>
  <Message>The name is not a valid storage account name. Storage account names must be between 3 and 24 characters in length and use numbers and lower-case letters only.</Message>
</Error>