Posts tagged technology

ipHouse Dot Logo

Adding Exchange 2010 mailboxes from text file with PowerShell

I wrote before about adding Exchange 2010 mailboxes with PowerShell and AWK. I was having some trouble with the syntax of importing from a .csv or tab-delimited file so I punted and used awk on my workstation and got the work done.

That workflow is not ideal. I’d rather do it all in PowerShell. I got some great help from the fine folks over at /r/powershell and Don Jones’s PowerShell books and videos.

Here is a better way:

  • Use the Import-Csv cmdlet to import the data as an array objects with text properties, for each column.
  • Add and adjust the properties we need and their values.
  • Pass the whole array to New-Mailbox, which will do the right thing, as long as all the parameter names match the object properties.

If I exported the data as .csv, with properly named column headers, this would get even easier, but I will give PowerShell the same data I gave awk for the sake of parity. So let’s say I have no control over the format the data arrives in and it comes space-delimited like this:

Alice Adams aadams aadams@corp.domain.com Password1
Bob Baker bbaker bbaker@corp.domain.com Password2
Charlie Carter ccarter ccarter@corp.domain.com Password3
Dan Davis ddavis ddavis@corp.domain.com Password4
Ed Evans eevans eevans@corp.domain.com Password5
Frank Foster ffoster ffoster@corp.domain.com Password6

Here is how to use PowerShell to add these users using the data from this file.

To use a space for the field delimiter, we’ll use -Delimiter ‘ ‘. This file does not have a header row. Import-Csv imports as key-value pairs, so each column needs a name.  By default, it uses the top row for that, but that would not be the right thing to do here, since the top row is data.  So we can either put a header row on the file, or define alternate column names with a -Header argument.  Here is the command import my users.txt file as an array of objects, $users:

PS> $users = Import-Csv -Delimiter ' ' -path .\users.txt -Header FirstName, LastName, SamAccountName, UserPrincipalName, plaintextpass

This loads the data from the file into an array of objects $users.  Each element of $users has properties as defined in the header with values from the corresponding row.  Here’s the first element in $users:

PS> $users[0]

FirstName         : Alice
LastName          : Adams
SamAccountName    : aadams
UserPrincipalName : aadams@corp.domain.com
plaintextpass     : Password1

Next, we’ll add the “Name” property, which is a string in the form “FirstName LastName”

PS> $users = $users | Select-Object -Property *, @{name='Name';expression={$_.FirstName + ' ' + $_.LastName}}

The property is appended to the end of the list, but that’s fine, since Add-Mailbox accepts these arguments in any order. Here’s how the first object looks now.

PS> $users[0]

FirstName         : Alice
LastName          : Adams
SamAccountName    : aadams
UserPrincipalName : aadams@corp.domain.com
plaintextpass     : Password1
Name              : Alice Adams

Add-Mailbox wants the password as a system.securestring, and won’t accept a plain string at all. Items of type System.SecureString is stored in memory encrypted.  We’re defeating the security benefits of that behavior by handling the passwords as plaintext elsewhere in the script and in the data file. For exactly that reason, ConvertToSecureString will complain if we use it to accept plain text with -AsPlainText, but it will do it anyway if we use -Force.  The whole thing goes like this.

PS> $users = $users | Select-Object -Property *, @{name='Password';expression={(ConvertTo-SecureString -AsPlainText -Force -String "$_.plaintextpass")}}

Now we have the password stored as a SecureString.  Trying to print the password only prints “System.Security.SecureString” and not the actual contents, but it is in there.

PS> $users[0]

FirstName         : Alice
LastName          : Adams
SamAccountName    : aadams
UserPrincipalName : aadams@corp.domain.com
plaintextpass     : Password1
Name              : Alice Adams
Password          : System.Security.SecureString

Now let’s get rid of that plaintext password.  Strictly, this step is not necessary. Since “plaintextpass” does not match any of the arguments that Add-Mailbox accepts, it will be discarded.  But since we need to encrypt the password as a SecureString to pass it anyway, why pass it as plaintext as well.  So we strip the property out like this:

PS> $users = $users | Select-Object -Property * -ExcludeProperty plaintextpass

And finally, our objects look like this:

PS> $users[0]

FirstName         : Alice
LastName          : Adams
SamAccountName    : aadams
UserPrincipalName : aadams@corp.domain.com
Name              : Alice Adams
Password          : System.Security.SecureString

It is not an accident that these are exactly the arguments that Add-Mailbox wants.  This is the fun part.

PS> $users | Add-Mailbox

That’s it. The contents of the properties of each object in $users are passed to the corresponding arguments Add-Mailbox accepts.  Add-Mailbox takes those arguments and creates six new users.

And of course, since this is powershell, all of this can be done in one big pipeline if readability is not your thing.  That would look like this:

PS> Import-Csv -Delimiter ' ' -path .\users.txt -Header FirstName, LastName, SamAccountName, UserPrincipalName, plaintextpass | Select-Object -Property *, @{name='Name';expression={$_.FirstName + ' ' + $_.LastName}}, @{name='Password';expression={(ConvertTo-SecureString -AsPlainText -Force -String "$_.plaintextpass")}} | Select-Object -Property * -ExcludeProperty plaintextpass | Add-Mailbox
ipHouse Dot Logo

Tegile in the news, and so are we!

Congratulations to Tegile, whose press release today (picked up on multiple news sites, links below) includes one of the reasons we chose their HA2100EP storage array for our needs: Low latency & high throughput. We also needed iSCSI and F/C for our customers.

ipHouse has a Tegile Zebi storage array in production since March, 2012, and the increase in performance has been noticeable.

ipHouse Deploys Tegile’s Zebi Storage Array http://ger.ms/KmJXa3 – Exciting to see Tegile growing and I’m still happy with my choice in new storage for our VMware clusters.

Newcomer gets out its box, plans to sell it cheaply to all comers http://ger.ms/LTjuzY

Tegile Selected as a Red Herring Top 100 North America Tech Startup http://ger.ms/KxUJZE

Our vmForge VDC clusters are peaking around 14,000 IOPS and the MASS solution is offloading about 11,500 IOPS via SSD. I wish I could graph this and show it to the public at large but I don’t have a way yet. (those are peaks, average is closer to ~8,000 IOPS with ~6,900 IOPS via SSD)

ipHouse Dot Logo

Common confusion between DNS and web configurations

There is always confusion about what DNS does and what it doesn’t do. In particular, I see constant reference to DNS functions mixed up with web server functions, and vice-versa. Hopefully this post clarifies things a bit to separate what DNS does and what web servers handle.
(more…)

This Old Code

Although revisiting and updating existing code isn’t necessarily fun or an obviously lucrative way to spend your limited time, it can certainly pay dividends. I know my personal knowledge, skill, and experience have changed over the years, and code which seemed perfectly good six years ago can be painful to read now. Perhaps you’ve gained new appreciation for readable code in general. Or limiting how deeply you nest your conditional blocks, or avoiding incomprehensible loops six pages long. Regardless, code which is easy to read and understand is easy to maintain, and has fewer bugs.

Sometimes, its not your skill which was necessarily at fault, but your environment. Perhaps the code has simply outgrown the original project scope, and become littered with references and obscure exceptions which were bolted on later. Reconsidering and refactoring the code is a necessary step to regaining control of the chaos. Or the original project simply didn’t afford enough time for development, and you had to leverage existing code which didn’t quite fit. Our own ipMom account interface started life as PostfixAdmin, which was quick and easy to put into production, though you wouldn’t be able to tell anymore.

Finally, programming languages themselves change. New libraries are added, and new functions which can make your code leaner and cleaner overall. With relatively new programming languages, its easy to have code which predates any developed or widely-used best practices for the language. Bringing the code up to spec now will make it easier for you, and easier for others, to maintain it in the future.

ipHouse Logo

Automagic Hacklisting of IP addresses

So, I got a little tired of FTP and SSH brute force attempts. I know that if you have strong passwords on your system, you can safely ignore them, and on customer systems behind real firewalls, I do so. However, on my personal systems, I have 0 problem blocking people who annoy me. So I installed pfBlocker on my virtual firewall to see what I could do.

pfBlocker is a package that has blacklist functions that supersede a couple older packages. I initially installed it a replacement for CountryBlock. The first thing I did was go through my logs and see which countries were the most obnoxious. China was the first to go, followed by Southeast Asia, and Venezuela. Sorry, I don’t want you accessing my network.

That allow took care of 70% of my attempted exploits. There are, however, plenty of compromised machines in the United States of America, so I had to think of something else.

(more…)

Go to Top