February 1, 2011 | SQL Server

A bacon- (and module-) saving PowerShell incident

Earlier today I made a big goof.  I opened a module in Notepad, intending to use it as the basis for a new module.  I was in the process of using "File > Save As" when my phone rang just at the precise instant that, for some reason, made me click on "File > Save" by mistake.  After hitting Ctrl+Z 30 times to try to get the old version of the module back, I remembered that Notepad has never had more than one level of Undo.  Back when I was coding ASP by hand, I was very well aware of this, but I hadn't come across this situation in a long time, so I was a bit rusty.

I went back and forth with Aaron Nelson a bit on how to recover from this grave error, and I came across this gem, which I could run, thankfully, because I had several windows with the module already loaded:

 $f = "FunctionName"; (Get-Command $f Show-List).Definition;

That let me see the original function on the screen, but I wanted it in a file so I wouldn't have to transcribe it.  Easy enough:

 $f = "FunctionName"; (Get-Command $f Show-List).Definition | Out-File -Append "C:\Aaron\Moron.txt";

If I had messed up more than one function in the module, I would have had to repeat this process multiple times; in this case, I had only touched a single function.

Just wanted to point out that little tip in case any of you ever find yourself in the same situation.  (And, admittedly, so that I can easily find the command should I ever do this to myself again.)

Yes, I hear all of you screaming at me because I'm an idiot and I shouldn't be using Notepad.  Yep, you're probably right.  Just remember that we're not always working on our cozy little personal machines with all of our preferred tools and applications set up – sometimes we have to work when RDP'd into a server that we've never used before, and it is not always prudent to make it feel like home because, well, it's not.


12 comments on this post

    • Joe Webb - February 2, 2011, 2:45 AM

      Thanks, Aaron! Great info.  I hope I never need this, but you never know.

    • Peter Schott - February 2, 2011, 7:26 AM

      Good info, but I'd likely try to see if I could run something like http://portableapps.com/apps/development/notepadpp_portable. I realize that's not always a possibility either, especially in cases like yours.

    • Rhys - February 2, 2011, 2:52 PM

      nice, one for the bookmarks!

    • Jeffery Hicks - February 2, 2011, 11:49 PM

      I wrote a function for exporting functions (sounds chaotic I know) because you never know.  This is a great example.
      That's also why you should never use Notepad. šŸ˜‰  At least use the ISE which does have some Undo functionality.

    • Senol Balaban - February 6, 2011, 3:59 PM

      I'm not a scripter but, wouldn't be usefull and much better to use a version control sytem(e.g. SVN)?

    • AaronBertrand - February 6, 2011, 5:43 PM

      When I'm writing one-off and single-use PowerShell scripts on a client's system? No, I think source control would be overkill in a lot of these scenarios.

    • Tyler - February 7, 2011, 9:32 AM

      Never the less, a source control system is always better than executing things directly on a customer's server… We try to do that only in real emergencies… šŸ™‚

    • AaronBertrand - February 7, 2011, 3:09 PM

      Still not sure how that helps. Does source control have some kind of quality check? No. I can write a script with an infinite loop and it will still do that whether or not I checked it into source control first. I'm not against source control, and I use it where appropriate. I just don't agree that you need to use it for every single line of code you write in your entire career.

    • Kenny - February 7, 2011, 5:24 PM

      Aaron ā€“ I'd suggest a habit of at least making a copy of the file first *before* opening it (i.e. the copy) in Notepad if you're going to save your changes as another copy anyways. Nice save though!

    • AaronBertrand - February 7, 2011, 5:35 PM

      Yep. Like I said, out of the Notebook habit for several years. I learned a lot more from the save than from my bad practices.

    • David - February 7, 2011, 7:57 PM

      Good save.  But at least you could have dug it out of the Backups … you do have backups, right ?? šŸ™‚

    • Richard - February 7, 2011, 10:33 PM

      Or "Previous Versions" if you're on Vista or 7.

Comments are closed.