Steven's profileDevelopmentalBlogLists Tools Help

Blog


    30 March

    Introduction to PowerShell XML

    Hopefully by now you have heard of PowerShell (PS) and know that it is a scripting shell much like BASH or any other Unix shell. However unlike those other languages, PS uses an object pipeline, rather than strings, and those objects work directly with the .Net Framework. What this gives us is a powerful, flexible, scripting platform where developers don't have to learn a whole lot before they can start leveraging some real scripting power.

    There's lots of examples around how to get started with PowerShell so I'd like to skip that part but first I just want to cover off a few key commands. First, everything in PS is an object of some sort. Unless otherwise specified, most commands will deal with objects or arrays of objects. Since all .Net types inherit from System.Object, this is how PS assumes type safety for all command calls.

    Creating an instance of a class is relatively easy. This is how you would create an XmlDocument:

    $xml = New-Object "System.Xml.XmlDocument"

    The dollar $ sign indicates a variable in PS. The New-Object is a command that accepts a string input for the type. We could then continue to load some XML as we would in .Net:

    $xml.Load("c:\somefile.xml")
    $xml.LoadXml("<a><b>test</b></a>")

    However, PS has a nice XML shortcut that you should know about, and that's the [xml] cast:

    $xml = [xml] Get-Content "c:\somefile.xml"
    $xml = [xml] "<a><b>test</b></a>"

    Now lets use something more interesting for our XML:

    $xml = [xml] '
        <world>
            <continent name="Africa">
                <country name="Zimbabwe" />
                <country name="South Africa" />
                <country name="Ethiopia" />
            </continent>
            <continent name="Asia">
                <country name="China" />
                <country name="Japan" />
            </continent>
        </world>'

    Another cool operation with XML is referring to Xml Nodes specifically rather than searching. The .Net way would be this:

    $continents = $xml.SelectNodes("/world/continent")

    This would return an array of Xml Nodes that represent all continents. In PS we can shortcut to:

    $continents = $xml.world.continent

    You can look at the type of any variable with the standard GetType method:

    $continents.GetType()

    Interestingly you will see that rather than an XmlNodeList or array of XmlElements, this in fact is an array of Objects! However if we look at the contents of that array we will see each item is something more reasonable:

    $continents | % { $_.GetType() }

    This command tells us that we have 2 objects of type System.Xml.XmlLinkedNode in our array. The syntax is specific to PS. The | symbol is a pipe, and means to take everything from the left, and shove it into the right. The % symbol is a shortcut for "foreach", which also works just as well. Because we have piped in some content, we don't need to specify any condition logic for our loop, and can go straight to the inner contents. When you don't specify what the name of the variable in the range will be, you can use the $_ variable instead. So overall, the above statement is the same as:

    foreach ($_ in $continents) { $_.GetType() }

    Let's do something a little different. We will attempt to now take our array of Xml nodes representing continents, and put just the continent name into a generic collection of strings. The syntax around creating an instance of a generic class is as follows:

    $list = New-Object "System.Collections.ObjectModel.Collection``1[System.String]"
    $list.GetType()

    The type shows as Collection''1 without the generic part (string) but it is still there, under the covers. Now lets put the name of all continents into that collection with out % syntax:

    $continents | % { $list.Add($_.Name) }
    $list | % { $_.GetType() }

    The second command shows the type of each item in the collection, and we can see they are all strings.

    So that wraps up my first PowerShell post. I've been doing a lot of it this past week, and was very green to begin with. All my work has been around Xml, and I'll be doing some more this week. My specific usage is relating to build and release management in TFS, and am enjoying it greatly.

    When it comes to using PowerShell you can create your scripts in any text editor. Currently I am using Notepad++ with PowerShell language definitions, and also PowerGui. PowerGui in particular is an excellent free tool.

    24 March

    My Introduction To F#

    During the week the QLD MSDN User Group was treated to a presentation by Leon Bambrick who demonstrated F# in all its glory. The very next day I had the opportunity to meet with Don Syme, inventor of F# (you never know who you meet sitting at the hot desks in Microsoft SDC!).

    To me, this was a sign from the powers that be: play with F# or else! First step to get started, is download the latest version MSI from the Microsoft Research site. After you install it, you can open Visual Studio and you will find that you have a new project template:

    image

    Not a lot of options for project types I'm afraid. But its a start at least, and Leon indicated there will be full commitment from Microsoft in the future (don't make me a liar Leon). When you create a new project, its very empty. So we add a new item, and notice a few options:

    image

    To be honest, I have no idea what they all do. I'm as new to this as you! The script file has a FSX extension and this makes me wonder what kind of cool script stuff you can do with it. Undecided, Leon's voice came to me as if in a dream... "Use the Source, Luke, use the source".

    Sure enough, the F# Source File option has a FS extension which is what I was looking for. It comes with a heap of template code for you see how the language syntax works. I was pretty comfortable with the magic keyword 'let' already from Leon's presentation. I set off instead to get the 'interactive' mode to work, since I found this quite a neat feature.

    So it seems that there is a 'FSI.exe' which stands for F# Interactive. It is a command line tool that allows you to pass snippets of F# code followed by two semi-colons ;; to terminate the parameter. It seems to remember your 'state' such that functions are remembered in between calls. This is further implemented as a command window in Visual Studio, which is a nice feature. You have to activate it after you have installed F# MSI, by turning it on in the 'Add In' section:

    image

    Tick all the boxes, and the F# Interactive window opens. Now you can start typing inline F# code to your heart's content! You can even execute sections of existing code in your .FS files by highlighting a section of code, and pressing ALT-ENTER. This is very similar behaviour to SQL Management Studio - Query window, where you can highlight some T-SQL and press F5 to run just that code. The F# Interactive window will show any errors inline.

    For example, when you add a new source file, it comes with a bunch of syntactical examples, and one of them is the "highest common factor" code, as follows:

    let rec hcf a b =           // notice: 2 arguments seperated by spaces
        if a=0 then b
        elif a<b then hcf a (b-a)           // notice: 2 arguments seperated by spaces
        else hcf (a-b) b

    So 'let' is kind of like 'dim' in VB, or 'var' in C# / Javascript, but, er, not really. 'rec' means that the call is recursive. You can't have a function that calls itself without the 'rec' identifier. Can't say that I am a fan of 'elif' instead of 'elseif'.

    I wanted to test to see if this function really works as indicated. Some tests I thought up in my head:

    hcf 15 5 // Should return 5
    hcf 48 52 // should return 4
    hcf 1 1 // should return 1

    I could have deleted all the other redundant code, and entered my 3 tests above, then compiled and run it, but I suppose I would also have to print out the results or some such... too much hassle! So I highlighted the function definition and pressed ALT-RETURN to run it in interactive mode. Then in the interactive window, I typed each example, placing ;; at the end. Each time it returned the correct result:

    val it : int = 5

    Finally I wanted to see the DirectX demo again that Leon showed in his presentation. The MSI also installs these examples, and I ran the DirectX demo in script mode, just to prove to myself Leon isn't a trickster or a witch (burn him!). Very impressive stuff.

    Great! I now feel that I can put F# on my resume. I am interested in exploring it some more. I have some thoughts around graphing and other mathematical applications. I am particularly interested in finding examples of complex algorythms made extremely simple through the F# syntax. Finally, since FSI can effectively be called script-like (and there are those mysterious FSX files), what advantages might we be able to see in using it with other scripting tools/languages like PowerShell?

    Oh, see you all at the launch on Wednesday.

    20 March

    Recent Announcements

    Some interesting things announced today. First, the official dates for ReMIX Australia:

    Sydney: Tuesday 20th May
    Melbourne: Thursday 22nd May

    To be honest I've never been, but I think I'll see if it's doable this year.

    Secondly, the speakers for CodeCampOz are announced! Check out the sessions here.

    Of particular interest to me are the following sessions:

    • Interactive video using Silverlight and WCF - Jonas Folleso
    • ORM Smackdown - Adam Cogan

    Oh and don't forget the Readify presenters!

    Finally, my new Microsoft SDC friend: Joseph Cooney on Silverlight 2.0 and WPF (even though it doesn't say WPF on the CodeCampOz site, trust me, he's doing a WPF / SL comparison).

    All in all its going to be a great weekend! Look forward to seeing you all there.

    16 March

    Visual Studio Built-in Web Server

    No doubt you've hit F5 to debug your web application from time to time, and you noticed the little web server that sits in your system tray.

    aspnet_devserver1In the old days you needed to have your web site setup as a virtual directory in IIS in order to be able to debug it. Now days it's a lot easier: the first time you debug Visual Studio starts up its own mini-IIS that hosts your web application and lets you debug. This works for web sites, ASMX web services, and WCF services.

    By default, it assigns a random port on the local machine. If you already have IIS then no doubt it is configured to use port 80, which is why the VS web server uses another random one. When your application loads up in browser, it will usually have a URL similar to this:

    http://localhost:53669/default.aspx

    Even when you stop debugging, the web server continues to run! Which means you can manually open a browser and still hit that same location. This is perfect if you don't have a version of XP or Vista that comes with IIS.

    However, by default the port number is random. This means tomorrow when you go to open and debug your application, it will run up with a different port number. Sometimes this is no big deal, but with web services and (in my case) WCF services, this hurts. Your WCF endpoints will be configured for debugging and the endpoints will specify the port number, so that you can debug them in VS. Every time the VS web server starts up, it gets a new random port number, which you then have to update in your WCF client endpoint.

    Luckily the internal web server is configurable. With your web projects there is a property under the project properties - Web section:

    webproject_properties

    By default, the "Use Visual Studio Development Server" option is checked, and "Auto-assign Port" is selected. However you can change this to "Specific Port" as I have in the above example. You can also configure the web server further to pretend to use a virtual directory. This is great because it allows you to simulate production environments by specifying the same combination of ports and virtual directories as would exist on the production server.

    Of course, you can save yourself the hassle of having multiple web servers running at once by just selecting "Use IIS Web Server" instead, however this isn't ideal in environments where multiple people work on a project and could potentially have different workstation setups.

    Further Resources
    ASP.NET and IIS Configuration
    Build Web Server Solutions with End-To-End Extensibility
    Web Servers in Visual Web Developer
    Troubleshooting the ASP.NET Development Server

    07 March

    Notification Services : Debug Tips and Tricks

    Sql Server Notification Services can be tricky to debug. You have no code that you can put a breakpoint in (except for your managed api calls of course) and there's a lot of working parts inside. Here's a couple of tips that have helped me today dealing with a local and alpha problem.

    Rule Testing
    After you upload your instance and the application is created, a view is created for your event, subscription, and notification schemas. This means that the Sql you define in your subscription rule can be run against the table. I tend to remove the "INSERT" part of the rule, and just execute the select to make sure that my event is getting picked up and associated with a subscriber.

    Even one step lower, you can just view the contents of your events schema (view) to make sure the event was posted there correctly, and view your subscriptions schema (view) to ensure that the correct subscription information is in there.

    Enable Distributor Logging
    In your Application XML there is a node for settings called <ApplicationExecutionSettings>. You can use this to turn on 3 layers of debugging in your Notification Services application as follows:

    <DistributorLogging>
        <LogBeforeDeliveryAttempts>true</LogBeforeDeliveryAttempts>
        <LogStatusInfo>true</LogStatusInfo>
        <LogNotificationText>true</LogNotificationText>
    </DistributorLogging>

    Obviously this will add some overhead to your processing, but if you don't have high through-put then you could leave this on all the time. But for initial setups (such as on your local machine, or on production databases) it is invaluable.

    As the name suggests, the logging occurs around distributions of notifications. Your event should get stored in the relevant table after it is submitted, and your rule should process the events and post them to the notifications table. At this point, the distributor logging kicks in. When the service 'ticks' and processes your notifications for delivery, information is logged in a view matching your notifications view. For example, if your notifications class name is "BlogUpdates" then there will be a view automatically created called 'NSBlogUpdatesNotificationDistribution'. You can monitor this view for changes while you post an event that should trigger a distribution. Keep refreshing the view and you will see a new entry appear for a desired notification delivery. It details what delivery channel to use, the user, the content being delivered, and a status of the delivery. Eventually this will change to "Delivery Succeeded". If not, you can find information inline to assist in debugging.

    Windows Event Log
    Some problems are not specific to Notification Services. For example, your distributor account might not have read access to the XSLT needed to format the content for delivery. In these cases, information is posted to the event log.

    Email Notifications
    A final tip relates specifically to email notifications. I have noticed that if the SMTP server rejects the service sending the distributions (for whatever reason) you will not receive any information to the affect. Nothing appears in the event log, and nothing in the distributor log view. If you suspect this is your issue, then I can only suggest installing an SMTP relay locally and pointing your delivery channel to that server instead. At least you can get more information in your SMTP logs this way.

    More Information On Notification Services
    My 4 part series for beginners:
    Notification Services - Part 1 - Technology Overview
    Notification Services - Part 2 - Example Overview and Instance XML
    Notification Services - Part 3 - Application XML
    Notification Services - Part 4 - Managed API

    Notification Services tutorial: http://msdn2.microsoft.com/en-au/library/ms170337.aspx
    MSDN help: http://msdn2.microsoft.com/en-au/library/ms172483.aspx
    API reference: http://msdn2.microsoft.com/en-au/library/microsoft.sqlserver.notificationservices.aspx

    06 March

    Particls

    Presumably you subscribe to blogs. You might even subscribe to this blog. How many do you subscribe to? What about other feeds, like Engadget? Podcasts? What about sites you read manually, like your local newspaper site?

    Let's be honest, there's a lot of information on the web that we would like to be able to digest. 3 years ago, I would simply load up my browser, go to a url of a site, and read the articles. This is how I kept up with the technology industry and the goings-on. Unfortunately it was hard to sort through and get to the quality pieces of information that I really wanted to know about.

    Then I started subscribing to blogs. A few at first. People who were at the top of their game and were driving the technologies I was interested in. I tried some other tools (including Outlook) for periods at a time, and eventually came across Google Reader. By now my list of feeds had grown.

    Content Is King

    I was following the big corporate bloggers as well as the little guy down the street. I was more interested in the content of the post than the poster themselves. In a move that would shock the world, some of the big guys on campus were scratched from my feed list. I spent a fair amount of time customising my Reader with the information I really wanted to receive. I still had a dilemna though: what about those posters who provide quality sometimes, boring crap other times? Well I guess I just had to skip those entries, 'mark as read', although sometimes I didn't know the post was crap and not related until I had spent time reading the first few lines or summary.

    Enter Particls

    I can't remember how I first came across Particls. But for me it was a smash hit instantly. The essence of Particls is about attention management. From their About page:

    The web is just too big. No one has time to keep track of all the sites and topics that interest them.

    You see, every person, site, feed has something interesting to say at some point. Why not have a system that can rate and prioritise the information for you so that you only get presented with information you are specifically interested in?

    Particls1
    For example, with Particls I can specify all those blog feeds I follow in Reader (imported as OPML of course) and all those feeds I previously removed. Then I can prioritise them individually: which ones are more important to me on the whole? Further, I can specify key words that I am interested in, and keywords that I am not interested in.

    Particls2Particls will present the inf ormation to me based on relevancy. By spending a certain amount of time up front defining the kinds of information I want to hear about, Particls can continue to deliver information that I specifically want to read.

    Furthermore, you can tell Particls to search for information. This means even if you don't have any blogs you subscribe to, you can still specify keywords of interest, and let Particls do the rest.

    The information can be delivered to you in a number of ways. If you must, you can get the annoying popups around the system tray area. You can get a news ticker across the top, or a nice sidebar (my personal choice). You can use multiple options if you like: tell your news ticker to display only the most relevant and important information, tell your sidebar to disappear when not in use and show less relevant information. Particls can even provide an RSS feed for you to consume in another reader if you so desire.

    desktop

    On my wide screen it works great with Vista sidebar. If you get a notification that is inaccurate, you can simply right click and tell it to give less information about this topic. In this way, Particls quickly learns what your priorities are in order to deliver you more relevant information.

    Oh, did I mention Particls is free? Enough reason to go and give it a shot.
    Even better, it is produced by a local Brisbane (Australia) company called Faraday Media.

    Currently the download link on the site is disabled. I believe this is because they might be moving from beta to production (can anyone shed light on this?). I'll let you know as soon as I hear something.

    Update: I've tried to contact Chris Saad from Faraday Media about this a few times this week but didn't want to delay the article any longer waiting for a reply.

    Technorati Tags: ,