February 2011 Archives

I awoke this morning with a start.  4:30am without an alarm clock I bolted upright.   So sudden I almost fell out of bed backwards as my roommate Cory watched with a sudden chuckle. 

That stark unstoppable realization rapping away at me.

I’m in Seattle.  Seattle, Washington.   I’m a very stone’s throw away from “the Mothership” as it is nicknamed.  Microsoft’s Head Office.

I would suspect there is a lot I can’t talk about.  For that I won’t breach any confidence.  But I can at least type and express what I am feeling.

I am feeling elated, inspired, and more Energized than I ever have in my entire life.   The mere thought of stepping through the gates of Redmond and just meeting the people and technology has put my brain into a spin I am having a very difficult time describing. 

It’s what has had me wide awake every single day at 4:30am, bolting upright without the alarm clock.   The energy of Redmond, Washington seems to be calling me.

Others smile and might think “Silly Sean” or may nod knowingly as it has the same effect on them.  I don’t know, but I can only say what it means to me.

It’s a dream come true.

Several years ago I decided it was my one goal to work for Microsoft.  In striving to reach that goal I began to change, and almost evolve.   Evolve seems to be the most accurate word.   What started that?  Microsoft.

I met the people I looked up to as heroes back in 2007 at a conference called Energized IT 2007.    One of these people looked over at me and said “Oh my God you’re just like us!”

Part of me almost fainted that day, but another part lit a spark I had never felt before.   It brought forth confidence I had never known.   I decided to be creative and start to make mistakes, write and sing songs (Yes, I admit it.  I was a former Friday Funny Guy and I will never dread that) and just blog, not worrying about whether what I did was good enough for anybody else.

That courage led to writing a Theme Song called “Thundergize” for Energize IT 2008, becoming a speaker at Techdays 2009, a Session Lead at Techdays 2010, and more that’s about to happen.

It led to me getting involved in the Windows Powershell community.  Engaging people that, prior to all this I marked as “the Computer gods”.   I became more willing to open up and say things, sometimes be wrong but LEARN.   Yet here is the neat part.

I didn’t change.   In truth, this was always in me but I lacked the courage.  I just didn’t believe in myself.   Now parts of me that were always there are bubbling and surfacing to the top.  My Energy levels seem to rise the closer I get to Microsoft and Redmond, Washington.  It is drawing me like a magnet and I do not want to walk away.   I need the inspiration that is Redmond and will not look away from what it does to me.

This both amazes and frightens those that know me.  I am a very Energetic person when you get me going on Technology.    Friends will attest to that.   The term “Release the Kraken” was coined when that excitement about technology hits it’s peak.

Which has people I know a little worried.   When I stand on the grounds of Redmond, Washington; “The Mothership”, the very place where some of the greatest minds INCLUDING Bill Gates, Steve Ballmer and Paul Allen have stood.   They wonder “What will happen?”

Will I “Bow down at the gates and kiss the ground?”

Will I “Release the Kraken and lose myself emotionally?”

Will I just faint?

No.   I will evolve as a person on a new level.  I don’t even know what it is.   But I look forward to just standing there in Redmond.  Soaking in the inspiration.   ?Letting her mold and shape me to a new level.

I look forward to the next 5 days.

There is only one problem I foresee.   I may find myself not wanting to go home. Winking smile

Sean
The Energized Tech

dr_scripto_2011_Soul 50

It’s time to Unleash the Power of Shell.   Pull the Scripter from within yourself and stand up and show your stuff!

WHEN?

On April 4th to April 15th, the 2011 Scripting Games are coming your way with all the uncontainable excitement they hold.

WHO?

Do you think yourself a Scripter? STEP up to the plate!  But there’s also stuff equally as fun for the Novice at heart as well!

The Scripting Games are just as much about Enjoying yourself as they are in testing your skills.

WHERE?

Read up more about them on “The Scripting Games Study Guide” , Follow Poshcode.org and “Hey Scripting Guy” for more details.   Or if you’re on Twitter watch for the Hashtag #2011SG for details!

In the meantime, let a little inspiration burn at your fingertips from a little video inspired from the Scripting community Smile

The 2011 Scripting– Powershell Unleashed

powershell

An interesting question came in Friday during the Webcast on Technet Talk Radio.   What I thought was “Huh, yeah you SHOULD be able to pull that off in Windows…”

“Is there a way in Powershell to close a file handle?”

I decided this morning, that was bugging me.   So I started digging into the Developer Land on MSDN to try and learn something.  That made my brain hurt a bit.   After a did a quick search on Bing for the obvious.  Maybe somebody had already built a tool to do this.

Wouldn’t it make sense the coolest bunch of Geeks at Sysinternals would have done this already?  YUP!

The tool is called “HANDLE” and you need to run it as an Administrator (So I imagine running it as SYSTEM should have the same rights if I had to schedule it)

HANDLE works very simply.  Key in HANDLE and the file you’re trying to close, it will yield a pile of info including the Process ID calling it.   It will also output the Hexadecimal ID the File is associated with.

Download it and run it at least once beforehand (To accept the EULA) and you’re good to play

Here’s the catch.  Standard Administrator?  Perfect.   I’m closing a handle, I manually run it.  But today we’d like to automate it. 

So our FIRST challenge is getting the output of HANDLE into Something Powershell will use.  That’s easy, LITERALLY grab the Console output.

$DATA=(& “C:\PathToHandle\Handle.exe” filesomedummyleftopen)

So now we have a Big pile of Data stored in conveniently a variable called $DATA.  Fortunately for us our good friends at Sysinternals formatted the output of HANDLE into CONSISTENT columns.

So the first 5 rows in our array we can ignore.   That’s the header from the output in HANDLE.EXE.   How do you tell? You can step through it Piece by piece like so

$DATA[0]

 

$DATA[1]

Handle 3.45

$DATA[2]

Copyright (C) 1997-2011 Mark Russinovich

$DATA[3]

Sysinternals - www.sysinternals.com

$DATA[4]

 

See? Now our FIFTH line is where the goodies start showing up.  It appears a “Certain ItPro” forgot to close off a Word document.

$DATA[5]

WINWORD.EXE        pid: 7912   type: File           7D0: C:\Users\sean.kearney\Documents\Sample.docx

*cough* oops.

The nice part with this output is we only need TWO pieces of info, the PID (Process ID) and the HEXID for the file handle.    The nicer part is since all of this is CONSISTENT in it’s formatting.

I found the PID Number always starts at column 24 and is never more than 7 bytes long.  

The HEXID could be more than what I see here (3 bytes) but the area IT takes up is ALWAYS the spot before the file name and ALWAYS after the word “File”.   Turns out THAT is starting at Column 41 and never more than 14 Characters.

How did I find this out? I’ll be honest.  I cheated.  I kept doing this and changing numbers

$DATA[5].substring(10)

$DATA[5].substring(15)

Which shows you all information from position 10 down or position 20 down.  After I went in and keyed in

$DATA[5].substring(24,1)

$DATA[5].substring(24,4)

Which shows you all the information from position 24 down and then only 1 character or 4 characters.   It's not “Geeky cool” but for a new Powershell Admin, knowing how you could navigate the data can help.

So what does that mean to us?  Consistency and a pattern we can work with.

$MYPID=$DATA[5].Substring(24,7)

$HEX=$DATA[5].Substring(41,14)

So great I HAVE the information and I’ll just pass that right back to HANDLE.EXE to close the file handle.

But wait.  We forgot something.  There might be blank space in that text.   We want JUST the information, no blanks to screw us up with Delimiters! *ARGH!* Sean !  You lied to us!  You said this was going to be easy!

Well, that’s doable to.  Just a little more trickery in Powershell.   We can use what is known as the “Trim” method on a string to “Trim” the blank spaces off either side

$MYPID=($DATA[5].Substring(24,7)).trim()

$HEX=($DATA[5].Substring(41,14)).trim()

So now ARMED with this information we can just call HANDLE.EXE from Powershell.exe and

(& “C:\PathToHandle\Handle.exe” –c $HEX –p $MYPID -y)

So how is this efficient?   If you play with Handle it can do “wildcarding” on Paths and files.   If you have a particular file structure on a FILE server that users are forever leaving Excel documents open?  Backup can’t get to it?  You can NOW use Powershell AND Systinternals together to create an even more powerful tool.

You’ll also note that I only referenced Element [5] in the array.  If I wished to step up one more level and have ALL of the files revealed by HANDLE.EXE closed, I can just do this. (But be careful on your targeting!)

# Get the console output of HANDLE.EXE from Sysinternals into a Powershell Variable

$DATA=(& “C:\PathToHandle\Handle.exe” FolderFullOfStuffLeftOpenByTheSillyUsers)

# Get the count of lines in the output
$Count=($INFO.Count)-1

# Remember, the good stuff starts at line 5
Foreach ($line in  5..$Count)
{
      # Get the Process Id for each file
      #
      $MYPID=($DATA[$Count].Substring(24,7)).trim()
      # Get the Hexadecimal ID for each open file
      #
     
     
$HEX=($DATA[$Count].Substring(41,14)).trim()
      # Close that file!      
      #
      (& “C:\PathToHandle\Handle.exe” –c $HEX –p $MYPID -y)
}

 

This is why I love Powershell.  I do NOT need to throw away perfectly good tools just to use Powershell.  I can leverage BOTH, like the amazing Console based tools from Sysinternals, at the same time and sometimes come up with solutions I never could easily conceive of before.

Remember, the Power of Shell is in YOU

Sean
the Energized Tech

powershell

I can just imagine now.  Every English teacher I ever had.  Their eyes are rolling up within their heads like a Shark.

“DOSsy? DOSsy?  You made up a word called DOSsy?”

Sounds like somebody’s name.   Of course it isn’t but today I’m going to show you how to have Powershell pull down information from Dos as an Object that Powershell can work with.

Let’s try something simple.   There are Environment variables within Dos.  PATH, USERNAME and others.

If you need to access ANY of those variables in Powershell just tack on $ENV: before the name

$ENV:PATH

$ENV:USERNAME

There you go.   All that information is easily accessible.

But wait.  Did you say there is Console output from an older application you’d like to work with in Windows Powershell?  Not a headache either.  Generally, if it echoes to the Console you can grab it in Powershell.

So let’s try something that could be useful.   A list of all the variables in Dos.   Normally in DOS I would just key in

SET

and afterwards a nice list would appear on my screen.  Wonderful.  But unless I want to “Copy Paste” that information (not very useful) It’s just sitting there in DOS land.

But JUST LIKE Powershell, you can pass parameters to CMD.EXE.   Just key in CMD.EXE /? in any shell (Dos or Powershell) to see the parameters.  The “C” (Command) parameter is the one we’re interested in.

So If I want to pass the “SET” command to Dos as a scripted function I would execute

CMD.EXE /C SET

Now would you like to work with that information?  You can.  Pipe that through any Powershell Cmdlet, say “FOREACH” to step through the information

CMD.EXE /C SET | FOREACH { WRITE-HOST This Value is $_ }

Neat eh?  But we can even get fancy and Pull the Values and Variables apart into a cleaner list.   There is a "split” method in Dotnet (You can see it if you PIPE it through GET-MEMBER)

CMD.EXE /C SET | GET-MEMBER

With Split you can pull of a really neat trick Dos can’t easily do.  You can “SPLIT” lines of text into new lines by giving it a Delimeter.   A Character that says “The line stops here".  So if I want to split my Variables and Values in Dos, I’ll use the Equals sign as my Delimeter since THAT is the part that separates them

CMD.EXE /C SET | FOREACH { $_.split(“=”) }

Now for the cool part.   When I split this ONE object, each piece of that split piece can now be referenced INDIVIDUALLY.   In my case, I’d like to look at just the FIRST piece (which will always be the variable) in each line.  I can do that easily by specifying it as an array.  Since the first member of an array ALWAYS starts at Zero (0)

CMD.EXE /C SET | FOREACH { $_.split(“=”)[0] }

Cool, you now have a list of the current variables accessible.   Silly?  I think not.   It’s not difficult to leverage this SAME method to grab Content from other Commands or output in CMD.EXE   This is just a simple example.

Remember, the Power of Shell is in YOU

Sean
The Energized Tech

TechNet Webcast: Talk TechNet with Keith Combs and Matt Hester – Episode 6: Sean Kearney on PowerShell (Level 200)

Event ID: 1032476756

Language(s): English.

Product(s): Other.

Audience(s): IT Decision Maker, IT Generalist.

Event Overview

Talk TechNet is all about discussing topics and trends in the world of IT Professionals.  In this show we’ll have guest Sean Kearney. Sean is a Microsoft PowerShell MVP and SME on virtualization. Sean is an Infrastructure Support Analyst and has been a speaker at the popular Microsoft Canada Tech Days. Call in and join us for what promises to be a lively 60 minute session.  Get some burning questions answered.
Presenters: Keith Combs, Senior Program Manager, Microsoft Corporation, Matt Hester, Senior IT Pro Evangelist, Microsoft Corporation, and Sean Kearney, Infrastructure Support Analyst, Microsoft PowerShell MVP

Live - February 11th 2011 on Technet Radio!