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. Continue reading

Process Snapshot

Yesterday I ended up running an impromptu browser test, comparing memory utilization. See what Twitter can do to your time!! The browsers themselves are really irrelevant. What you might find useful is the little PowerShell code I put together to periodically check and compare the browser processes. Continue reading

Scripting Games 2011 Beginner Event 5 Commentary

My commentary for Beginner Event 5 in the 2011 Scripting Games is now available. One item that seems to be missing on the ScriptingGuys site is my complete solution so I thought I would share it here, plus a variation. Continue reading

Get Common Data

While judging entries in this year’s Scripting Games I realized there were some common properties that were repeatedly used. This got me thinking about a simple way to retrieve that information with a single command Then you could access the data values from within your script. I’ve put together a relatively simply function called Get-Common that you might find helpful.

There’s nothing fancy about this function.

I gathers a number of what I think of as common values such as username, computername, PowerShell host and whether the user is running as an administrator. The function writes a custom object to the pipeline, using New-Object.

Assuming you have dot sourced the function, you could use it in your script like this:

Maybe this will help simplify your scripts or maybe it won’t. Of course you can easily extend it to include other core or common data you use frequently in your scripts. If there’s something you add, I’m curious to learn what it is.

Download Get-Common.

Get File Hash

The other day I had the need to calculate MD5 file hashes in order to compare files. The PowerShell Community Extensions has a nifty cmdlet, Get-Hash, that does exactly that. Unfortunately, sometimes even free tools like this aren’t an option, as in the case of my client. Or they don’t go far enough. So with a little work I wrote my own function to compute a file hash using either MD5, SHA1, or SHA256. Continue reading