Hey everyone! I hope you’re enjoying your very small break — I know I am. Ok, actually, I’m at work, toiling on, getting your lab machines wiped and reinstalled and ready for a brand new quarter that will start on June 29!
I have some time to kill while servers are installing, so I thought I’d hop on here and share some more command line judo that you can use to help automate the more mundane tasks that you may have. You may remember, I posted a very small sampling of command line tools you could use in a batch file here, and this post is designed to build on that one. These are examples of things I’ve been doing today to keep myself from doing the same thing over and over and over.
So, first of all, my objectives: After setting up my servers (using unattend files… sweet!), I had a number of tasks I needed to accomplish: I needed to either create or join a domain, copy setup files from the Server disc, copy the Support tools files from the Setup disc and then share both sets of files. Finally, I would need to reboot the server so that everything would work properly.
Of course, I could mindlessly go through the punches of accomplishing this through the GUI. But who wants to do that? So I fired up Notepad by issuing this command from the Run command inside Server:
notepad C:\batch.bat
This had the effect of loading notepad and automatically creating a file called batch.bat on the C:. Once open, I clicked inside Notepad and began my batch file adventure…
Creating a Domain
The first thing I set out to accomplish was creating a domain. This is easy enough as I have an unattended install file for this as well, called DC.txt, which lives on a floppy disk. If not using a batch file, I might have typed the following into the Run command, or perhaps as a RunOnce command at the end of my Winnt.sif file I used when I installed the OS.
The command to promote a member server to a domain controller is DCPROMO of course, and I used the /answer switch to point to my set of “answers” for the wizard. Here’s what it looked like:
dcpromo /answer:A:\dc.txt
Joining a Domain
Of course, creating a domain is nice, but I don’t need every single server I install today to be it’s own domain. For the servers who need to join an existing domain as a member server, I used the NETDOM tool. NETDOM isn’t included natively on Server 2003 (although it *is* on Server 2008, provided you have the AD Domain Services role installed) — it’s included when you install the optional Windows Support Tools. So, after my server installation, I quickly installed the support tools, which gave me the ability to include NETDOM in my batch file.
NETDOM has a host of subcommands included with it. The one I was after was JOIN (makes sense). Some options I’d be using would indicating which domain I’d like to join, what domain user account would be joining (this account would need to have rights to add a computer account to the domain), and that user’s password. Here’s what all that looks like:
netdom join wg03svrb /domain:wg03.contoso.com /userd:Administrator /passwordd:P@ssw0rd
Note that after JOIN is wgo3svrb. The command doesn’t automatically assume you mean to join the computer you are working from, and so you’ll need to specify which computer you’re talking about. The computer in this case wg03svrb.
Making Some Directories
I had two directories I needed some files to live in — the files I’ll be getting ready to copy from the Setup media. I needed an “i386″ directory and a “Support” directory inside of the “i386″ one. Making directories is easy with the MD command, and here’s what I put in my batch file:
md C:\i386
md C:\i386\support
Copying
If you’ve ever taken a look at the i386 folder on the Windows Setup disc, you know it’s not just a handful of files — there are a lot of folders with a lot of other folders and yadda yadda. We want to copy that entire structure, and the old-fashioned COPY command won’t cut it, but XCOPY will. I won’t rehash the syntax here, but feel free to see my previous post about command line goodness for more info.
xcopy D:\i386\*.* C:\i386 /s/e/x
xcopy D:\support\*.* C:\i386\support /s/e/x
Yes, I know the /s switch with the /e switch is redundant, but it’s just more memorable to type /s/e/x. What can I say, I’m a guy.
Setting up Shares
Now that the files are copied over, we need to share them. To set up a share, we’ll use the NET command with the SHARE subcommand. I need for the C:\i386 folder to be shared as WinDist, and I need the C:\i386\support folder to be shared as Support. We’ll also need to configure permissions for the share — in this case we want the Everyone group to have Read-only share permissions. for both shares. Here’s the syntax for that:
net share WinDist=C:\i386 /grant:Everyone,Read
net share Support=C:\i386\support /grant:Everyone,Read
As a bonus, if I wanted to map a drive to those shares on another computer, here are the commands I would issue:
net use w: \\wg03svrb\windist
net use x: \\wg03svrb\support
Conclusion
After all this was finished, I needed to reboot my computer (since I either created a domain or joined one). To accomplish this, I would use the shutdown command with the /r switch (/r is for reboot). By default, this displays an annoying box with a 30 second countdown, designed to give me time to save my docs, but since I’m slightly impatient with this, I used the /t switch. The /t switch allows me to configure how many seconds I want to wait — anywhere from zero to 600 (10 minutes). I, of course, chose zero.
shutdown /r /t 0
And I’m done!
All in all, this was a very simple batch file, but it saved me some time and made my life be less of a headache. Here’s what my finished batch file might have looked like:
netdom join wg03svrb /domain:wg03.contoso.com /userd:Administrator passwordd:P@ssw0rd
md C:\i386
md C:\i386\support
xcopy D:\i386\*.* C:\i386 /s/e/x
xcopy D:\support\*.* C:\i386\support /s/e/x
net share windist=C:\i386 /grant:Everyone,Read
net share support=C:\i386\support /grant:Everyone,Read
shutdown /r /t 0
Thankfully it worked beautifully! Hopefully I didn’t forget anything and the summer quarter labs will go smooth as silk!