Compartir a través de


‘Twas the Night of Before Scripting: Part 1

Summary:  Dr. Scripto teaches a non-scripting Administrator to easily create users in Active Directory.

Microsoft Scripting Guy, Ed Wilson, here. Returning once again to delight us all with a little holiday merriment is Honorary Scripting Guy, Sean Kearney. Other than his habit of occasionally singing off-key Windows PowerShell music, he has taken on the challenge of writing an annual holiday Hey, Scripting Guy! Blog special.

This year, the Scripting Guys proudly present to you in 3-D PowerShell-O-Rama Vision, the latest holiday edition. But before you rush to read today’s post, see Sean’s latest video: Just Script It!

‘Twas the night before scripting,

A place of deep sorrow,

An Admin sat crying,

Not seeing tomorrow.

 

A project was dropped onto him,

Unfathomable in size,

Impossible to scope,

As he looked to the skies.

 

Management undercommitted,

And sales oversold,

To gain this holiday contract,

How were they so bold?

 

The tasks of the project

Placed before he

Were four tasks that must done

With consistency.

 

And our good friend,

A person like you,

Had never learned to script,

Oh yes, this was true.

 

Our good fellow, he cried.

Tears rolled to the floor

As he stared to the distance,

At the darkness of the door.

 

With very little time,

Just four days, you see,

He was to complete these tasks,

Finish them to a tee.

 

This meant all day at work,

No sleep at all,

No smile from his wife,

No hugs at all.

 

These tasks, you wonder,

Just bad, they were?

I will tell you now

To ruffle your fur.

 

Remember a time,

Far long ago,

When you could not script,

And now you will know.

 

Today he would start

With the first task at hand,

Create some users

Within his new land.

 

Populate a new Active Directory

With five hundred or so

User accounts (details included),

Addresses and mo’.

 

How the tears, they welled

Into the keyboard below,

Sparking and smoking,

Like electrical snow.

 

When out of the room,

He heard a small voice,

Like a tiny digital mouse,

It was oddball, by choice,

 

“My friend, I can see,”

Said a voice far below,

Bespectled and kind,

Was good Dr. Scripto!

 

A wreath of deep green,

Barely eight inches tall,

As white as a December

Massive snowfall.

 

“I’ll show you how,

With no effort, you see,

To use PowerShell to solve

Your problems with glee.”

 

So Dr. Scripto looked at

The first task on the plate,

“We’ll have this done

Before it is late.”

 

The Good Doctor then looked

At the data below,

A spreadsheet called Users.csv

With columns to show:

FirstName,         LastName,         UserAccount,     Address,                       SomeCity,          ST

Arthur,              Dent,                 dent,                 1 Microsoft Way,           Redmond,         WA

Tricia,                McMillan,          trillian,               88 Bridgeport Ave,         Shelton, CT

Peter,                Jones,               thevoice,           16 A Place,                    Ottawa,             ON

Ford,                 Prefect,             towelguy,          3 Beleljuice Lane,            Atlanta,             GA

“Typing would be slow, a pain,

And monotonous to do.

I’ll show you a line

In PowerShell to do.”

 

First, he laid out

Some lines on the screen,

“This will give us our users,

And do it all clean.”

 

$Users=IMPORT-CSV

 

“Now we’ll access their names,

Without overload,

With just a few lines,

Some tiny little code:”

 

Foreach($User in $Users)

{

$First=$User.Firstname

$Last=$User.Lastname

$Acct=$User.UserAccount

$Addr=$User.Address

$City=$User.City

$State=$User.St

}

 

All of the information

Scrolled right by his eyes,

Much to the amazement

And delight of his eyes.

 

“A password, we’ll build,

Simple and sweet,

And the UPN for AD,

So the credentials will meet:”

$TempPassword=(CONVERTTO-SecureString –asplaintext –force –string ‘TempPassword1’

$DomainUPN=’@Contoso.local’

“Now to finish it all,

A cmdlet we’ll use,

Called New-ADUser,

Not a magical ruse:”

Foreach($User in $Users)

{

$First=$User.Firstname

$Last=$User.Lastname

$Acct=$User.UserAccount

$Addr=$User.Address

$City=$User.City

$State=$User.St

$TempPassword=(CONVERTTO-SecureString –asplaintext –force –string ‘TempPassword1’)

$DomainUPN=$Acct+’@Contoso.local’

NEW-ADUSER $Acct –GivenName $First –Surname $Last –StreetAddress $Addr –City $City –State $State
–UserPrincipalName $DomainUPN –AccountPassword $TempPassword

}

“To a file, we’ll save this,

Call it NEWUSERS.PS1,

Now we start it in PowerShell,

And have us some fun:”

.\NEWUSERS.PS1

In a short span of minutes,

Not even time to go click,

The Admin pulled up his list,

And saw users appear so quick:

Image of list

Our Admin friend paused,

And a smile came free,

Perhaps there was hope?

For that, he would see…

Come back tomorrow and we’ll see just how much Dr. Scripto can help our good non-scripting friend. Perhaps he will see the holidays? You’ll know before the end.

~Sean

I invite you to follow the Scripting Guys on Twitter and Facebook. If you have any questions, send email to scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Sean Kearney,
Honorary Scripting Guy and Windows PowerShell MVP