Tuesday, 19 February 2008

Send Welcome Email

Wouldn't it be nice to send your new users an email welcoming them to the company and providing them with some info about how things work. Anyway, I had a request to do this and since I don't think you can do it natively within Exchange 2003 I turned straight away to Powershell.

So, all you need to do is run a daily sceduled task which calls the below Powershell script. It finds the current and next days's dates, searches AD for users created between those dates (i.e. on the current day) and sends an email to the users it finds with the relevant info attached.

(Note: You need the Quest AD cmdlets installed unless you want to use ADSI for the AD search part.)

Doing the search by specifiying the exact date didn't seem to work very successfully. Digging around the Internet revealed the correct format to get the dates into and also searching between the two adjacent dates.

#Find today's and tomorrow's dates and store them in the 'yyyyMMdd}000000.0Z' format
$dayStart = "{0:yyyyMMdd}000000.0Z" -f (Get-Date)
$dayEnd = "{0:yyyyMMdd}000000.0Z" -f ((Get-Date).AddDays(1))

#Find AD users created between the two dates
$newuser = Get-QADUser -searchroot 'springfield.local/userou' -LdapFilter "(&(whenCreated>=$dayStart)(whenCreated<=$dayEnd))" -size 0

#Find the email address for each of the users and send a mail to them with the WelcomeTo.doc attachment
foreach ($address in $newuser)
$recipient = $address.mail
$sender = "sender@domain.com"
$server = "smtpservername"
$file = "path to attachment"
$subject = "subject"
$body = "body text"
$msg = new-object System.Net.Mail.MailMessage $sender, $recipient, $subject, $body
$attachment = new-object System.Net.Mail.Attachment $file
$client = new-object System.Net.Mail.SmtpClient $server
$client.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials

Wednesday, 13 February 2008

Scripting Games 2008

Scripting Games 2008 - looking forward to it. Entering the Powershell Beginners divison which should be interesting!


Bye bye my vbscripts?

OK, so I've moved onto Powershell these days, but I thought I'd start my blog posting with one of my favourites. Its adapted from a great idea at http://www.microsoft.com/technet/scriptcenter/resources/qanda/apr05/hey0429.mspx and essentially makes the description field of your computer accounts dynamically populated.

Why might you want to do this you ask?

Well, have you ever needed to find a computer name easily without needing to get the user to find it out? This script if run at login will populate the description field of the computer account with: username, current AD site, make and model of machine, time logged in, e.g.

Joe Bloggs logged on in Paris using HP 6710b, 08:30.

Simply sort your computer accounts by the description column and hey presto easy to find the computer the user is logged into. Simple, but very effective.

I also used it to easily sort computers into separate OU's for laptops and desktops.

On Error Resume Next
Set objSysInfo = CreateObject("ADSystemInfo")

Set objUser = GetObject("LDAP://" & objSysInfo.UserName)
Set objComputer = GetObject("LDAP://" & objSysInfo.ComputerName)
objSitename = objSysInfo.SiteName

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")

For Each objItem in colItems
strManufacturer = objItem.Manufacturer
strModel = objItem.Model

strMessage = objUser.CN & " logged on in " & objSitename & " using " & strManufacturer & ", " & strModel & " " & Now & "."

objComputer.Description = strMessage

(By the way you'll need to add the 'Write description' property on the OU(s) where your computer accounts reside to something like Authenticated Users.)