If only I were

Building Great Software

Posts Tagged ‘PowerShell

Twin Cities Code Camp IV

with 3 comments

Yesterday, I was in the Cities at the fourth Twin Cities Code Camp.  It really was a fantastic experience, likely the the best one yet (I presented at the first two code camps as well). From the presentations I attended I would say that the quality of the presentations was very high. They easily would rival what you get at a paid conference. I also met a bunch of people I had never seen in person which is really cool.

Some of the Iowa crowd that went up was Javier Lozano, Bryan Sampica and Greg Wilson. There were several other attendees from Bryan’s company as well. The Iowa presenters and attendees have grown significantly since the first one where I was the sole Iowan as far as I know.

Some interesting people I met/saw were D’Arcy Lussier, Neil Iverson (Inetium), Brandy Favilla (New Horizons), Robert Boedigheimer, Chris Williams (Magenic), Aaron Erickson (Magenic), Kent Tegels (DevelopMentor), Jeff Ferguson, Chris Johnson, Saviz Artang, John Thurow, Kirstin (Magenic), Nicole and Kristen (New Horizons) and Justin Chase.

My favorite session was Neil Iverson’s PowerShell for Developers.  It was a fast paced live demo that kept incrementally building.  Rarely have I been so engaged in a session. D’Arcy’s MVC vs ASP.Net talk was also really interesting.  We only had about 6 people in the session, so we went around the room and said where we were coming from in our ASP.Net development experience. Then D’Arcy showed us how he typically structures his webforms applications, and we peppered him with questions.  I learned a lot from the session.

My talk was the second of the day in the large seminar room so we actually had about 50 people in the session.  One thing that was cool was that D’Arcy Lussier did an intro talk right before mine, so I got to build off of what he did in the session before. Mine seemed to go pretty well. There were a lot of questions and interest in what MVC brings to web development in the Microsoft space.

Jason Bock did a great job again bringing this all together.  It’s a lot of work coordinating an event like this.

If you liked what you got at the Twin Cities Code Camp you’ll definitely want to check out the Iowa Code Camp.  We’ll be a little bit smaller, but have some top notch presenters, a great facility and will have great prizes as well.  The registration is right on the home page and is as simple as it gets.

Written by Chris Sutton

April 6, 2008 at 9:27 pm

PowerShell File Search

with 4 comments

If you are looking for a file or directory in your file system try something simple like this:

dir -recurse -filter *power* | sort name | ft directory, name

dir is an alias to the Get-ChildItem cmdlet – it does pretty much what it did in the DOS world (and more). The -recurse parameter looks in subdirectories as you might expect. -filter lets you pass in a file or directory name or pattern. sort name … well you can figure this out.  ft directory, name is an alias to the Format-Table cmdlet and it lets you decide what columns you want in the table that gets outputted.

Here is a slightly more powerful expression:

dir -recurse | ?{$_.name -match “^*.jpg|gif$”} | sort name | ft directory, name

With this expression you can do comparisons on specific fields. Here I’m doing a regex pattern match for all files that end with jpg or gif. ? is an alias to the Where-Object cmdlet. where is also an alias to Where-Object and might be more readable than ?.

Written by Chris Sutton

April 1, 2008 at 7:49 pm

Posted in Technology

Tagged with , ,

PowerShell Hex to Char

with 4 comments

Rob Conery posted a poem in hex on Friday and since I’m playing with PowerShell quite a bit, I decided to convert it back to readable English with some PowerShell.

First, I copied and pasted the hex characters into a text file and saved it as message.txt.

Next, I needed to load the file into a PowerShell variable so I could manipulate it. I’m issued the command in the same directory where message.txt is located.

$m = gc message.txt

Explanation: PowerShell variables are always prefixed with a $ symbol so in this case $m is going to hold the contents of message.txt.  gc is an alias to the Get-Content cmdlet which literally get the contents of a file.

Next, we build up the actual statement to convert the hex values to decimal and then decimal values to a characters.

$m.Split() | % {[Convert]::ToInt32($_,16)} | %{[Convert]::ToChar($_)}

.Net Type Explanation: $m is a string type at this point. You can verify it by issuing this statement – $m.GetType().FullName – and you will get System.String back. PowerShell is built on top of the .Net type system so you have access to all of the same objects and members as you do in C# or VB.Net.  $m.Split() is going to give back an array of strings.

Pipeline Explanation: You can’t go very far in PowerShell without encountering the pipeline. The statement above you read from left to right, and you use the | symbol to separate the statement into three distinct sections. The first section makes an array of strings from the original data.  The second section converts each hex value to an integer and the third section changes the decimal value to a readable character.  Each section of the pipeline gets processed and then the results of the current section get passed to the section to its right so it can do a new set of operations.

% Explanation: The % symbol is an alias to the ForEach-Object cmdlet. It is also aliased as foreach. % is usually going to be paired with a block {} that will be repeated for each item in a list/collection/array.

$_ Explanation: $_ is a variable reference to the current item in the foreach operation.

[Convert] Explanation: Square brackets around a word like [Convert] say that you are invoking a preexisting .Net type called Convert. If you were writing a C# or VB.Net app you could easily call Convert.ToInt32(“32”) and it would convert the string representation to an integer type.  So here, [Convert].ToInt32($_, 16) is an overload of the ToInt32 function that takes a string representation of a hex value and converts it to an integer.

:: Explanation: The :: operator lets you invoke a method or property on a .Net Type, hence we have [Convert]::ToInt32($_,16)

You have to read this poem vertically at this point, but you can do the work to fix that. I’m almost sure there are probably some ways to simplify this expression as well, so feel free to post comments on improvements you would make. This has become a much longer post than I originally intended, but before I sign off here is what the fully qualified expression would look like:

$m.Split() | ForEach-Object {[Convert]::ToInt32($_,16)} | ForEach-Object {[Convert]::ToChar($_)}

Written by Chris Sutton

March 30, 2008 at 2:36 pm

Posted in Technology

Tagged with , , , ,

PowerShell left hand rule

with one comment

PowerShell uses the “left hand” rule when deciding how to interpret operators. Meaning that the type of the value on the left side of the operator is dominant. Here are some examples:

2+2  (gives 4)

2+”2″  (gives 4)

“2” + 2  (gives “22”, string data type wins out using + for concatenation)

“2” + 5  (gives “25”, concatenation again)

To have a little more fun try some multiplication:

3 * 3 (gives 9, ok this is still boring)

4 * “5”  (gives 20, the “5” becomes an integer)

“3” * 4 (gives “3333”, that’s more interesting, “3” means the string data type is the primary type, this acts much like Ruby)

Here is a bit more that might be interesting:

15,30,45 + 3  (gives a new list of 15,30,45,5)

15,30,45 * 3  (gives a new list of 15,30,45,15,30,45,15,30,45)

Kind of cool how this works.  The DOS prompt was stone-age comparatively.

Written by Chris Sutton

March 27, 2008 at 6:31 pm

Posted in Learning, Technology, Thoughts

Tagged with , , ,

PowerShell del

with 2 comments

del is an alias to the Remove-Item cmdlet and you can use it pretty much like you did in a DOS shell. But there are some additions.  It can delete more than just files and directories. It can now delete certificates, registry keys, aliases and more.

One of the nicest features is that you can issue a del command with the -whatif parameter and it won’t run the command, but will tell you what would be deleted if you really did issue the command. So del *.zip -whatif would give you a list of .zip files in your current directory, but wouldn’t actually delete them.

If you haven’t tried PowerShell yet and you work from the command line a lot, you really should check it out.  It is a pretty easy transition from cmd.exe, but there is way more power under the hood if you want to dig deeper.

I’ll likely blog more on PowerShell in the future and will link to many of the resources out on the web to help you learn how to use it.

Written by Chris Sutton

March 26, 2008 at 7:39 am

Posted in Learning, Technology

Tagged with , , ,