Foreach now captures variables! (Access to modified closure)

Foreach has changed in C# 5.0! Consider the following piece of code in C# < 5.0: public class Test { public static void Main() { var words = new[] { “foo”, “bar”, “baz”, “beer” }; var actions = new List<Action>(); foreach (string word in words) { actions.Add(() => Console.WriteLine(word)); } actions.ForEach(e => e()); } } […]

When using an enum in PowerShell, use the member’s name, not the member’s value

Consider the following enum in C#: enum State { Started, Stopped, Unknown } Note that I have not added an explicit value for the enum members. They will be generated by the compiler. As stated in the C# spec: … its associated value is set implicitly, as follows: If the enum member is the first […]

ServerConnection and Login failed for user. Reason: Attempting to use an NT account name with SQL Server Authentication

Today I had to work with the ServerConnection class. This class provides a method to specify the connection to the Server class. So usage would be like this: # http://sqlblog.com/blogs/allen_white/archive/2008/04/28/create-database-from-powershell.aspx [System.Reflection.Assembly]::LoadWithPartialName(‘Microsoft.SqlServer.SMO’) [System.Reflection.Assembly]::LoadWithPartialName(‘Microsoft.SqlServer.ConnectionInfo’) $sqlServer = “server” $username = “username” $password= “password” $serverConnection = new-object Microsoft.SqlServer.Management.Common.ServerConnection($sqlServer, $username, $password) $server = new-object Microsoft.SqlServer.Management.Smo.Server($serverConnection) Write-Host (“SQL Version: {0}” -f […]