Skip to content

Instantly share code, notes, and snippets.

@grenade
Last active December 31, 2015 03:59
Show Gist options
  • Save grenade/7930773 to your computer and use it in GitHub Desktop.
Save grenade/7930773 to your computer and use it in GitHub Desktop.
param(
[string] $username = $env:username
)
function Get-User {
param(
[System.String] $username
)
$ds = New-Object System.DirectoryServices.DirectorySearcher
$ds.Filter = "(&(objectCategory=person)(sAMAccountName=$username))"
$ds.SearchRoot = "LDAP://{0}" -f ([ADSI] "LDAP://RootDSE").Get("rootDomainNamingContext")
return [ADSI] $ds.FindOne().Path
}
function Get-UserGroups {
param(
[System.DirectoryServices.DirectoryEntry] $user
)
return $user.MemberOf | ForEach-Object { [ADSI] "LDAP://$_" } | Sort-Object { $_.Get("name") }
}
function Get-GroupUsers {
param(
[System.DirectoryServices.DirectoryEntry] $group
)
return $group.Member | ForEach-Object { [ADSI] "LDAP://$_" } | Sort-Object { $_.Get("displayName") }
}
function Get-OccasionalProperty {
param(
[System.DirectoryServices.DirectoryEntry] $entry,
[System.String] $property
)
try { return $entry.Get($property) }
catch { return $null }
}
$user = Get-User $username
Write-Host ("{0}" -f $user.Get("displayName"))
Write-Host ("{0}" -f $user.Path)
Get-UserGroups($user) | ForEach-Object {
Write-Host (" - {0}" -f $_.Get("name"))
$description = Get-OccasionalProperty $_ "description"
if($description) {
Write-Host (" {0}" -f $description)
}
Write-Host (" {0}" -f $_.Path)
}
param(
[string] $username = $env:username
)
function Get-User {
param(
[System.String] $username
)
$ds = New-Object System.DirectoryServices.DirectorySearcher
$ds.Filter = "(&(objectCategory=person)(sAMAccountName=$username))"
$ds.SearchRoot = "LDAP://{0}" -f ([ADSI] "LDAP://RootDSE").Get("rootDomainNamingContext")
return [ADSI] $ds.FindOne().Path
}
function Is-ActiveUser {
param(
[System.String] $username
)
$ds = New-Object System.DirectoryServices.DirectorySearcher
$ds.Filter = "(&(objectCategory=person)(sAMAccountName=$username)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))"
$ds.SearchRoot = "LDAP://{0}" -f ([ADSI] "LDAP://RootDSE").Get("rootDomainNamingContext")
try {
if($ds.FindOne().Path){
return $true
}
return $false
}
catch {
return $false
}
}
function Show-UserInfo {
param(
[System.DirectoryServices.DirectoryEntry] $user
)
$props = @("givenName", "sn", "mail", "employeeID", "sAMAccountName", "countryCode", "physicalDeliveryOfficeName", "manager")
foreach ($prop in $props) {
Write-Host (" {0}: {1}" -f $prop, (Get-OccasionalProperty $user $prop))
}
Write-Host (" active: {0}" -f (Is-ActiveUser $user.Get("sAMAccountName")))
}
function Get-OccasionalProperty {
param(
[System.DirectoryServices.DirectoryEntry] $entry,
[System.String] $property
)
try { return $entry.Get($property) }
catch { return $null }
}
$user = Get-User $username
Write-Host ("{0}" -f $user.Get("displayName"))
Write-Host ("{0}" -f $user.Path)
Show-UserInfo $user
using System;
using System.Collections;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Linq;
namespace worm.hole
{
class Program
{
static void Main(string[] args)
{
var username = args.Any()
? args.First()
: Environment.UserName;
var user = Directory.GetUser(username);
Console.WriteLine("{0} ({1})", user.Name.Substring(3), user.GetProperty("displayName"));
foreach (var group in user.GetGroups())
Console.WriteLine(" - {0}", group.Name.Substring(3));
Console.ReadKey();
}
}
static class Directory
{
private static readonly string Protocol = (!string.IsNullOrWhiteSpace(Environment.UserDomainName) && Environment.MachineName != Environment.UserDomainName)
? "GC"
: "LDAP";
private static readonly DirectoryEntry SearchRoot = new DirectoryEntry(string.Format("{0}://{1}", Protocol, new DirectoryEntry(string.Format("{0}://rootDSE", Protocol)).Properties["rootDomainNamingContext"].Value));
public static DirectoryEntry GetUser(string username)
{
var sr = new DirectorySearcher
{
Filter = string.Format("(&(objectCategory=person)(sAMAccountName={0}))", username),
SearchRoot = SearchRoot
}.FindOne();
return sr != null
? sr.GetDirectoryEntry()
: null;
}
// One property
public static object GetProperty(this DirectoryEntry user, string propertyName)
{
return user.Properties.Cast<PropertyValueCollection>()
.Where(o => o.PropertyName.Equals(propertyName, StringComparison.OrdinalIgnoreCase))
.Select(o => o.Value).FirstOrDefault();
}
// All properties
public static Dictionary<string, string> GetProperties(this DirectoryEntry user)
{
return user.Properties.Cast<PropertyValueCollection>()
.ToDictionary(x => x.PropertyName, x => x.Value.ToString());
}
// Top level groups
public static IEnumerable<DirectoryEntry> GetGroups(this DirectoryEntry user)
{
return ((IEnumerable)user.Invoke("Groups"))
.Cast<object>()
.Select(x => new DirectoryEntry(x));
}
// Token groups (including nested)
public static IEnumerable<DirectoryEntry> GetTokenGroups(this DirectoryEntry user)
{
user.RefreshCache(new[] { "tokenGroups" });
return user.Properties["tokenGroups"]
.Cast<byte[]>()
.Select(x => new System.Security.Principal.SecurityIdentifier(x, 0))
.Select(x => new DirectorySearcher { SearchRoot = SearchRoot, Filter = string.Format("(objectSid={0})", x.Value) }.FindOne())
.Where(x => x != null)
.Select(x => x.GetDirectoryEntry());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment