jump to navigation

Lovin’ me some POSH, part 2 Thursday, 09/22/2011

Posted by Percy in Programming.
Tags: , ,
add a comment

So, I’m still riding the PowerShell train these days.  I’ve written a few more scripts, and I wanted to share.  Recently, I’ve written scripts…

  • …that audit all of our TFS build definition retention policies
  • …that update all of our TFS build definitions to have the standard retention policy
  • …that audits all of the builds for our TFS build definitions such that we can tell which builds are taking up the most space on our build machine and cut those down
  • …that locks down/unlocks all branches of a certain type for all of our clients, so that we can clean them up and get ready for branching without having rogue checkins (As an FYI, this one worked so good initially, that the branches weren’t visible to most people.  I’ve refined it since.)
  • …that will query multiple similar databases and will return the data for all the disparate result sets into one grid view (I know you can do this through SSMS, but it requires setting up registered servers and the query results aren’t sortable or filterable like the Out-GridView cmdlet output is)
  • …that will deploy our database scripts – somewhat complicated, Shared common functionality => Client-specific functionality, but scripts need to be run in a certain order, and they are not named in a standard format
  • … that will go through a directory of files recursively, determine the encoding of certain files and set the encoding on the files that aren’t set appropriately (xml config files not set to UTF8)

I’m still trying to find something that it can’t do, and I’ve yet to find it.  Most of the time, it takes less than an hour to write the script for something that would have taken me multiple days.  If anyone is interested in some code samples, let me know.   I doubt they follow “best practices”, since I’m only a few months into using this and I don’t know what the best practices are yet.  :)

I’ve also set up a profile that is synced with Dropbox.  So, all of my computers can have the same set of functions, if I want them to.  Too, if I make a change to one of the functions, it’s propagated automatically to all my computers.  Again, I’m not sure if I’m doing this the best way, but it’s working so far.

Too, I’ve got a work in progress – a script that will sync Outlook calendar items with Google calendar items.  I’ve got a script that will query both, but I’ve not yet tied them together.  The app I usually use for this is a bit buggy, doesn’t always work, and doesn’t sync regularly.  So, I’m replacing it…in my spare time…which means it will be done in 2020.  :)

So, have you done anything cool with POSH lately?

SQL Saturday, anyone going? Tuesday, 09/13/2011

Posted by Percy in Programming.
Tags: , , , ,
add a comment

I’m signed up for SQL Saturday this weekend – 9/17.  Anyone else going?

Here’s what I’m planning on attending:

09:00 AM – 10:00 AM Professional Dev Breaking out of your shell: Cube Dweller to Leader (Bobby Dimmick, Level: Beginner)
10:15 AM – 11:15 AM: Professional Dev Are you a Linchpin? Career management lessons… (Brian Moran, Level: Intermediate)
11:15 AM – 11:45 AM Lunch
11:45 AM – 12:45 PM Powershell ETL Smackdown with Julie Smith (Aaron Nelson, Level: Beginner)
01:00 PM – 02:00 PM Powershell Using StudioShell to Automate Visual Studio (Jim Christopher, Level: Beginner)
02:30 PM – 03:30 PM Powershell PowerShell: Are you checking out my profile? (Nicholas Cain, Level: Beginner)
03:45 PM – 04:45 PM Powershell Stupid PowerShell Tricks (Jim Christopher, Level: Beginner)

Evernote

I’m looking forward to seeing two co-workers of mine (Aaron Nelson and Julie Smith) duke it out in the ETL session.

To “this.” or not to “this.” – that is the question Tuesday, 09/06/2011

Posted by Percy in Programming, Visual Studio, Work.
Tags: ,
1 comment so far

In my profession there are a lot of ways to accomplish specific tasks. Some of the ways to do things are better in a quantifiable way than others. Then, there are certain things which, while they may be drastically different, have no impact on the end result (efficiency, maintainability, readability, etc.).

There are also people in my profession that cling to the way they do things, to the point of ridiculing those who do it differently. Now, while the ridicule part is inappropriate, I don’t have a problem with someone having a different opinion. However, if you can’t prove that whatever idea/process/standard you’re advocating falls into the first category – quantifiable improvement – then what you are pressing is your opinion, and the one I have is just as good as yours. Further, if I can prove that my opinion actually is an improvement in a quantifiable way, then, realistically, mine is better than yours. At that point, it’s not an ego thing, it’s a provable fact. You don’t have to come to my side of things, but you do have to concede that you’re way is deficient in some way. The same thing goes the other way. If you can prove your way is better than mine in a demonstrable way, then usually I’ll concede and adopt you’re ideas as my own.

Developers get very passionate about code style, and, truth be told, I’ve been known to belligerently uphold those opinions of mine. I’d like to think I’ve mellowed in my maturity, but I have my moments. One of those code style conventions is the use of “this” or “base” to access object properties or call methods of the current or parent object. This actually goes along with using the object name in references to static properties or methods. The prevailing and popular thought on this is to not use either one of these conventions. I believe I can show that actually using these conventions can make your code more readable, if only slightly. If there’s an argument against using these that trumps code readability in this manner, I’d certainly enjoy hearing about it. I’ve actually convinced some people who held the popular opinion that it’s actually better to do things differently.

Just so we’re clear, the idea of increasing code readability is the practice of pushing as much information into the actual writing of code as possible so that ancillary information (usually in the form of comments) becomes unnecessary. You try to write your code in such a way as the meaning of the code and the information you’re attempting to convey leaves as few questions as possible. The idea is to be as precise as possible so that when people come behind you (and if you’re code has any chance of surviving past your own dev cycle, people will) they don’t have to infer or assume anything. Now, where there’s a rule, there’s the use of it in moderation. There’s a swing of the pendulum that goes too far when you have method or property names that start to crack four digits in length. I don’t think what I’m proposing goes that far.

So, here’s my proof. Let’s take a simple, yet extremely common, class called Customer, that has over 200 lines of code in it. I think we can all agree that any main entity in an app will probably have more lines of code than this, but this will suffice for the sake of this discussion. The point is that we deal with classes that are larger than our “viewport” more often then not.

Now, let’s say, you’re coming in after someone to fix a problem with their code, and you see this:

Now, the error you’re getting is that there is a null reference exception at line 166. Let’s take a step further, and say that the “Code before” and “Code After” constitute enough code that you don’t even have the method prototype on the screen (I think the argument works without this, but let’s take it to the extreme). Ok, what’s the scope of “Reference” in this context? Is it static or local? Is it a property of Customer, or is it part of a completely different object called Reference? Where is this property defined? Is it in this method, the Customer class, some base class or in a completely separate class? Should you be checking the constructor to see if it’s set (local scope) or some other entity’s process (static scope)? Now, you can use all kind of IDE features to find out that information, but you have no idea just by looking at the code. There are some questions that you can’t answer by simply reading the code – i.e, the readability of this code is diminished.

Now, what if you saw this, instead -

Then, I could answer those questions simply by reading the code. Reference is a local variable for the object Customer. It’s defined somewhere in the Customer class (given that with partial classes, that might be across multiple files). The same holds true for using “base”, except that you know the property is defined in another class. In this case, the readability of the code is increased over the previous example.

Or, what if you saw this -

Again, you could answer the questions. Reference is a static property of the object Customer. It’s defined somewhere in the Customer class. And, again, the readability of the code is increased over the first example.

Yes, I know you can just hold control, and click on “Reference” to go to it’s definition, or right click and “Go To Definition”. I’m not saying you can’t answer the questions I posed at all without using “this”, “base” or the object for static properties. All I’m saying is that it’s easier to answer those questions if you do use them. So, to me, that makes the code more readable. In my opinion, better code readability is worth the extra few keystrokes you have to use to add this feature in the long run – especially in the Intellisense age of development.

I know that some people can be passionate about these kinds of things. I like using this convention, and I think it makes my code better in a way that I can quantify. So, I’m going to use this every chance I get until someone convinces me otherwise. If you disagree, so be it. I’m not going to hold that against you. Even if I see your code not using it, I won’t change it unless I really need to. The only time I might question you a bit farther on it is if you’re entire rationale is based on “because MS/ReSharper/Visual Studio/my boss/[insert other authority figure here] says so.” Why do they say so? I think that even if some authority on a subject makes a statement, they need to back it up with something or we need to find out the reason behind it – it may be a good one or not. “Trust, but verify”.

So, what do you think? Agree/disagree? If you disagree, why?

Lovin’ me some POSH… Thursday, 09/01/2011

Posted by Percy in Fun with Powershell, Technology.
Tags: ,
add a comment

POwerSHell, that is.  Apparently the PS acronym is overused, so the folks at MS decided that PowerShell should be called POSH.

I started going through a PowerShell primer a few months ago, and while it’s a good start, you don’t really learn something until you start applying it.  At least, that’s usually how it works for me.  At the time, I didn’t really understand it, and so I didn’t understand how to apply it.

Then, about a six weeks ago, I started working on a task for my current project.  Basically, we were trying to figure out an easy way to set up our enterprise application which is fairly complicated.  I started by setting up the web piece locally, but quickly realized that I didn’t have all the necessary dependencies that I needed.  I was in a spiral of writing a console app to go through a directory of assemblies, load up each assembly to find all of it’s references, and make sure they all were downloaded.  Thank you, cyclic dependency in the .NET framework (System references System.Configuration, which references System, etc.)

After a few google searches, I came across an article describing a PowerShell script that could load up assemblies and query the references.  The article was describing a different problem then I was trying to solve, but I decided to see if I could tweak it to help us out.  The more I started playing with it, the more I started to realize the potential.

Long story short, I’ve been using PowerShell almost exclusively over the last six weeks, and I’m loving it.  So far, I’ve written scripts (including but not limited to)

  • …that will parse a directory of assemblies (.dlls and .exes) and, given a list of directories, will search and download any needed dependencies until all the dependencies are downloaded from the list of directories into the original directory (it’ll also search the GAC just to be sure) – it also validates that you are downloading a specific version
  • …that will download a core website, and the client customizations on top of it, download all the dependencies (see first script), create all the necessary virtual directories (configurable), create an app pool with custom authentication that can be passed in, links the website to the app pool, and updates your host file with the site name
  • …that will download all configuration files for both applications and websites and will drop them in the appropriate folder for our application
  • …that will look into all configuration files that are downloaded and make sure the data in them is accurate (database server, file locations, etc) for their purpose and environment
  • …that will change all configuration files for an entire environment of clients to be used in a local setting (changing any database reference to “localhost”, changing all file paths to a local version, etc.)
  • …that will query my google spreadsheets, download all the information, parse it, and return it to me in a format I can use for personal financial reporting
  • …that uses the producteev API, and will add tasks to a given dashboard based on whatever criteria I provide
  • …that will poll all of the websites on a machine and return all of the virtual directories in a common format
  • …that will parse the output from the virtual directory “audit” (above script) and will return data in a format that helps me determine what virtual directories are common and which ones are individual
  • …that, given a URL, will hit the URL at a frequent interval, and scrape the page to show updated information
  • …that, given a list of our clients (around 26), will get the latest successful build location from TFS, and download it to a common location for deployment, then checks all of the downloaded assemblies to make sure the version of the references are updated
  • …and a bunch of little scripts and commands here and there to help me gather information

I’m completely digging PowerShell.  So far, I haven’t seen a reason to write another console app – though it’s much more flexible and powerful than most of the console apps I’ve written.  You have access to full .NET objects, so the possibilities are really limitless.  It’s not just for IT Administrators.  It’s really for Devs and DBAs as well.  It’s not just a scripting language.  It’s definitely so much more.

Are you using PowerShell?  If so, how?  If not, why?  :)

Follow

Get every new post delivered to your Inbox.

Join 670 other followers