A company called Quest provides an extra snap-in for PowerShell. The idea
is for these Active Directory cmdlets to work alongside the native PowerShell
commands. As a result we can examine users' properties, and with care,
change values and even reset their passwords.
# PowerShell QADUser cmdlets Get-Command | Where {$_.name -match "QADUser"}
Note 1: There is a rich seam of verbs that you can apply to
QADUser. You can examine the user with 'get', then configure them with 'set', 'enable'
or 'unlock'. To facilitate a bulk import of users from a spreadsheet
there is also, 'new-QADUser'.
Objective: To Get Information About Active Directory Users
Let us assume that you
have fulfilled the above pre-requisites, now there are just two things to
do before my scripts will work:
a) Connect to Active Directory, best would be to logon at a domain controller
in a test network. Remote connection works well, and you could try
Virtual PC for your test network.
b) Find the variable $OU in my script(s); then amend its value to reflect
your domain and your Organizational Unit. You many need a little
extra work with Active Directory Users and Computers in creating an OU and a handful of users.
# PowerShell script to list
Active Directory users in a named OU $OU = "YourDomName/YourOu" Get-QADUser -SearchRoot $OU
Note 1: -SearchRoot is the parameter which
connects to Active Directory.
Note 2: You did change the value of $OU -
didn't you? Also Remember that these QAD cmdlets don't exist in the initial PowerShell
install, they are only available after you successfully run: add-PSSnapin
quest.activeroles.admanagement. If your script does not work refer
back to the pre-requisites.
Note 3: DN, SID, GUID, UPN or Domain\UserName
Guy Recommends: Solarwinds' Free Bulk Import Tool
Import users from a spreadsheet. Just provide a list of the
users with their fields in the
top row, and save as .csv file. Then launch this FREE utility and match
your fields with AD's
attributes, click to import the users. Optionally, you can
provide the name of the OU where the new accounts will be born.
There are also two bonus tools in this free download, and all 3 have been approved by Microsoft:
Example 2a: How to Discover the Names of a User's Properties
These QAD cmdlets are designed to fit seamlessly into PowerShell, for
example we can apply our trusty interrogation techniques such as, Get-help
Get-QADUser.
# PowerShell script to list a User's
Properties # Author: Guy Thomas # Version 1.1 August 2008 tested on PowerShell v 1.0
Get-Help Get-QADUser
Note 1: I suggest you try my parallel learning
technique, and match the user properties revealed by QADUser, with the
property sheet that you see in Active Directory Users and Computers.
Note 2: PowerShell's help tells us that you can
connect to an individual user if you know their: Domain\UserName, DN
(Distinguished name) or UPN (victim@yourdom.com).
Example 2b: How to List a User's Property with Get-QADUser
As with many of my scripts, there are two learning threads in this example, a real-life
objective (Listing user properties) and also learning PowerShell
techniques
(Piping and word-wrap).
Important Preparation: Change the value of $OU.
"YourDomName/YourOu" is unlikely to work on your domain, so adjust this
value. Any doubts of the name, consult your Active Directory Users and Computers.
# PowerShell script to list users
and their DisplayNames # Author: Guy Thomas
Note 1: The unusual backtick symbol (`) means, wrap the
command to the next line.
Note 2: The pipe symbol (|) is PowerShell's
signature tune; it means push the output of the first clause (Get-QADUser) into this
next command (format-Table).
Challenges: If I were you I would take a timeout to add
values to your user's property sheet, e.g. LastName, or DisplayName.
The second part of my challenge is to put into practice what we learned with Get-help QADUser,
namely to add different fields from my example 2b, for example, Company or Office.
Here is
further advice on researching these LDAP properties.
Guy Recommends: A Free Trial of the Network Performance Monitor
(NPM)
Solarwinds'
Orion performance monitor
will help you discover what's happening on your network. This
utility will also guide you through troubleshooting; the dashboard will
indicate whether the root cause is a broken link, faulty equipment or
resource overload.
Perhaps the NPM's best feature is the way it suggests solutions to network
problems. Its
second best feature is the ability to monitor the health of individual VMWare
virtual machines. If you are interested in troubleshooting, and creating network maps, then I recommend that you take advantage of Solarwinds' offer.
My objectives here are twofold, firstly, to practice scripting Active
Directory in a relatively harmless fashion. For instance, changing a user's
property called 'DisplayName' is less intrusive than changing their password.
Secondly, if we add a text string to displayName then we
have a 'handle' to filter Active Directory. Just to emphasise that
the benefit of having a known value for displayName is that we have an extra control to prevent
a rogue script changing everybody's password.
Important Preparation: As with example 2, you
need to edit the this line: $OU = "YourDomName/YourOu"
# PowerShell script to change a
user's properties with Set-QADUSER
Note 1: Never miss a chance to learn a PowerShell
verb; mostly we employ, 'get', but observe that here we also employ the more
active 'set'.
Note 2: See how I reinforce the idea of piping (|);
for example,
the output of 'set' becomes the input of FT, which stands for
format-Table.
Timeout: Investigate -SearchScope
Before we are
ready to experiment with the -SearchScope parameter, I invite you to create a child OU underneath "YourDomName/YourOu".
Next, create few new test accounts in the child OU.
Amend this line of example 3: Get-QADUser -SearchRoot $OU -SearchScope 'OneLevel' `
Next try 'SubTree': Get-QADUser -SearchRoot $OU -SearchScope 'SubTree' `
Here is a script which sets the password for users. The
variable $OU specifies the precise location of the user accounts
targeted in your
domain.
Be aware: This script has two safety catches. Firstly, it
changes only users with a particular value for DisplayName; secondly I use
the -whatIf parameter to test the output.
If the script does as you wish, then remove the last line.
# PowerShell Set-QUADUser script to change users' passwords
Note 1: Set-QADUser has different properties
from Get-QADUser, for example, 'set' has a property called -userPassword.
Note 2: As mentioned previously, this script
has 'where-Object' clause which acts an extra check that you are changing the
users with a particular displayName. Once you understand how this
script works, you could remove the 'where-Object' clause.
Guy Recommends:
The SolarWinds Exchange Monitor
Here is a
free tool to monitor your Exchange Server. Download and
install the utility, then inspect your mail queues, monitor the Exchange
server's memory, confirm there is enough disk space and check the CPU
utilization. This is the real deal - there is no catch. SolarWinds
provides this fully-functioning product for free, as part of their commitment to
supporting the network management community.
Beware: This script has NO safety catch. If you prefer,
you could begin by appending the -whatIf parameter to the last line, as in the the script
above.
Preliminary step, investigate the parameters with the command: Get-help
Set-QADUser.
Result of above research: UserMustChangePassword sounds
interesting. Incidentally, this PowerShell parameters seem much friendlier than the
equivalent pwdLastSet and userAccountControl of VBScript.
# PowerShell script to set a user's passwords and force a change at
logon #
Note 2: Setting 'userMustChangePassword 1' looks
easy, and seems logical enough. However, I only hit upon this value of
numeric one after failing with = "Yes", True, and "1". You need just
plain 1 with no
speech marks, and no equals sign.
Note 3: Observe just how I just appended
the -userMustChangePassword parameter. Did I use a comma?
No. A semi-colon? No. Just straightforward
userMustChangePassword 1.
Warning: If you are not sure of what's
happening here, I strongly recommend that you append -whatIf.
For those who know what they are doing it is possible to create a
script which changes all Active Directory accounts. The secret is
to persuade the script to start at the domainRoot/. The way you
achieve this dangerous task is to shorten the line: $OU = "YourDomName/YourOu",
to $OU = "YourDomName/".
The result would be a script which could 'get', or 'set' all the
accounts.
»
Summary of PowerShell QADUser
There is a whole family of QADUser commands each preceded with a different verb. The two
cmdlets that I
feature on this page are 'get' and 'set'. As for learning
progression, research how to extract existing properties, then try
'setting' innocuous properties such as DisplayName. Once you have
mastered the basics and stumbled upon the 'whatIf, then you can tackle
real tasks such as changing users' passwords.
If you like this page then please share it with your friends
Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems.
Fortunately, Solarwinds
have created the
Free WMI Monitor so that you can actually see and understand these gems of
performance information. Take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.