October 3, 2009

Connect with the Powershell Team

Give them feedback and tell them any issues that you see. You can get to the community from here.

October 2, 2009

Active Directory and C# VI

Time to continue this series of posts on the domain side. You can download all the files from here. I will upload this file from this post this week. You can see the first post related to this class file here. This post once again refers to the sAMAccount, Groups and SID administration in AD.

public DirectoryEntry OpenEntryBySAMAccountName(string sAMAccountName)
{
//from config
string rootPath = SearchPath;
String filter = string.Format("(&(|(objectClass=user)(objectClass=group))(sAMAccountName={0}))", sAMAccountName);
string[] propsToLoad = new string[0];

using (DirectoryEntry entry = OpenExistingEntry(rootPath))
{
if (entry == null)
throw new Exception(string.Format("Failed to open root for search by SAMAccountName. {0}", rootPath));

using (DirectorySearcher searcher = new DirectorySearcher(entry, filter, propsToLoad))
{
SearchResult sr = searcher.FindOne();

if (sr == null)
return null;

return sr.GetDirectoryEntry();
}
}
}

public List GetAllGroupsSAMAccountNames()
{
List adGroups = GetAllGroups(new string[] { "sAMAccountName" });

try
{
List groups = new List();

foreach (AdGroup adGroup in adGroups)
groups.Add((string)adGroup.Entry.Properties["sAMAccountName"].Value);

return groups;
}
finally
{
Dispose(adGroups);
}
}

public List GetAllGroups(string[] propsToLoad)
{
//from config
string rootPath = GroupsSearchPath;
String filter = "(objectClass=group)";
List groups = new List();

using (DirectoryEntry entry = OpenExistingEntry(rootPath))
{
if (entry == null)
throw new Exception(string.Format("Root to search groups by SAMAccountName failed to open. {0}", rootPath));

using (DirectorySearcher searcher = new DirectorySearcher(entry, filter, propsToLoad))
{
using (SearchResultCollection src = searcher.FindAll())
{
foreach (SearchResult sr in src)
{
groups.Add(new AdGroup(sr.GetDirectoryEntry()));
}
}
}
}

return groups;
}

private DirectoryEntry OpenRootDSE()
{
return OpenEntry("rootDSE");
}

public static Int64 UnboxAdsiInt64(object ADsLargeInteger)
{
IADsLargeInteger val = (IADsLargeInteger)ADsLargeInteger;
Int64 res = ((Int64)val.HighPart <<>
Turns out I need one more post for this to discuss password expiration and policy. Then this huge class file will be uploaded for you to use!

October 1, 2009

Active Directory and C# V

Time to continue this series of posts on the domain side. You can download all the files from here. I will upload this file from this post this week. You can see the first post related to this class file here. This post refers to the sAMAccount, Groups and SID administration in AD.
 
public AdUser OpenUserBySAMAccountName(string userSAMAccountName, params string[] propsToLoad)
{
//usersearchpath from config
string rootPath = UsersSearchPath;
String filter = string.Format("(&(objectClass=user)(sAMAccountName={0}))", userSAMAccountName);

using (DirectoryEntry entry = OpenExistingEntry(rootPath))
{
if (entry == null)
throw new Exception(string.Format("Root to search user by SAMAccountName failed to open. {0}", rootPath));

using (DirectorySearcher searcher = new DirectorySearcher(entry, filter, propsToLoad))
{
SearchResult sr = searcher.FindOne();

if (sr == null)
return null;

return new AdUser(sr.GetDirectoryEntry());
}
}
}

public string GetSidPath(DirectoryEntry entry)
{
byte[] sidBytes = (byte[])entry.Properties["objectSid"][0];
SecurityIdentifier sid = new SecurityIdentifier(sidBytes, 0);
return string.Format("LDAP://", sid.ToString());
}

public List GetUserGroups(DirectoryEntry user)
{
List groups = new List();

//we are building an '|' clause
List sids = new List();
user.RefreshCache(new string[] { "tokenGroups" });
foreach (byte[] sid in user.Properties["tokenGroups"])
{
//append each member into the filter
sids.Add(string.Format("(objectSid={0})", BuildFilterOctetString(sid)));
}

if (sids.Count == 0)
return groups;

//end our initial filter
string filter = "(|" + string.Join("", sids.ToArray()) + ")";
//GroupsSearchPath from config
DirectoryEntry searchRoot = OpenEntry(GroupsSearchPath);
using (searchRoot)
{

//we now have our filter, we can just search for the groups
DirectorySearcher ds = new DirectorySearcher(
searchRoot,
filter
);

using (SearchResultCollection src = ds.FindAll())
{
foreach (SearchResult sr in src)
{
groups.Add((string)sr.Properties["samAccountName"][0]);
}
}
}

return groups;
}

private string BuildFilterOctetString(byte[] bytes)
{
StringBuilder sb = new StringBuilder();

for (int i = 0; i < bytes.Length; i++)
{
sb.AppendFormat("\\{0}", bytes[i].ToString("X2"));
}

return sb.ToString();
}


I'll upload this file tomorrow. This will make it a lot easier to follow.

September 30, 2009

MSDeploy is RTW

Here is the link to the post, and here is the link to the tool. This is a great tool, you should take a look.

September 29, 2009

Active Directory and C# IV

To continue this series of posts, I'd now like to talk about groups and my group helper class. It is actually quite simple and kind of looks just like the User helper. You can download all the files from here. I will upload this file from this post this week. You can see the first post related to this class file here. This post refers to the groups administration in AD.
  public AdGroup OpenGroup(string groupName)
{
if (groupName.IndexOf('=') != -1)
return OpenGroupByDN(groupName);
else
{
//GroupsSearchPath from config
if (string.IsNullOrEmpty(GroupsSearchPath))
throw new Exception("To search groups by SAMAccountName the PathToSearchForGroupsBySAMAcountName setting should be specified.");

return OpenGroupBySAMAccountName(groupName);
}
}

public AdGroup OpenGroupByDN(string groupDN)
{
DirectoryEntry entry = OpenExistingEntry(groupDN);

if (entry == null)
return null;

return new AdGroup(entry);
}

public AdGroup OpenGroupBySAMAccountName(string groupSAMAccountName)
{
//GroupsSearchPath from config
string rootPath = GroupsSearchPath;

String filter = string.Format("(&(objectClass=group)(sAMAccountName={0}))", groupSAMAccountName);
string[] propsToLoad = new string[0];

using (DirectoryEntry entry = OpenExistingEntry(rootPath))
{
if (entry == null)
throw new Exception(string.Format("Root to search groups by SAMAccountName failed to open. {0}", rootPath));

using (DirectorySearcher searcher = new DirectorySearcher(entry, filter, propsToLoad))
{
SearchResult sr = searcher.FindOne();

if (sr == null)
return null;

return new AdGroup(sr.GetDirectoryEntry());
}
}
}
I'll post one more time on this and also upload the file for you to download.

September 28, 2009

Busy Day

Sorry, will have to continue our AD posts tomorrow...a crazy day as one of our production servers failed and our disaster recovery plan did not kick in correctly!

September 27, 2009

Playing with NHibernate

In my new project, I am playing with NHibernate. So far, very interesting. I had known of NHibernate back from my Java days with Hibernate, but never really played with it a lot. I'll share with you guys things that I come across with it as I continue to work with it.