Brian Hartsock's Blog

Author Archive

I’m a Mac user

by bhartsock on Jun.28, 2010, under Uncategorized

I have officially switched my personal computer to a Mac (MacBook Pro to be specific). I’m sure some of you might be wondering, why did I switch to a Mac? Most of my blog is on .NET and Powershell related topics, which aren’t so prevalent on a Mac. Well, here are a few of the main reasons.

  • Promoters – Mac owners are promoters. When I asked the Twitterverse what I should do, all but one response was “Get a Mac!”. Maybe I am hopping on the bandwagon, but people love Macs for a reason.
  • I missed my Linux roots. If you go back far enough in my blog, you will see I used to be a PHP developer whose work, home, and school computers were running Ubuntu.
  • Windows – I can still run Windows on a VM and on Bootcamp.

So far, I am pretty darn happy with the Mac. There are some oddities, but overall it is a crisp, clean machine.

Now, enjoy the beauty that is my mac! (Ignore the Logitech nano dongle. I need my ergonomic mouse so give me a break)

Post to Twitter Post to Digg Post to Facebook Post to Reddit

Leave a Comment : more...

Ruby video for non-ruby programmers

by bhartsock on May.28, 2010, under Uncategorized

This video is a must watch for experienced developers trying to learn Ruby. It will explain the magic of metaprogramming.

Post to Twitter Post to Digg Post to Facebook Post to Reddit

Leave a Comment : more...

Transitioning from svn to git

by bhartsock on May.28, 2010, under Uncategorized

Great post on git commands and their subversion equivalent. Very concise compared to most articles.

(Sorry for all the links, but I have been catching up on Google Reader and finding some real gems)

Post to Twitter Post to Digg Post to Facebook Post to Reddit

1 Comment :, more...

A better Start-Job cmdlet

by bhartsock on May.22, 2010, under Uncategorized

With Powershell 2, the Start-Job command was added, which allows statements to be executed in the background. There are a few oddities regarding script blocks that need to be understood first though.

  • Powershell doesn’t support closures
  • The current working directory isn’t preserved

I confronted these issues first hand today while working with a Rails app. I wanted to execute ruby script\server in a job, but it wasn’t working.

PS C:\Users\Brian\workspace\myapp> start-job { ruby script\server }

Id              Name            State      HasMoreData     Location             Command
--              ----            -----      -----------     --------             -------
3               Job3            Running    True            localhost             ruby script\server

PS C:\Users\Brian\workspace\myapp> receive-job 3
C:\Ruby19\bin\ruby.exe: No such file or directory -- script/server (LoadError)
    + CategoryInfo          : NotSpecified: (C:\Ruby19\bin\r...ver (LoadError):String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

For some reason, when executing the job, it is executing in a different working directory. It is pretty easy to confirm.

PS C:\Users\Brian\workspace\myapp> start-job { get-location }

Id              Name            State      HasMoreData     Location             Command
--              ----            -----      -----------     --------             -------
3               Job3            Running    True            localhost             get-location

PS C:\Users\Brian\workspace\myapp> receive-job 3

Path
----
C:\Users\Brian\Documents

Knowing that the working directory was not preserved, I figured it would be easy enough to write my own function that added this functionality. This was my first stab at it.

 
function start-jobhere([scriptblock]$block){
  start-job -argumentlist (get-location),$block { set-location $args[0]; . $args[1] }
}

But, it didn’t work.

PS C:\Users\Brian\workspace\myapp> start-jobhere { ruby script\server }

Id              Name            State      HasMoreData     Location             Command
--              ----            -----      -----------     --------             -------
1               Job1            Running    True            localhost             set-location $args[0]...

PS C:\Users\Brian\workspace\myapp> receive-job 1
The term ' ruby script\server ' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    + CategoryInfo          : ObjectNotFound: ( ruby script\server :String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

I had a sneaking suspicsion that Powershell was serializing the scriptblock to a string, which I confirmed.

PS C:\Users\Brian\workspace\myapp> $sb = { "hello world "}
PS C:\Users\Brian\workspace\myapp> start-job -ArgumentList $sb { $args[0] | gm }

Id              Name            State      HasMoreData     Location             Command
--              ----            -----      -----------     --------             -------
15              Job15           Running    True            localhost             $args[0] | gm

PS C:\Users\Brian\workspace\myapp> receive-job 15

   TypeName: System.String

Name             MemberType            Definition
----             ----------            ----------
Clone            Method                System.Object Clone()
CompareTo        Method                int CompareTo(System.Object value), int CompareTo(string strB)
Contains         Method                bool Contains(string value)
CopyTo           Method                System.Void CopyTo(int sourceIndex, char[] destination, int destinationIndex,...
EndsWith         Method                bool EndsWith(string value), bool EndsWith(string value, System.StringCompari...
Equals           Method                bool Equals(System.Object obj), bool Equals(string value), bool Equals(string...
GetEnumerator    Method                System.CharEnumerator GetEnumerator()
GetHashCode      Method                int GetHashCode()
GetType          Method                type GetType()
GetTypeCode      Method                System.TypeCode GetTypeCode()
IndexOf          Method                int IndexOf(char value), int IndexOf(char value, int startIndex), int IndexOf...
IndexOfAny       Method                int IndexOfAny(char[] anyOf), int IndexOfAny(char[] anyOf, int startIndex), i...
Insert           Method                string Insert(int startIndex, string value)
IsNormalized     Method                bool IsNormalized(), bool IsNormalized(System.Text.NormalizationForm normaliz...
LastIndexOf      Method                int LastIndexOf(char value), int LastIndexOf(char value, int startIndex), int...
LastIndexOfAny   Method                int LastIndexOfAny(char[] anyOf), int LastIndexOfAny(char[] anyOf, int startI...
Normalize        Method                string Normalize(), string Normalize(System.Text.NormalizationForm normalizat...
PadLeft          Method                string PadLeft(int totalWidth), string PadLeft(int totalWidth, char paddingChar)
PadRight         Method                string PadRight(int totalWidth), string PadRight(int totalWidth, char padding...
Remove           Method                string Remove(int startIndex, int count), string Remove(int startIndex)
Replace          Method                string Replace(char oldChar, char newChar), string Replace(string oldValue, s...
Split            Method                string[] Split(Params char[] separator), string[] Split(char[] separator, int...
StartsWith       Method                bool StartsWith(string value), bool StartsWith(string value, System.StringCom...
Substring        Method                string Substring(int startIndex), string Substring(int startIndex, int length)
ToCharArray      Method                char[] ToCharArray(), char[] ToCharArray(int startIndex, int length)
ToLower          Method                string ToLower(), string ToLower(System.Globalization.CultureInfo culture)
ToLowerInvariant Method                string ToLowerInvariant()
ToString         Method                string ToString(), string ToString(System.IFormatProvider provider)
ToUpper          Method                string ToUpper(), string ToUpper(System.Globalization.CultureInfo culture)
ToUpperInvariant Method                string ToUpperInvariant()
Trim             Method                string Trim(Params char[] trimChars), string Trim()
TrimEnd          Method                string TrimEnd(Params char[] trimChars)
TrimStart        Method                string TrimStart(Params char[] trimChars)
Chars            ParameterizedProperty char Chars(int index) {get;}
Length           Property              System.Int32 Length {get;}

So, instead of invoking the scriptblock like your normally would, you need to call Invoke-Expression instead. My function ended up looking like the this.

function start-jobhere([scriptblock]$block){
  start-job -argumentlist (get-location),$block { set-location $args[0]; invoke-expression $args[1] }
}

Now it works like a charm!

Post to Twitter Post to Digg Post to Facebook Post to Reddit

2 Comments :, more...

Why the hell can’t I delete that folder?

by bhartsock on May.11, 2010, under Uncategorized

The other day I heard a coworker cussing about the fact he couldn’t delete a folder. To give him credit, he dual boots Linux and Windows, and has just recently realized Windows is much better. But, Windows doesn’t have sudo rm -f (ignoring chattr +i which I didn’t know about until today).

The problem is easy to solve though.

  • Search for the path of the folder, and you see the process/executable holding onto the file.

Post to Twitter Post to Digg Post to Facebook Post to Reddit

Leave a Comment : more...