Tag Archives: new-object

Friday Fun: Job Announcer

announcer-blueLast week I came across some people having fun with the SAPI.SPVoice COM object. This is the object that lets your computer “speak”. If you’ve never played with this, it is very simple to do in PowerShell. The voice quality in Windows 7 and 8 is quite nice, as far as synthetic voices go.


$v = new-object -ComObject "SAPI.SPVoice"
$v.speak("Powershell rocks!")

This got me to wondering…what about using this as a job announcer? Instead of having to go back periodically to check the status of a background job, why not have the job notify me when it is finished? All I would need to do is add a few lines of code to the scriptblock to create the voice object and announce when the job is complete. Here’s a sample.


#define a scriptblock for the job
$sb = {
Param($jobname)
#find all errors and warning in the system eventlog since midnight yesterday
Get-Eventlog -LogName System -EntryType Error,Warning -After (Get-Date).AddHours(-24).Date
#notify that the job has finished
(New-Object -ComObject SAPI.SPVoice).Speak("Thank you for your patience. Your job $jobname has finished.") | Out-Null
}
#define a name for the job that can be passed to the scriptblock
$jobname = "Get Latest Eventlogs"
Start-job -Name $jobname -ScriptBlock $sb -ArgumentList $jobname

The scriptblock could simply announce that the job is finished. If I only have a single job, that could be fine. But I wanted the scriptblock to be “aware” of what job it was running so I could have a more meaningful announcement. Thus, I pass the job name to the scriptblock so the voice object can use it in the announcement. It probably wouldn’t be too difficult to build a wrapper function to incorporate the voice announcement with the scriptblock. But I’ll leave that for you. If you come up with something, I hope you’ll share it and let me know.


$v.speak("Enjoy and Happy New Year")

Ping IP Range

Last week I came across a post on using PowerShell, or more specifically a .NET Framework class, to ping a range of computers in an IP subnet. The original post by Thomas Maurer is here. I added a comment. And after looking at this again I decided to take the ball and run with it a bit further. I’m a big proponent of PowerShell tools that are object oriented and that can add real value to the pipeline. To that end I wrote Test-Subnet. Continue reading

Convert Text to Object

Today I have another tool in my new battle regarding turning command line tools into PowerShell tools. The bottom line is we want to have objects written to the pipeline. At the PowerShell Deep Dive in Frankfurt there was a suggestion about providing tools to help with the transformation from CLI to PowerShell and this is one of the items I came up with.

Many command line tools write output in a structured format. For example run this command:

There’s an “object” staring at us for each task. As an aside, don’t focus on the command line tool I’m using, pay attention to the technique. To turn output like this into ah object we simply need to split each line at the colon. The first index of the array will be the “property” name and the second index is the value. Here’s the function I pulled together.

The function processes each incoming line of text and splits it on the delimiter. The default is the colon since many CLI tools seem to use it.

Since I prefer property names without spaces I remove them from index 0 of the array. I’ve also found it helpful to trim everything to eliminate extra white space.

Each line is then added to a hash table.

When the hash table is complete it is used to create a new object.

The other “trick” I added is to group lines into an object. This let’s me take say every 5 lines of text and make that a single object. I create a counter and increment it whenever a line is processed. When the counter meets the limit, the object is created, the hash table is cleared and the process repeats.

Now I can run a command like this:

The output looks similar but now I have objects so I can take advantage of PowerShell. Here are some examples:

This is by no means perfect but I’d like to think it is a good start. For example, one downside is that all properties are strings where it would be nicer to have properties of an appropriate type. Still, I hope you’ll try this out and let me know what you think.

Download Convert-TexttoObject

Update
A newer version of the script can be found at http://jdhitsolutions.com/blog/2012/01/convert-text-to-object-updated/