Showing posts with label powershell. Show all posts
Showing posts with label powershell. Show all posts

Friday, August 9, 2013

Use PowerShell to send test email in batches or on schedules

Recently, I had to test a scenario where an ActiveSync device received a relatively large (70-100) batch of emails in a relatively short (60-90s) window.

For smaller tests of this type, I've set Outlook into Offline mode, created 5-10 draft emails, sent them all, then switched back to Online mode to have them send at the same time. In this case, I wasn't interested in copypasta'ing 70 to 100 times per test, so I did some quick research, and found that Exchange 2010 has a Send-MailMessage cmdlet that ... well ... sends a mail message...

Anyhoo, a bit of Google-fu led me to this ASP.Net forum post. I ran the HTML code in the final reply through a parser, and found a script that pointed me to JFRMilner's Tech Blog. I saw an opportunity to expand that script and add some functionality, and some intense PowerShell scripting later, I had a finished product. You can check it out on Gist.

Wednesday, July 17, 2013

PowerGUI Syntax Highlighting

If you do any scripting in PowerShell, allow me to strongly recommend that you check out PowerGUI. It's exceptionally handy, and pretty tweakable.

After reading Jason Slaughter's TechNet Blog post on Exchange ActiveSync logging via PowerShell, I realized that I wasn't satisfied with the default syntax highlighting in PowerShell, and went on a quest to find out how to improve it. As it turns out, I wasn't able to find much on the topic online. The PowerGUI wiki mentions that it's possible to change via an XML file, but doesn't go into much more detail than that. Adam at CSharpening.net has a blog post with the results of his foray into the topic, which also pointed me to the list of named colors that could be used, which came in handy.

Using this as a foundation, I got to work.

First, a couple of quick notes. In order to save your changes to the PowerShellSyntax,xml file, you're going to have to open it with elevated rights. Also, these changes don't take effect immediately, so you're going to have to close and re-open PowerGUI each time you make changes in order to see their effects.

As I worked through the different styles, I realized that I actually wasn't sure when some of them were supposed to be applied, so I wrote a syntax test script with examples of each type, and a block at the bottom that lists the strings that trip each type. You can snag it from GitHub.

One of the first things I noticed when working my way through the XML was that the AutoVars style was not used anywhere. There's a PSAutoVars group defined, but it uses the VariableStyle style. I changed that, since a separate highlighting style makes sense for these variables:

Find:
<ExplicitPatternGroup TokenKey="PSAutoVars" TokenID="23" Style="VariableStyle" LookAhead="{NonWordMacro}|\z" CaseSensitivity="AutoCorrect">

Replace with:
<ExplicitPatternGroup TokenKey="PSAutoVars" TokenID="23" Style="AutoVars" LookAhead="{NonWordMacro}|\z" CaseSensitivity="AutoCorrect">

In the list below that line, I also added $true and $false, since those are also built-in variables that I wanted highlighted using this style.

That done, I messed with styles themselves. Here's what I finally settled on:
<!-- Highlighting Styles -->  <Styles>    <Style Key="ReservedWordStyle" ForeColor="Blue" Italic="True" />    <Style Key="OperatorStyle" ForeColor="Red" />    <Style Key="OperatorWordStyle" ForeColor="Red" />    <Style Key="VariableStyle" ForeColor="DarkSlateGray" Bold="True" />    <Style Key="CmdletStyle" ForeColor="Blue" />    <Style Key="NetClassStaticStyle" ForeColor="teal" />    <Style Key="NetClassStaticMethodStyle" ForeColor="saddlebrown" />    <Style Key="CmdletParamStyle" ForeColor="Navy" />    <Style Key="NumberStyle" ForeColor="Black" />    <Style Key="StringDelimiterStyle" ForeColor="Purple" />    <Style Key="StringDefaultStyle" ForeColor="Purple" Italic="True" />    <Style Key="CommentDelimiterStyle" ForeColor="Green" />    <Style Key="CommentDefaultStyle" ForeColor="Green" />    <Style Key="AutoVars" ForeColor="DarkOrange" Bold="True"/>    <Style Key="FunctionStyle" ForeColor="CadetBlue" />  </Styles>


And a side-by-side for ya: