Fixing the auto correct – today’s pet hate! 

The mysterious case of the missing hyphen…

Whilst most of the time auto correct does a great job the millions of funny posts showing where it got it wrong just highlights the fact that automation is a double edge sword.

For example here are the top 25 funniest auto corrects

http://www.damnyouautocorrect.com/13603/the-25-funniest-autocorrects-of-dyacs-first-year/

This post is not really about auto correct on your mobile rather the auto correct that office does as you type text.

Mostly it does a good job

Mostly it does a good job but some changes are pretty annoying like it’s conversion of the humble hyphen to a dash – obviously someone at Microsoft thought a dash looks nicer than a hyphen.

Here’s the top ten annoying things in Office, apparently 🙂

http://www.techrepublic.com/blog/microsoft-office/10-annoying-word-features-and-how-to-turn-them-off/

 Getting the ASCII Code

In a powershell prompt you can get the ASCII code by doing this [byte][char]”-” and this would return 45, not quite the meaning of life which is 22 according to the hitch hikers guide to the universe but it’s closer to it than the ASCII code for a “dash” which is 150.

Why do we care what the ASCII code is?

Why is this a problem?  Well if you use word and then paste to excel and enter a hyphen it might get converted to a dash.  Still not a problem until you want to use import-csv to get that data into a variable so that PowerShell can do something useful with the data.  What you’ll discover is that the dash disappears and is replaced with a space or maybe a nice little square box depending on the encoding you select.  It’s not a space it’s CHR 150 but you can’t see it so it’s displayed as a space.  Try splitting the string with .substring(position,1) and then cast it to a byte as mentioned above.

A real world problem

Today I was importing data into AD using a CSV file provided by the HR department.  Somehow one of the cells had a dash instead of a hyphen.  When I pushed the data into AD the hyphen or more accurately the dash got lost.  There was another problem with the file in that the column names didn’t match the AD attribute names.  This was easily dealt with by formatting the data as I imported it like this… ( the UTF encoding keeps those pesky UMLAUTS where they need to be in case you are wondering why I used it)

Translating the column names and calculating values

$HRUsers = Import-Csv $HRFile -Encoding “UTF7” |
Select-Object @{Name=”EmployeeID”;Expression={$_.’PS ID’}},
@{Name=”givenName”;Expression={$_.’First Name (PRF)’}},
@{Name=”sn”;Expression={$_.’Last Name (PRF)’}},
@{Name=”Title”;Expression={$_.’Business Title’.replace([char]150,”-“)}},

All I need do is set the Name to the AD attribute Name and then the Expression uses the column name from the HR extract.

Replacing the dash with a hyphen

To deal with the dash problem I used a replace in the expression:

$_.’Business Title’.replace([char]150,”-“)

Which replaces the DASH with a hyphen, just how we like it 🙂

Easy – well it is, now I’ve posted this.  There were a lot of people with similar issues but no full solution that I saw, so I cobbled this together from the pieces I found.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.