PowerShell examples to query ActiveDirectory users

Caching AD queries for all users

Caching is useful for slow network connections or testing. After running, you can perform queries on $ad

function Get-ADActiveUsers() {
    # Fetch all AD Users who are Enabled and have an EmployeeNumber
    $ad_data = get-aduser -properties * -LDAPFilter '(&(objectCategory=User)(employeeNumber=*)(!(useraccountcontrol:1.2.840.113556.1.4.803:=2)))';
    return $ad_data;
}

# Cache all active users:
$ad = Get-ADActiveUsers

Get employee using EmployeeNumber attribute

Find EmployeeNumber == 123. Here EmployeeNumber is used, but any attribute works.

 $ad | where {$_.EmployeeNumber -eq 123}

output:

CN : John A. Doe
Company : Big Company, Inc.
EmployeeNumber : 123
<...outputs all AD attributes...>

Using a wildcard search on attribute names

This will find all attributes that match the  partial name "name" for EmployeeNumber 123

$ad | where {$_.EmployeeNumber -eq 123} | select *name*

output:

CanonicalName : company.com/Users/John A. Doe
DisplayName : John Doe
DistinguishedName : CN=John A. Doe,OU=Users,OU=Accounts,DC=company,DC=com
UserPrincipalName : jdoe@company.com
<...all attributes matching wildcard...>

Get multiple attributes using Select

This is similar to a SQL select statement, list the only columns to fetch.

$ad | where {$_.EmployeeNumber -eq 123} | select DisplayName, UserPrincipalName

You can combine select with wildcards

$ad | where {$_.EmployeeNumber -eq 123} | select *group*, *name*, EmployeeNumber

Using a wildcard on attribute values

In this case, we replace -eq (equals) with -like (wildcard equals)

$ad | where {$_.UserPrincipalName -like "*smith*" } | select UserPrincipalName, DisplayName

output:

UserPrincipalName DisplayName 
----------------- ----------- 
tsmith@company.com Ted Smith
bsmith@company.com Bob Smith

Test if user exists by returning True / False

If you need True / False of user’s existence, but you don’t want an object.

[bool] (get-aduser -Filter { SamAccountName -eq 'jdoe' })
[bool] (get-aduser -Filter { SamAccountName -eq 'jno' })
[bool] (get-aduser -Filter { EmployeeNumber -eq 123 })

output:

True
False
True

Get total number of active employees

$ad.length

output:

123

Dwarf Fortress — Quantum Stockpiles Made Easy Tutorial

Requirements

Each Quantum Stockpile (QS) requires 1 minecart. If it’s for heavy material (like stone), you want several wheelbarrows for each QS. Make sure at least one dwarf has the mechanic labor enabled, so he can build the track.

Heavy and Light stockpiles

Dwarves move heavy resources like stone and furniture at a slower speed. Wheelbarrows fix this by carrying items at full speed.

Heavy Stone Quantum Stockpile

Lightweight Gem Quantum Stockpile

This time we don’t require wheelbarrows.

see also:

/r/MechGuides/ has many verbose guides for Dwarf Fortress.

Automatically Close All Dwarf Fortress Windows When You Quit

DwarfFortress mod windows.

Previously I had to close all DF and DFHack windows manually. Now when I quit from the game, all windows automatically close.

Create a shortcut to this AutoHotKey script, and use it to run DF instead of the PyLNP shortcut.

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
; 2015/12/06 Jake
; About: Closes extra windows when you exit DwarfFortress

Run, "C:\Games\Dwarf Fortress 40_24 Starter Pack r15\Starter Pack Launcher (PyLNP).exe"
WinWait, Dwarf Fortress,, 120
WinWaitClose

; Add any more windows you want, use the window's title.
WinClose, SoundSense
WinClose, Dwarf Therapist
WinClose, PyLNP

ExitApp

If you use a different pack, replace the line “Run” with your filepath.

If you want to close extra windows, add a “WinClose” line with the title of the window.

 

JavaScript Inheritance in ES5 verses ES6

New `class` keyword

The new keyword class is syntactic sugar. Inheritance in ES6 continues to act the same as ES5.

ES6

class keyword (mozilla.org)

"use strict";

class Polygon {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
}

class Square extends Polygon {
  constructor(sideLength) {
    super(sideLength, sideLength);
  }
  get area() {
    return this.height * this.width;
  }
  set sideLength(newLength) {
    this.height = newLength;
    this.width = newLength;
  }
}

var square = new Square(2);

ES5

classical inheritance (mozilla.org) using Object.create()

function BaseShape() {
  this.x = 0;
  this.y = 0;
}

BaseShape.prototype.move = function(x, y) {
  this.x += x;
  this.y += y;
};

function Rectangle() {
  BaseShape.call(this); // call super constructor.
}

Rectangle.prototype = Object.create(BaseShape.prototype);
Rectangle.prototype.constructor = Rectangle;

var rect = new Rectangle();
rect.move(1, 1);

See also: new operator

Dwarf Fortress script saves addon defaults for a new Fortress

Usage:

This will let you setup several mods with default settings when you create a new fortress.

If you’re using the Dwarf Fortress Lazy Newb Pack, create a new text file named initworld.init and place it in the same directory as PyLNP_dfhack_onLoad.init

After creating your new game, in the DFHack console type:

script initworld.init

file: initworld.init

# autolabor vs Dwarf Therapist: If you prefer to use Dwarf
# Therapist, delete all lines that contain "autolabor".
enable autolabor
enable workflow
enable seedwatch
seedwatch start

enable getplants

autobutcher autowatch
autobutcher start

seedwatch all 15

autolabor CUTWOOD enable
autolabor COOK 1 1 3
autolabor MINE 3 5

workflow count FOOD 80 20
workflow count DRINK 80 30
workflow count BIN 30
workflow count BARREL 30

# You can skip this section if you don't want the initial stats  
seedwatch list
autobutcher list
autolabor list
workflow jobs
workflow list