November 21, 2009

Weekend

Hope everyone is having a good weekend!

November 20, 2009

Ruby and Rake to Deploy ASP.NET and SQL II

In a follow-up to my article from yesterday. It seems that starting last night the website that I am pointing too went down. I am posting a link to the ruby file here with all credit to derek who wrote it up. The details of what he did are on his site and I hope it comes back up soon.

November 19, 2009

Ruby and Rake to Deploy ASP.NET and SQL

I currently use NAnt and some powershell to deploy most of my web applications in our environment. I was curious if I could do this with Ruby. I came across this informative article here that discusses how to use Rake and Ruby to do deployments for ASP.NET and also how to do SQL compares. Very interesting!

November 18, 2009

Add Uses to Group in Bulk using C#

So I had a task to take a list of usernames in a text document and add them to AD and map them to group(s). I am sure I could have done this quickly in Powershell, but I wanted to do it in C#. You can get the code here. The application is console based and uses some of our past AD classes.

The format was simple:
user1
user2
user3
A snippet of the code is below:
//using log4net;
//using ActiveDs;
public void Run()
{
foreach (AdUser user in ReadUsers())
{
using (user)
{
Dictionary userGroups = GetUserGroupsDn(user.Entry);

foreach (string groupDn in _groupsToAddUserTo.Keys)
{
if (userGroups.ContainsKey(groupDn))
continue; // User already in this group

AdGroup group = _groupsToAddUserTo[groupDn];

try
{
group.NativeObject.Add(user.Entry.Path);

log.Info(string.Format("User: {0} added to Group: {1}",
user.Entry.Properties["sAMAccountName"][0], group.NativeObject.Name));
}
catch (Exception)
{
log.Error(string.Format("User: {0} can't be added to Group: {1}. Possibly this is it's primary group already.",
user.Entry.Properties["sAMAccountName"][0], group.NativeObject.Name));
}
}
}
}
}
...and main:
 private void Run(string[] args)
{

//logging removed for brevity in snippet

Settings settings = SettingsReader.ReadFromConfig();

Adder adder = new Adder(settings);
adder.UsersToProcess = ReadUsersToProcess(settings.UsersFilePath);

adder.Run();
}
Anyone want to show how this can be done in powershell in 25 or so lines?

November 17, 2009

Reflection to invoke objects

So where did the ComWrapper come from yesterday? What is the point of this article in regards to reflection? Well, in short I wanted to show some some of the classes that helped me with my new AzMan library. Here is the TypeUtilities class:

//using System.Reflection;
public class TypeUtilities
{
private static string ToString(Type[] types)
{
string str = "";

foreach (Type type in types)
{
if (str.Length != 0)
str += ", ";

str += type.Name;
}

return str;
}

public static object GetProperty(object obj, string prop)
{
PropertyInfo pi = obj.GetType().GetProperty(prop);
return pi.GetValue(obj, new object[0]);
}

public static object GetProperty(Type type, object obj, string prop)
{
PropertyInfo pi = type.GetProperty(prop);
return pi.GetValue(obj, new object[0]);
}

public static Type[] GetTypes(object[] parameters)
{
Type[] types = new Type[parameters.Length];

for (int it = 0; it < parameters.Length; it++)
{
Type type = parameters[it].GetType();

if (type == typeof(ComWrapper))
{
ComWrapper wrapper = (ComWrapper)parameters[it];
type = wrapper.Type;
parameters[it] = wrapper.Object;
}

types[it] = type;
}

return types;
}

public static object InvokeOverloaded(object obj, string method, params object[] parameters)
{
Type objType = obj.GetType();
Type[] types = GetTypes(parameters);
MethodInfo mi = obj.GetType().GetMethod(method, types);
Assert(mi, objType, method, types);
return mi.Invoke(obj, parameters);
}

public static object Invoke(object obj, string method, Type[] types, object[] args)
{
Type objType = obj.GetType();
MethodInfo mi = objType.GetMethod(method, types);
Assert(mi, objType, method, types);

try
{
return mi.Invoke(obj, args);
}
catch (Exception ex)
{
Logger.Error(ex);
throw;
}
}

public static void Assert(MethodInfo mi, Type objType, string method, Type[] types)
{
if(mi == null)
throw new Exception(string.Format("Can't find method: {0}.{1}({2})", objType, method, ToString(types)));
}
Using the above classes, I'll show how in my AzMan library I was able to do things like:
while (!(entity is AzStore)) {
entity = (IAzParent)TypeUtils.GetProperty(entity, "Parent");

November 16, 2009

C# COM Wrapper

One of the classes that I use often, will show in details this with other classes that are part of my AzMan main solution.
public class ComWrapper
{
public object Object { get; set; }
public Type Type { get; private set; }

public ComWrapper(object obj, Type type)
{
this.Object = obj;
this.Type = type;
}

public static ComWrapper Create(T obj)
{
return new ComWrapper(obj, typeof(T));
}
}

November 15, 2009

One Line Powershell to show installed software

Do this with one line in powershell!