Tuesday, July 13, 2010

A Quiet Word to the Chinese Comment Spammer

Hi.  Thanks for all your comments, especially all those that contain URLs to adult sites. I appreciate how you follow up nearly every post to this blog with more comment spam. You will notice that none of the comments actually get published – that’s because I review every comment and am rejecting yours. I will continue to reject spam like this, so you might consider not wasting your, and my, time with comments that will never get published (not now and not ever).

[later]

Almost as predicted, you (婉婷) did indeed try to leave a spam comment message. :-( 

Monday, July 12, 2010

PowerShell Needs a New Approved Verb

I’ve been playing around a bit with the System.Speech namespace, in particular the System.Speech.Synthesis.SpeechSynthesizer class. This class allows you go get the Speech Synthesis engine speak for you. On my workstation, I have just one voice, called Anna. If you look over on my PSHScripts blog, there’s a script to get all of the voices installed on your system.

As you can see from the script, the class is pretty simple, although it’s one that PowerShell loads by default. Once you create a SpeechSynthesizer object, you can then get the installed voices as the script shows. As you can see, on my system, there’s only one loaded voice (Anna).  The Speech Synthesizer has another useful class – Speak (well two, the second being SpeakAsync). These methods enable the SpeechSynthesizer object to speak some text.

I’ve written a couple of scripts that will demonstrate these APIs, and I’ll publish these shortly. But in doing so, I realised that the PowerShell Approved Verb list needs a new verb: Speak, which mirrors the Speak method. If you look closely at the approved verb list, there’s no verbs relating to a voice modality, which with the benefit of hindsight, is unfortunate. For Version 3, I think a new verb is needed, which could be Speak (my favourite) or perhaps Say.

 

Friday, July 02, 2010

PowerShell and XML Element Attributes

I’ve been playing a bit this week with XML and PowerShell. As you no doubt know, PowerShell has first class XML support built in. To see more about that, see Tobias’s Ebook Chapter on XML and PowerShell. My task this week was to work with attributes that can appear inside an XML tag. I was using the .NET XML class System.XML.XMLElement and it’s various attribute related method.

An XML Element, as  noted in MSDN, is a node in a DOM (XML) document. These elements can have attributes which you can associate with the element. For example, consider the following XML element:

<book genre='novel' ISBN='1-861001-57-5'>
<title>Pride And Prejudice</title>
</book>"

Such an element would normally be part of a much larger collection (eg <books></books>), but for the purposes of playing with element attributes, you can load it and then treated as an XML document with elements (albeit not many). You can load this document like this (and yes, there are a  bunch more ways!)

$Doc = New-Object System.Xml.XmlDocument 
$Doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" + 
             "   <title>Pride And Prejudice</title>" + 
             "</book>"

In the XML document, the book element has two attributes, genre and ISBN. Each attribute has the simple format (in the XML) of <attribute name>=<attributevalue>.

Once you load the document, you can do things like:

  • Check whether an element has a particular named attribute
  • Get the value of an attribute
  • Remove an attribute
  • Set and attribute

To do this in PowerShell you would do something like this, e.g. to set an attribute:

$Root = $Doc.DocumentELement
$Root.SetAttribute("attributename","value")

In richer XML scripts the attributename and the value would be held in a variable (that you in turn might have obtained from another XML document).

I’ve written several sample scripts over on the PowerShell scripts blog, which re-implement a number of MSDN attribute handling C# samples:

  • Get-XMLAttribute.ps1 – this script loads the XML then checks to see if the element has an attribute and if so, the code prints out the value of the attribute.
  • Remove-XMLAttribute and Remove-XMLAttributeAt.ps1 – these scripts load the XML and then remove the attribute, but using different .NET methods (i.e. RemoveAttribute and RemoveAttributeAt). Using the Remove AttributeAt, where you specify the position of the attribute, and not the name, is potentially dangerous. I have the t-shirt on that one! 
  • Set-XMLAttribute.ps1 – this script as the name might imply, loads the XML and adds an attribute to the element.

Fun stuff!

Technorati Tags: ,,,

PowerShell and XML Element Attributes

I’ve been playing a bit this week with XML and PowerShell. As you no doubt know, PowerShell has first class XML support built in. To see more about that, see Tobias’s Ebook Chapter on XML and PowerShell. My task this week was to work with attributes that can appear inside an XML tag. I was using the .NET XML class System.XML.XMLElement and it’s various attribute related method.

As noted in MSDN, are a node in a DOM (XML) document. These elements can have attributes which you can associate with the element. For example, consider the following XML element:

<book genre='novel' ISBN='1-861001-57-5'>
<title>Pride And Prejudice</title>
</book>"

Such an element would normally be part of a much larger collection (et <books></books>), but for the purposes of playiing with element attributews, you can load it and then treated as an XML document with elements (albeit not many). You can load this document like this (and yes, there are a  bunch more ways!)

$Doc = New-Object System.Xml.XmlDocument 
$Doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" + 
             "   <title>Pride And Prejudice</title>" + 
             "</book>"

In the XML document, the book element has two attributes, genre and ISBN. Each attribute has the simple format (in the XML) of <attribute name>=<attributevalue>.

Once you load the document, you can do things like:

  • Check whether an element has a particular named attribute
  • Get the value of an attribute
  • Remove an attribute
  • Set and attribute

To do this in Powershell you would do something like this, eg to set an attribute:

$Root = $Doc.DocumentELement
$Root.SetAttribute("attributename","value")

In richer XML scripts the attributename and the value would be held in a variable (that you in turn might have obtained from another XML document).

I’ve written several sample scripts over on the PowerShell scripts blog, which re-implement a number of MSDN attribute handling C# samples:

  • Get-XMLAttribute.ps1 – this script loads the XML then checks to see if the element has an attribute and if so, the code prints out the value of the attribute.
  • Remove-XMLAttribute and Remove-XMLAttributeAt.ps1 – these scripts load the XML and then remove the attribute, but using different .NET methods (i.e. RemoveAttribute and RemoveAttributeAt). Using the Remove AttributeAt, where you specify the position of the attribute, and not the name, is potentially dangerous. I have the t-shirt on that one! 
  • Set-XMLAttribute.ps1 – this script as the name might imply, loads the XML and adds an attribute to the element.

Fun stuff!

Technorati Tags: ,,,