Pages

Saturday, December 20, 2014

How To Change User Password With PowerShell

I am going to show a couple of very easy ways to change or reset a user’s local or domain account password using PowerShell.  You do not need any PowerShell modules.  Just built in PowerShell will be used to change the password.
Why do anything with PowerShell when you can already use CMD?
An important concept to grasp is the ability to reuse code and process multiple items.
“I can already use the NET USER command from CMD.  Why use PowerShell?”
We want to use PowerShell for 3 reasons:
  1. It’s the new way.  PowerShell creator Jeffrey Snover is now the Lead Architect for Windows Server 8.  Him moving into a role of such importance is the exclamation point on what PowerShell people have been saying since it came out: “Start using it or start getting left behind!”
  2. Reusability.  Even though our simple task of changing a user’s password should not be something that needs to be scripted, and then reused, it could be.  And anything you use with PowerShell can be.  It’s definitely one of the important reasons we want to use PowerShell when we can.  You never know at the start of a project that you won’t want to reuse some part of it on a later project.
  3. Write once, process many.  Once we’ve got a command to change a password, there’s no reason we can’t use that command on multiple objects.  It’s easy, and that’s philosophy is at the core of all automation.
Change every local user account on a computer to have the same password
Lets dive into an example to make a great point about the reusability and the ability to handlemultiple password changes with a single command.
First, we need to get a list of users to change the passwords on.  We’ll use WMI for this task.
A great thing about using WMI for this task is that it can be used remotely or locally.  If you want to run this on a remote computer (appropriately named “remote-pc”) it’s almost as easy – no extra setup required (firewall exceptions and RPC come in handy though.)

How To Get a List of Local User Accounts with PowerShell:

How To Get a List of Local User Accounts from Remote Computer with PowerShell:


Change Password using ADSI object

Secondly, we’ll look at how to change a password using the ADSI object in .NET.
To get the ADSI object for a user, we need the computer name that the local account belongs to, and the user account name.  One thing that we can use from the list of user accounts from WMI that we got with PowerShell is that the computer name and user account name are both listed in a property called “Caption”.   Too bad the Caption property uses a backslash (“\”) and ADSI uses a slash (“/”).  To get around that, I will use the string method Replace().  That part looks like this:
There are several options for telling the ADSI class what kind of account to connect to.  Two of those I find : WinNT (for local accounts), and LDAP (for domain accounts).  Those are case sensitive. Since we’re looking for a local account, it will be in this format:
Retrieving a domain user account is just as easy.  It’s in the form:

How to Set a Password For a User Account

There are two options for creating the user account object:  use it one time then it’s gone, or save it as a variable.
If you’re going to do a password reset, and will not be making any other changes, you can use this simple method of creating your user object instance.  Just use parentheses around the object, and this will ensure that the object is able to be referenced and methods can be run from it.  It looks like this:
After this runs, the password for the account “Accountname” on the computer “Remote-PC” is set to “Shazbot!”.  There is no further reference to the object in the script.
 you know the computer name, and the account name, this one liner will set your password:
On the other hand, if you’re going to need to account for anything else, such as viewing or changing any other property on the account, then you should keep it as a variable.  It’s very similar to the first method:
Now you have a variable that represents the user account, and you can change the password using the same SetPassword() method used above.

How To Reset All Local User Accounts on a Computer to the Same Password

Here’s a quick one-liner that sets all user accounts on a computer to have the same password.
If that seems like gibberish to you, here’s the translation:
Get the local user accounts from WMI, and since we’re not done with those objects, they are passed through the pipeline.  They are the input for the Foreach-Object, which applies everything that’s in the scriptblock to each individual user account.  When being passed through the scriptblock, individual user accounts are referenced by the “$_”.  We build a string like “WinNT://computer/user” by switching the “\” symbol from the user accounts “caption” property into a “/” by the use of the string method Replace.  After the string is put together, [adsi] processes and creates an ADSI reference to the real user account on the computer.  The SetPassword method is called on the object, which sets the password to “FluxCapacitor!11-5-1955”.  It performs that on each local user account, and then it’s done.

Conclusion

PowerShell can be used to administer every facet of Windows Operating Systems.  In this example we see how to easily use PowerShell to work with .NET and WMI to get a list of user accounts from the local, or the remote, system.  The ability to both query WMI and take actions from .NET in the same command is just one example of the flexibility and robustness of PowerShell, and it’s improvements over previous ways of managing one account at a time.

Configuring Basic DNS Records in Windows Server 2008 R2 and 2012

Once a Windows DNS server is up and has been configured with a forward and reverse lookup zone it is ready to be configured with DNS records. This article will start with a brief overview of the primary types of DNS records and will then walk through the configuration of basic DNS record types in Windows Server 2008 r2 and Windows Server 2012.

Basic DNS Record Types

There are a few different types of records that are primarily used for most of the devices on the Internet and inside company intranets. The following is a list of these primary record types:
  • Address (A) record – This type of record is used to translate a domain name to a specific IPv4 address.
  • Address (AAAA) record – This type of record is used to translate a domain name to a specific IPv6 address.
  • Canonical name (CNAME) record – This type of record is used to specify a secondary name (commonly referred to as alias) for an existing A or AAAA record.
  • Mail Exchange (MX) record  – This type of record is used to direct the mail communications for specific domains on the Internet. The record includes a priority and mail exchange agent domain name (this references an existing A, AAAA, or CNAME).
  • Start of Authority (SOA) record – This type of record is typically configured with the creation of a zone and includes authoritative information about a specific domain name.
  • Name Server (NS) record – This delegates the authoritative name servers for a specific domain, this record is also typically configured with the creation of the zone (in simple configurations).

Basic DNS Record Walkthrough

This walkthrough is on Windows Server 2012, but similar steps can be taken on Windows Server 2008 R2. As a starting point, the Server Manager dashboard is used, but any method can be used to access the DNS Manager. Figure 1 below shows that the DNS Server role has been installed and can be selected from the left pane.
Configure Basic DNS Records: server manager

  • Once DNS has been selected, the available DNS servers will be displayed. Right-click on the target server and select DNS Manager, as shown in Figure 2 below.
Configure Basic DNS Records: selecting DNS manager

This will now bring up the DNS Manager. In the image below you can see that both a forward and reverse lookup zone have been created.
Configure Basic DNS Records: DNS manager

  • Choose the forward lookup zone, which will bring up a list of the existing zone records. Figure 4 below shows the basic records that are automatically created by the DNS configuration wizard. The first record that will be created is an A record linking the parent domain name (testing.local in this case) to the IPv4 address 192.168.1.100.
Configure Basic DNS Records: DNS manager

  • Right-click in the right pane and select New Host (A or AAAA). This will bring up a window as shown below in Figure 5.
  • Now fill out the IP address textbox with the target address of 192.168.1.100.
  • Click on the Create associated pointer (PTR) record and select Add Host.
Configure Basic DNS Records: DNS manager host record

  • This will display the successful creation of the record. Select OK and bring back the Add Host window in case multiple records need to be created
  • Select Close. The screen will now show a new A record with the information that was entered.
Configure Basic DNS Records: DNS manager new A record

  • Click on the reverse zone that was previously created. Notice that a new PTR record now exists (as shown below in Figure 7).  This record will allow a reverse lookup of the 192.168.1.100 record to the testing.local domain name.
Configure Basic DNS Records: DNS manager pts record

  • Click back on the forward zone, then right-click on the right pane again and select New Alias (CNAME). This will bring up the window shown in Figure 8.
  • At this point enter www in the Alias Name textbox and enter testing.local in the Fully Qualified Domain name (FQDN) for target host textbox. This will create an alias record for www.testing.local that maps to the A record for testing.local.
Configure Basic DNS Records: DNS manager new CNAME record

  • Select Next, this will bring back the main DNS Manager window (as shown in Figure 9) with a new CNAME record.
Configure Basic DNS Records: DNS manager new CNAME record

  • The next record type that will be created is a MX record. Right-click on the right pane and selectNew Mail Exchanger, which will bring up the window shown in Figure 10. From this window the only thing that will be configured is the “Fully qualified domain name (FQDN) of mail server” textbox. This is because the mail being routed is for the whole testing.local domain and not specific sub-domains. The name that is placed in this textbox is the name of the mail server, in this case mail.testing.local. (An A record for mail.testing.local was added previous to this step but was not covered in the walkthrough).
Configure Basic DNS Records: new mx record

  • Once complete, select OK. This will bring back the main DNS Manager window showing a new MX record.
Configure Basic DNS Records: new mx record

The last record that will be shown created is an AAAA record, which is similar to the A record but works with an IPv6 address instead of an IPv4 address.
  • Right-click on the right pane and select New Host (A or AAAA). This will bring up the window shown in Figure 12 (and Figure 5). In this window enter the IPv6 address 2001:DB8::1 to link to the parent domain name.Configure Basic DNS Records: new host AAAA record

  • Once complete select Add Host, then select OK.
  • Select Done to get back to the main DNS Manager window shown below. This window shows that a new host record has been created using an IPv6 address.
Configure Basic DNS Records: new host AAAA record

And finally these records can be tested by using the Windows nslookup command. As shown below, the various records are looking up correctly.
Configure Basic DNS Records: test new dns record

The basics of DNS are not hard to pick up, but DNS can quickly become complex in larger organizations with multiple DNS servers that serve only portions of the internal networks. However, on smaller networks that use only a few portions the configuration can be very simple and can be easily managed by someone with a good understanding of networks and how DNS operates. Hopefully this article will help in learning the Windows DNS Server role and how it can be configured to meet the requirements of an organization.