Monthly Archives: September 2010

vbScript – So just how hard was it the first time?

Today I had to provide a simple solution.  Create a formatted text file from two inputs from a user.

There was no time to have Development build an application and it literally WAS a simple app.

Get two variables, write them to a formatted text file.

So Why NOT do it in Powershell?

In all honesty, Powershell would have been easier but I needed something that could be converted to ASP.NET later on (if so desired).  So I played with vbScript for my first time.  I mean REALLY wrote something in vbScript.

First off, I think my code was poorly written and could have been easily optimized.   But it wasn’t hard.  It was a little different from the Syntax of Powershell but not horrid to deal with.   I did learn however (after having sat down and done it) just why I prefer Powershell.  That lack of need to Predefine every variable was one.

Perhaps I’ll get into some functions next time with vbScript.   Really, both languages support many of the same mechanisms.   Maybe if I optimize my vbScript code it will feel a little more like Powershell.   

But it was nice to code in another language and not find it so horribly different.  

Flex your creative muscles, you’ll be surprised what you can do.

Sean – The Energized Tech

FacebookTwitterGoogle+Share

Powershell – the Highway to Powershell- Almost 3 years after

Powershell

A fated day in 2008, I wrote a single Blog post about Powershell called “Highway to Powershell”.  It was simple thing.  A fun song I wrote.   Something I did in a time when I was known as a “Friday Funny Guy”.

A simple post that sent me down a path.

Powershell.  Not just the technology but a community of some of the most interesting, intelligent and passionate people I have ever met.

It started with me looking at the stats for the post, which on a good day would have been 10 views a day.  As you can tell I had a quiet little blog.  That day, things got a little “Scary”

500 views in one days.  600 more after that.  I tracked back the posts and found some fellow name “Jeffrey Snover” had viewed, commented and posted it to some “obscure blog” on http://blogs.msdn.com/powershell

“Can’t wait for the Youtube video” was one of the comments from him.

It spawned into my first ever attempt at singing and Youtube and Videos.   The lyrics carried forward into this new and unknown community of people of Powershell Enthusiasts and experts.   The Lyrics became used in at least two huge Microsoft events, Energize IT 2008 and Heroes Happen Here in Norway, the latter performed on stage by Ragnar Harper of Crayon.

“What have I done?” I kept thinking.  A simple song.   Why would it affect anybody?

Then I began to need to use Powershell, and used it, discovering why people would be so passionate about it’s use.  A Unified Management Scripting solution across various Microsoft and 3rd party vendor platforms.  A solution that could be as simple as a Network Administrator would need but had the unlimited power to be built upon and leveraged by Developers.   A technology that would still retain the ability to leverage existing scripting solutions from vbScript and CMD.

I had stepped into the world of Powershell, a world that helped ITProfessional and Developer find a common ground.

And so today, I release back to the Community I have come to love, a final release of “Highway to Powershell” with the very music Ragnar did on stage combined with a video holding tribute to the Community, the MVP’s and actually using Powershell to sing and even play a guitar.   I will post that script in a later post.

I <3 Powershell and the Powershell Community

By the way, as after note.  the Three characters in the picture below you?

Myself (on the left)

Dmitry Sotnikov from Quest (Rear)

Ragnar Harper – Kick ass guy and passionate about technology from Crayon in Norway.

Three guys and now friends, from different lifestyles and different countries unified by one technology

POWERSHELL

FacebookTwitterGoogle+Share

Techdays 2010 – Edmonton Alberta HERE I COME!

techdays-2010

Bonus of the week!  In LESS than 13 DAYS Techdays 2010 for the first time ever will be held in Edmonton, Alberta!

Edmonton!  Home of the kick butt Edmonton Oilers, killer Stanley Cup champions and home to Champion IT Professionals!

I get to present not one but TWO sessions and I hope to see YOU there!

I not only Excited but ENERGIZED about Techdays 2010 in Edmonton !

If you haven’t registered yet?  Don’t miss this opportunity to be at the first edition in Edmonton of the GREATEST Event in Canada!

Techdays 2010.  I’m honoured to be a part of the first of it’s kind in Edmonton.

Hope to see YOU there and change your day for the better !

Sean
The Energized Tech

FacebookTwitterGoogle+Share

Techdays 2010 – Session DPL 231 – “Automating your Office 2010 Install”

techdays-2010

Are you interested in finding a FASTER and more efficient way of deploying Office 2010?

Check out this Turbo Talk being held at all the Techdays 2010 events across Canada.   Find out quickly and efficiently how YOU can deploy Office 2010.   Automated deployments work on Open License, Retail and OEM.   See the experts and learn how in clear terms.

Just Register for Session DPL231 – Automating your Office 2010 Deployment and immediately walk away with techniques you can deploy in the field and get ahead of your competition.

Get your brains gassed up, kick in the Turbo, Register at www.techdays.ca and learn all you can learn.

FacebookTwitterGoogle+Share

Techdays 2010 – Session DPL 301 – “Application Compatibility Tools for Free”

techdays-2010

What do you envision for deployment?  Do you picture problems and uncontrollable issues?  Legacy documents that won’t convert?

Check out Session DPL 301 at www.techdays.ca to learn how to control and mitigate those issues.   Free Application Compatibility tools from Microsoft you can use now in a safe and non intrusive manner to not only identify but control, and in some cases eliminate your issues.

Don’t fear “The Morning After” – Check out Session DPL 301 and learn how to easily control all the issues and look forward to deployment.

If you’re lucky, you may even get to see a long lost friend as well during the session

FacebookTwitterGoogle+Share

Powershell – Function to Strip Blank Lines from Output

Powershell

I caught this one in Twitter yesterday.

“Is there a way to remove Blank Lines from the output?”

Top of my head I could think of an –EXCLUDE option for blanks but it isn’t hard to identify and remove blank lines from Text content.

So today’s task class is we’re going to use Powershell to erase Nothing.

“Huh?!?”

Well actually what we’re going to do is get rid of Blank lines from text files.

It’s pretty easy really.

When you read in the Content from GET-CONTENT it actually ends up as an Array of text,  meaning you can select and examine each line individually, including the number of characters.

So for the million Dollars, Dinner with Bill Gates, a signed autograph from Steve Ballmer with love *AND* a pat on the back from Paul Allen…… guess how many characters in a blank line?

None, 0, Zilch, Nada.  The CR/LF (Carriage Return line feed is used as a Delimiter and does not remain in the Array)

So when we do this

$FileContent=GET-CONTENT C:\SOMESILLYTEXTFILE.TXT

$FileContent is actually an array.  Running a GET-MEMBER against it like this

$FileContent | GET-MEMBER

Will show us several properties including one important one called Length.  Length tells us how many members in that array (or in human speak, “lines”)

To access individual pieces of the array I execute this piece of code

For the first line

$FileContent[0]

For the second line

$FileContent[1]

For the third line

$FileContent[2]

Seeing a pattern emerge?  Arrays start counting at zero.   Knowing that is important because although the Length of the array might be 30 lines, you’ll stop at 29 for the last one (since you start at 0 not 1)

On any one of those Array members you can also run a GET-MEMBER like this

$FileContent[1] | GET-MEMBER

andf view its available Properties.  Each line ALSO has a Length property.   This is how many characters in a line.   So to identify blank lines we simply look for Members which have a zero length.

IF ($FileContent[1].Length –eq 0) { WRITE-HOST “Blank Line”}

But in our case we’re going to skip those lines and build a new array of text.  Easily done, we’ll create a variable called $NewArray typed as an [Array]

So we get the Size of the FileContent array first and bump back by 1 (Since our last position of the Array to be access is the Length of the Array –1, we started at 0 not 1 remember?)

$EndOfArray=($FileContent.Length)-1

Then we setup a new Blank array to store the data in

[Array]$NewArray=$NULL

Finally we run through the list with a Foreach loop, checking and returning lines that AREN’T blank, storing the built result into $NewArray.

FOREACH ($Position in 1..$End) {
     IF ($FileContent[1].Length –ne 0) { [Array]$NewArray+=$Filecontent[$Position])
}

Now as a script we would have something like this

——————————

$FileContent=GET-CONTENT C:\Somesillyfile.txt

$EndOfArray=($FileContent.Length)-1

[Array]$NewArray=$NULL

FOREACH ($Position in 1..$End) {
     IF ($FileContent[1].Length –ne 0) { [Array]$NewArray+=$Filecontent[$Position])
}

——————————-

And we would have a simple script to pull out the and display it without Blank lines.   Here’s the fun part, we want this useful.    We can make this a function to add to our modules and “Return” the results back to the user.   One easy change, watch the Before and After

——————————

Function Global:REMOVE-BLANK ($FileContent) {

     $EndOfArray=($FileContent.Length)-1

     [Array]$NewArray=$NULL

     FOREACH ($Position in 1..$End) {
          IF ($FileContent[1].Length –ne 0) { [Array]$NewArray+=$Filecontent[$Position])
     }
Return $NewArray
}

——————————-

 

There you go!  Once defined in your profile or modules you can execute this code

$MyStuff=GET-CONTENT C:\Somesillyfile.txt

$NoBlank=REMOVE-BLANK $MyStuff

You can now do whatever you what with your “Blank Free” data.

 

See?  That wasn’t so hard!  The Power of Shell is in YOU, now use it! :)

Sean
The Energized Tech

FacebookTwitterGoogle+Share