Enterprise Software

How to automate account pre-staging in WDS with PowerShell

Streamline your Windows Deployment Services rollouts by pre-staging computer accounts in Active Directory or in stand-alone mode using PowerShell.

Image: Microsoft

Deploying computers can be one of the most complicated processes a Windows sys admin will face. Even with the right tools and knowledge, a rollout can still go sideways, because it relies on multiple variables to go smoothly.

Luckily, Microsoft has modified much of the machination of deploying devices with Windows Deployment Services (WDS) and turned it into a modular, lightweight deployment tool. And with PowerShell, Microsoft has gone a step further and managed and scripted much of the process, too.

One prerequisite that should be followed when deploying devices is pre-staging accounts in Active Directory (AD) — even if you're using WDS (Figure A) in stand-alone mode. It makes for a smoother process, and it can greatly assist in joining machines to the domain post-deployment.

Figure A

Image: Microsoft

Requirements to automate pre-staging

  • Server with Windows Server 2008+ installed
  • WDS role installed and configured
  • PowerShell
  • CSV formatted file with computer names and MAC addresses

Setting the correct CSV file format

First, let's look at how to properly format the CSV file we'll be using to link the MAC addresses on the computers to the computer names we wish to assign.

1. Correlate a list with two columns separated by a comma: HostName,GUID.

2. For each device that will be added to the list, enter the computer name you wish to assign to that device, followed by a comma and the MAC address that pertains to the device (Figure B).

Figure B

wdspowershellfiga032116.jpg
Image: Jesus Vigo/TechRepublic

3. After the information for each computer is entered, save the file with a CSV extension to the C drive for use in the next section.

Note: The CSV file acts as a translator, telling the PowerShell command below to match the MAC address on the device with the hostname provided and to use that hostname to create a computer object in the directory.

Using PowerShell to automate the import process

1. Launch Notepad.

2. In Notepad, enter the command that will be run in PowerShell. There are three parts to it, so we will go section by section, beginning with the first part: stating our variable.

$csv = Import-Csv C:\filename.csv;

The line above will tell the server to use the CSV file created in the previous section and saved to the C drive. The command Import-Csv will process the file and place the output of that first command in the $csv variable for use again during the course of the process.

3. For the next line in the PowerShell command, press Enter twice to skip down two lines and enter the second section: reapplying the variable to each computer in the line.

Foreach ( $line in $csv ) {

The command Foreach will cause the command to process each line in the CSV independently, applying the command that will be entered in section three over and over again until all the lines have been processed.

4. The final line in the command also requires pressing the Enter key twice to skip down two lines to enter the third section: creating a computer object for each line in the CSV file.

WDSUtil /Add-Device `"/Device:$($line.Hostname)`" /ID:$($line.GUID)

}

WDSUtil is a PowerShell command used to makes changes to WDS instead of doing so via the GUI. Unlike the GUI, the PowerShell version has the ability to process multiple entries for scripting purposes; this is why the command is able to automate the tedious process of creating accounts (Figure C).

Figure C

wdspowershellfigb032116.jpg
Image: Jesus Vigo/TechRepublic

5. Once all the sections have been entered, save the file with a PS1 extension.

With both components in place, you are now ready to execute the PS1 file to automate the importing process. Simply double-click the PS1 file to execute the PowerShell command. If you receive an error that the command is prevented from executing, you may need to reconfigure PowerShell to allow for execution of commands.

To sidestep this, find PowerShell and right-click the shortcut, select Run ISE As Administrator. After authenticating, PowerShell's Integrated Scripting Environment (ISE) will launch. In this mode, go to File | Open and locate the PS1 file created in section two. You will see the breakdown of the code and be able to launch it from within this environment by selecting the green play button. The results of the script will be output along the bottom half of the screen (Figure D).

Figure D

wdspowershellfigc032116.jpg
Image: Jesus Vigo/TechRepublic

Upon the scripts' completion, launch WDS and refresh the Active Directory Prestaged Devices node if you're running WDS in native mode. If you're running WDS in stand-alone mode, refresh the Prestaged Devices node, and each computer in your CSV file should have an entry in the WDS prestaged devices pane.

Share your experiences

How is WDS working in your organization? Are you having any pain points? Please share your comments.

Also see

About

Jesus Vigo is a Network Administrator by day and owner of Mac|Jesus, LLC, specializing in Mac and Windows integration and providing solutions to small- and medium-size businesses. He brings 15 years of experience and multiple certifications from seve...

Editor's Picks