‘Twas the Night Before Scripting: Part 2

Summary: Dr. Scripto helps an Admin learn how to easily modify custom attributes in Active Directory.

Microsoft Scripting Guy, Ed Wilson, here. If you missed it yesterday, see Sean’s latest video: Just Script It! Also be sure to read yesterday’s post before you start on today’s: ‘Twas the Night of Before Scripting: Part 1.

Here’s Sean…

In yesterday’s episode, we encountered an Administrator who was near tears due to the time constraints of a massive project and the bad timing of it occurring during the holidays. Magically from a spark of ozone, our good friend Dr. Scripto appeared to aid him. Today, we continue as he gains some further insight into Windows PowerShell and the power of automation.

 

And so we continue,

Much deeper to the night,

The Admin who never scripted,

And suddenly took fright.

 

A project that would take

All of the hours

Of holiday time,

And also no showers.

 

But Dr. Scripto

Appeared in time,

In a sparking of ozone,

And badly done rhyme.

 

With PowerShell, they made

Users (so many),

And did it barely

In the cost of a penny.

 

But now, an email stirred

To our good Admin friend,

That made him just panic,

Nearly to the end.

 

“It’s from the Boss.

He insists that I quickly

Update the field,

With the raw Info account provided to me!”

 

Oh, the panic in his eyes,

and gasps of pure dread!

To manually update over 500 staff…

Fear crept to his head!

 

But the good Doctor,

With a twinkle in his eye,

Smiled and said,

“We can solve this without try.”

 

“Let us take the first script,

The one we used to make

The staff of 500,

We’ll change it easy as cake.”

 

So he copied it out,

And pasted it back,

The previous script,

His small coding snack:

 

$Users=IMPORT-CSV Users.csv

 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

}

 

“One more cmdlet

For this task we’ll need,

A simple cmdlet

To aid in our deed.

 

We do not need UPN

Or a password this time,

So we’ll comment out

All of those lines:”

 

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

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

 

Our Admin blinked,

And looked at the screen,

Using the hash mark

Would skip like a sheen.

 

“We must now take info

Provided us from file

USER.CSV and

Combine to a pile:”

 

$First=$User.Firstname

$Last=$User.Lastname

$Acct=$User.UserAccount

$Addr=$User.Address

$City=$User.City

$State=$User.St

$info=$First+”,”+$Last+”,”+$Acct+”,”+$Addr+”,”+$City+”,”+$State

 

Our good friend, Scripto,

Did then type to the screen,

The cmdlet to use

To aid in their mean:

 

SET-ADUSER $Acct –replace @{info=$info}

 

“Ooops! I almost forgot

To do one more thing!

Comment out New-ADUser,

And our script, it can sing!”

 

And so he looked at

The script he would run,

And saved it as the new

UPDATE.PS1:

 

$Users=IMPORT-CSV Users.csv

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

$info=$First+”,”+$Last+”,”+$Acct+”,”+$Addr+”,”+$City+”,”+$State

SET-ADUSER $Acct –replace @{info=$info}

}

 

Running the script

And checking AD

Showed their User;

They had done it correctly!

 

Dr. Scripto looked up

At his friend in the chair,

“Amazing what you can do

With some PowerShell flair!”

 

The strain seemed to

Pass from his face,

Perhaps he might win

In this mad deployment race?

 

Pop in tomorrow as we watch further. Will our good Admin friend manage to find some way to complete all of his work in a reasonable time? Will he be home for the holidays?  Only further reading will tell!

~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 

No Comments